Use UsernameNotFoundException Factory

Issue gh-17179
This commit is contained in:
Josh Cummings 2025-05-28 14:13:02 -06:00
parent da2d9aa868
commit 215547f8c8
No known key found for this signature in database
GPG Key ID: 869B37A20E876129
6 changed files with 6 additions and 7 deletions

View File

@ -164,7 +164,7 @@ public class InMemoryUserDetailsManager implements UserDetailsManager, UserDetai
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserDetails user = this.users.get(username.toLowerCase(Locale.ROOT));
if (user == null) {
throw new UsernameNotFoundException("user '" + username + "' not found");
throw UsernameNotFoundException.fromUsername(username);
}
if (user instanceof CredentialsContainer) {
return user;

View File

@ -93,7 +93,7 @@ public final class PasswordComparisonAuthenticator extends AbstractLdapAuthentic
}
}
if (user == null) {
throw new UsernameNotFoundException("User not found: " + username);
throw UsernameNotFoundException.fromUsername(username);
}
if (logger.isTraceEnabled()) {
logger.trace(LogMessage.format("Comparing password attribute '%s' for user '%s'",

View File

@ -307,8 +307,7 @@ public final class ActiveDirectoryLdapAuthenticationProvider extends AbstractLda
throw ex;
}
// If we found no results, then the username/password did not match
UsernameNotFoundException userNameNotFoundException = new UsernameNotFoundException(
"User " + username + " not found in directory.", ex);
UsernameNotFoundException userNameNotFoundException = UsernameNotFoundException.fromUsername(username, ex);
throw badCredentials(userNameNotFoundException);
}
}

View File

@ -104,7 +104,7 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
}
catch (IncorrectResultSizeDataAccessException ex) {
if (ex.getActualSize() == 0) {
throw new UsernameNotFoundException("User " + username + " not found in directory.");
throw UsernameNotFoundException.fromUsername(username);
}
// Search should never return multiple results if properly configured
throw ex;

View File

@ -154,7 +154,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
return new DirContextAdapter(attrs, LdapUtils.getFullDn(dn, ctx));
}
catch (NameNotFoundException ex) {
throw new UsernameNotFoundException("User " + username + " not found", ex);
throw UsernameNotFoundException.fromUsername(username, ex);
}
});
}

View File

@ -62,7 +62,7 @@ public class ReactivePreAuthenticatedAuthenticationManager implements ReactiveAu
.filter(this::supports)
.map(Authentication::getName)
.flatMap(this.userDetailsService::findByUsername)
.switchIfEmpty(Mono.error(() -> new UsernameNotFoundException("User not found")))
.switchIfEmpty(Mono.error(() -> UsernameNotFoundException.fromUsername(authentication.getName())))
.doOnNext(this.userDetailsChecker::check)
.map((userDetails) -> {
PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(userDetails,