如何在 Spring Security 中创建自定义 UserDetail 对象 [英] How to create Custom UserDetail Object in Spring Security
本文介绍了如何在 Spring Security 中创建自定义 UserDetail 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经为 Spring Security 构建了我的自定义身份验证管理器,就像这样
I have built my custom Authenticaton Manager for Spring Security which goes something like this
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 对象?我将使用它来存储与帐户相关的值
but how do I create my own custom UserDetail object? I'll use that to store account related values
推荐答案
您需要实现 UserDetailsService 并覆盖 loadUserByUsername 方法以返回您自定义的 UserDetails 类.
you need to implement UserDetailsService and override loadUserByUsername method to return your customized UserDetails class.
检查以下链接:
http://www.javaroots.com/2013/03/how-to-use-custom-dao-classe-in-spring.htmlhttp://www.javacodegeeks.com/2012/08/spring-security-implementing-custom.html
这篇关于如何在 Spring Security 中创建自定义 UserDetail 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文