相关疑难解决方法(0)

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

我为Spring Security构建了自定义的Authenticaton Manager,就像这样

   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;
    }

}
Run Code Online (Sandbox Code Playgroud)

但是如何创建自己的自定义UserDetail对象?我将使用它来存储与帐户相关的值

java spring spring-mvc spring-security

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×1

spring ×1

spring-mvc ×1

spring-security ×1