gpt4 book ai didi

java - 如何在 Spring Security 中创建自定义 UserDetail 对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:40:00 30 4
gpt4 key购买 nike

我已经为 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 对象?我将用它来存储与帐户相关的值

最佳答案

您需要实现 UserDetailsS​​ervice 并覆盖 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com