我已经为 Spring Security 构建了我的自定义身份验证管理器,它是这样的
public class AccountAuthenticationProvider implements AuthenticationProvider{
@Autowired
private AuthenticationService authService;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String userName = authentication.getName();
String password = (String)authentication.getCredentials();
if(authService.isValid(userName,password)){
List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>();
grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_USER"));
SecurityContext securityContext = new SecurityContextImpl();
return new UsernamePasswordAuthenticationToken(userName,password);
}
return null;
}
public void setAuthService(AuthenticationService authService) {
this.authService = authService;
}
@Override
public boolean supports(Class<?> authentication) {
return true;
}
}
但是如何创建自己的自定义 UserDetail 对象?我将用它来存储与帐户相关的值
最佳答案
您需要实现 UserDetailsService 并覆盖 loadUserByUsername 方法以返回您自定义的 UserDetails 类。
检查以下链接:
http://www.javaroots.com/2013/03/how-to-use-custom-dao-classe-in-spring.html http://www.javacodegeeks.com/2012/08/spring-security-implementing-custom.html
关于java - 如何在 Spring Security 中创建自定义 UserDetail 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26447739/