我已经编写了以下类:
PncUserDetailsAuthenticationProvider扩展自AbstractUserDetailsAuthenticationProvider
该类实现了receiveUser方法:
@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken token) throws AuthenticationException {
try {
server = (PncUtilRemote) new InitialContext().lookup("PncUtilBean");
if (server != null) {
String password = SHA1(token.getCredentials().toString());
String[] auth = server.login(username, password);
if (auth.length > 0) {
PncUserDetails details = new PncUserDetails(username, password);
for (int i = 0; i < auth.length; i++) {
details.addAuthority(auth[i]);
}
return details;
}
}
} catch (Exception e) {
System.out.println("! " + e.getClass().getName() + " in com.logica.pnc.security.PncUserDetailsAuthenticationProvider.retrieveUser(String, UsernamePasswordAuthenticationToken): " + e.getMessage());
}
throw new BadCredentialsException("");
}
为了启用您的AuthenticationProvider,您需要在application-context.xml文件中添加一些行:
<bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager">
<property name="providers">
<list><ref local="PncAuthenticationProvider" /></list>
</property>
</bean>
<bean id="PncAuthenticationProvider" class="com.logica.pnc.security.PncUserDetailsAuthenticationProvider">
<security:custom-authentication-provider />
</bean>
重要的是将auto-config设置为false:
<security:http auto-config="false" />
感谢rodrigoap指出AuthenticationProvider的问题 :)