Skip to content

Commit 215547f

Browse files
committed
Use UsernameNotFoundException Factory
Issue gh-17179
1 parent da2d9aa commit 215547f

File tree

6 files changed

+6
-7
lines changed

6 files changed

+6
-7
lines changed

core/src/main/java/org/springframework/security/provisioning/InMemoryUserDetailsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public UserDetails updatePassword(UserDetails user, String newPassword) {
164164
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
165165
UserDetails user = this.users.get(username.toLowerCase(Locale.ROOT));
166166
if (user == null) {
167-
throw new UsernameNotFoundException("user '" + username + "' not found");
167+
throw UsernameNotFoundException.fromUsername(username);
168168
}
169169
if (user instanceof CredentialsContainer) {
170170
return user;

ldap/src/main/java/org/springframework/security/ldap/authentication/PasswordComparisonAuthenticator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public DirContextOperations authenticate(final Authentication authentication) {
9393
}
9494
}
9595
if (user == null) {
96-
throw new UsernameNotFoundException("User not found: " + username);
96+
throw UsernameNotFoundException.fromUsername(username);
9797
}
9898
if (logger.isTraceEnabled()) {
9999
logger.trace(LogMessage.format("Comparing password attribute '%s' for user '%s'",

ldap/src/main/java/org/springframework/security/ldap/authentication/ad/ActiveDirectoryLdapAuthenticationProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ private DirContextOperations searchForUser(DirContext context, String username)
307307
throw ex;
308308
}
309309
// If we found no results, then the username/password did not match
310-
UsernameNotFoundException userNameNotFoundException = new UsernameNotFoundException(
311-
"User " + username + " not found in directory.", ex);
310+
UsernameNotFoundException userNameNotFoundException = UsernameNotFoundException.fromUsername(username, ex);
312311
throw badCredentials(userNameNotFoundException);
313312
}
314313
}

ldap/src/main/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public DirContextOperations searchForUser(String username) {
104104
}
105105
catch (IncorrectResultSizeDataAccessException ex) {
106106
if (ex.getActualSize() == 0) {
107-
throw new UsernameNotFoundException("User " + username + " not found in directory.");
107+
throw UsernameNotFoundException.fromUsername(username);
108108
}
109109
// Search should never return multiple results if properly configured
110110
throw ex;

ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapUserDetailsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private DirContextAdapter loadUserAsContext(final LdapName dn, final String user
154154
return new DirContextAdapter(attrs, LdapUtils.getFullDn(dn, ctx));
155155
}
156156
catch (NameNotFoundException ex) {
157-
throw new UsernameNotFoundException("User " + username + " not found", ex);
157+
throw UsernameNotFoundException.fromUsername(username, ex);
158158
}
159159
});
160160
}

web/src/main/java/org/springframework/security/web/server/authentication/ReactivePreAuthenticatedAuthenticationManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Mono<Authentication> authenticate(Authentication authentication) {
6262
.filter(this::supports)
6363
.map(Authentication::getName)
6464
.flatMap(this.userDetailsService::findByUsername)
65-
.switchIfEmpty(Mono.error(() -> new UsernameNotFoundException("User not found")))
65+
.switchIfEmpty(Mono.error(() -> UsernameNotFoundException.fromUsername(authentication.getName())))
6666
.doOnNext(this.userDetailsChecker::check)
6767
.map((userDetails) -> {
6868
PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(userDetails,

0 commit comments

Comments
 (0)