在使用 Spring Security 时遇到一个问题,我自己定义了 User 的 POJO,UserDetail 是用的官方的,然后官方的一个类名也叫 User,所以只能在这里把官方的全名写出来,就是很长,有没有更优雅的方法来解决?
或许只能给自己的类改名?但是还有比 User 更清楚明确的名字吗...
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
} else {
Set grantedAuthorities = new HashSet<>();
for (Role role : user.getRoles()) {
grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
}
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
}
}