如何在Spring Security中创建自定义UserDetail对象 [英] How to create Custom UserDetail Object in Spring Security

查看:1033
本文介绍了如何在Spring Security中创建自定义UserDetail对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Spring Security构建了自定义的Authenticaton Manager,其功能如下

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

推荐答案

您需要实现UserDetailsS​​ervice并重写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.html http://www.javacodegeeks.com/2012/08/spring -security-implementing-custom.html

这篇关于如何在Spring Security中创建自定义UserDetail对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆