当用户尝试使用暂停、锁定或无效的帐户调用我的网络服务时,我试图显示自定义错误。
问题是,无论我尝试什么,相同的消息不断返回:“访问此资源需要完全身份验证”
我的 CustomUserDetailsService 是这样的:
@Service
public class CustomUserDetailsService implements UserDetailsService {
private static final Logger logger = LogManager.getLogger(CustomUserDetailsService.class);
private @Autowired CredentialsServiceQuery credentials;
private @Autowired MemberProfile memberProfile;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User.UserBuilder builder = null;
try {
boolean exists = credentials.checkUserExists(username);
if (exists) {
memberProfile = credentials.getUserInformation(username);
builder = User.withUsername(username);
builder.password(memberProfile.getPassword());
builder.authorities(getGrantedAuthorities());
logger.info("User exists: {}", username);
} else {
throw new UsernameNotFoundException(SpringSecurityMessageSource.getAccessor().getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", new Object[] {username}, "User credentials is wrong"));
}
} catch (Exception ex) {
throw new UsernameNotFoundException(SpringSecurityMessageSource.getAccessor().getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", new Object[] {username}, "User credentials is wrong"));
//throw new UsernameNotFoundException("An error occured while trying to find the username, " + username, ex);
}
return builder.build();
}
private List<GrantedAuthority> getGrantedAuthorities(){
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.clear();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
return authorities;
}
}
素胚勾勒不出你
相关分类