• notice
  • Congratulations on the launch of the Sought Tech site

Sping Security custom UserDetailsService keeps reporting the return value is empty exception

Use a custom UserDetail class as the return object, the code is as follows

@Data
public class CustomUser implements Serializable, UserDetails {
    private String username;
    private String password;
    private boolean state;
    private List<GrantedAuthority> authorities;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }
    @Override
    public String getPassword() {
        return this.password;
    }
    @Override
    public String getUsername() {
        return this.username;
    }
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    @Override
    public boolean isEnabled() {
        return true;
    }
    public CustomUser(String username, String password, boolean state, List<GrantedAuthority> authorities) {
        this.username = username;
        this.password = password;
        this.state = state;
        this.authorities = authorities;
    }
}

It is no problem to use the instance of UserDetail to return the value.Once I use the CustomUser defined by myself as the return object, it will report a null value exception.

@Service
public class CustomUserDetailService implements UserDetailsService {
     @Autowired
     private UsersDao usersDao;
     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
         if (StringUtils.isEmpty(username)) {
             throw new NullPointerException("CustomUserDetailService.loadUserByUsername: Null value exception!");
         }
         UsersPOJO user = usersDao.selectOneByUseremailUsersPojo(username);
         if (StringUtils.isEmpty(user)) {
             throw new UsernameNotFoundException("The user does not exist!");
         }
         return new User(user.getUseremail(), user.getPassword(), new ArrayList<>());
     }
}


return new User(user.getUseremail(), user.getPassword(), new ArrayList<>()); Only report a null pointer if you pass an empty list here

Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

order lipitor 10mg generic & lt;a href="https://lipiws.top/"& gt;buy lipitor 10mg pills& lt;/a& gt; atorvastatin 40mg tablet

Mfoblz

2024-03-08

Leave a Reply

+