IllegalArgumentException:权限列表不能为空

时间:2016-03-29 21:23:24

标签: java spring

我正在Java Spring中编写一个Web应用程序。我有这段代码会导致一些麻烦:

List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
user.setAuthorities(authorities);

UserDetails userDetails = new MyUserDetails(user);
jdbcUserDetailsManager.setDataSource(jdbc.getDataSource());
jdbcUserDetailsManager.setJdbcTemplate(jdbc);
jdbcUserDetailsManager.createUser(userDetails);

最后一行导致异常

  

java.lang.IllegalArgumentException:权限列表不能为空

这是MyUserDetails类:

public final class MyUserDetails extends MyUser implements UserDetails {

private static final long serialVersionUID = 1L;

public MyUserDetails (MyUser user) {
    super(user);
    this.setPassword(user.getPassword());
    this.setLogin(user.getLogin());
    this.setAuthorities(user.getRoles());
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    return getRoles();
}

@Override
public String getUsername() {
    return getLogin();
}

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

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

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

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

这是MyUser类:

public class MyUser {

    String login;
    String password;
    List<GrantedAuthority> roles;

    public MyUser(MyUser user) {
        super();
        this.login = user.getLogin();
        this.password = user.getPassword();
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }

    public void setAuthorities(List<GrantedAuthority> authorities) {
        this.roles = roles;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public List<GrantedAuthority> getRoles() {
        return roles;
    }

}

非常感谢有关此主题的任何想法和建议。

1 个答案:

答案 0 :(得分:1)

MyUser班级替换

public void setAuthorities(List<GrantedAuthority> authorities) {
    this.roles = roles;
}

public void setAuthorities(List<GrantedAuthority> authorities) {
    this.roles = authorities;
}