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

标签 java spring spring-mvc spring-security

我已经为 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/

相关文章:

java - Spring Kafka Embedded - 测试之间已经存在主题

java - 如何创建 Alfresco Share Java 支持的媒体查看器?

Spring可选@PathVariable?

java - 当我尝试连接到多个数据库时,为什么会出现 Spring JDBCTemplate 问题?

java - 一对多 hibernate 的空约束

java - libgdx 使用像素图从叠加层创建纹理

java - java.ext.dirs如何支持多个目录

java - 从 spring boot war 文件中运行 linux 命令

java - 异常 org.springframework.beans.factory.UnsatisfiedDependencyException

java - 如何限制上传到 Spring MVC3 Controller 的文件类型