gpt4 book ai didi

java - 非法参数异常 : Authorities list must not be null

转载 作者:行者123 更新时间:2023-12-01 10:08:50 31 4
gpt4 key购买 nike

我正在使用 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: Authorities list must not be null

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

}

有关此主题的任何想法和建议将不胜感激。

最佳答案

MyUser类中替换

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

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

关于java - 非法参数异常 : Authorities list must not be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295350/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com