【问题标题】:Spring security Role cannot be converted to granted authoritySpring 安全角色无法转换为授予权限
【发布时间】:2015-09-19 15:18:08
【问题描述】:

User.java

public class User implements Serializable{
    @Id
    @Size(min=5,max=15)
    @Column(name="username", unique=true)
    private String username;

    @OneToMany(mappedBy="user")
    private Collection<Role> roles;

public User(User user) {
        this.username=user.username;
        this.roles=user.getRoles();
    }
}

Role.java

public class Role implements Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private String role;

    @ManyToOne
    @JoinColumn(name="username")
    private User user;
}

UserServiceImpl.java

public class UserServiceImpl implements UserServiceDetails {

    private UserRepo userRepo;

    @Autowired
    public void setUserRepo(UserRepo userRepo) {
        this.userRepo = userRepo;
    }

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        User user=userRepo.findUserByUsername(username);
        if(user == null) {
            throw new UsernameNotFoundException("Could not find user " + username);
        }
        CustomUser customUser=new CustomUser(user);
        return customUser;
    }

    public final static class CustomUser extends User implements UserDetails
    {
        public CustomUser(User user) {
            super(user);
        }

        @SuppressWarnings("unchecked")
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            List<GrantedAuthority> authorities =
                    new ArrayList<GrantedAuthority>();
            authorities.addAll((Collection<? extends GrantedAuthority>) getRoles());

            return authorities;
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

    }

}

SecurityCONfig.java

public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth
            .userDetailsService(userDetailsService); 
    }
}

错误: 角色不能转换为 org.springframework.security.core.GrantedAuthority 我的角色表包含​​表单字符串“用户”、“管理员”等形式的角色

有什么建议吗?

谢谢

【问题讨论】:

  • 买一本关于 java 和接口如何工作的书。您的Role 是否实现GrantedAuthority?这就是告诉你的错误。

标签: java spring spring-mvc spring-security


【解决方案1】:

正如 Denium 所说,转换不会像在您的代码中那样工作。我通过循环遍历每个角色来做类似的事情,如下所示:

authorities = new HashSet<GrantedAuthority>(roles.size());

for (String role : roles)
    authorities.add(new SimpleGrantedAuthority("ROLE_" + role));

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 2018-10-11
    • 2020-12-07
    • 2015-10-28
    • 1970-01-01
    • 2020-04-27
    • 2019-04-13
    • 2017-10-30
    相关资源
    最近更新 更多