Lowercase username in changePassword lookup
InMemoryUserDetailsManager keys its user map on the lower-cased username everywhere except changePassword, which looked the current user up with the raw name. A user whose username contains uppercase letters could therefore not change its password. Lower-case the lookup key to match the rest of the class. Closes gh-19336 Signed-off-by: junhyeong9812 <pickjog@gmail.com>
This commit is contained in:
committed by
Josh Cummings
parent
2aa3c1ab2e
commit
9fdd2dc675
+1
-1
@@ -149,7 +149,7 @@ public class InMemoryUserDetailsManager implements UserDetailsManager, UserDetai
|
||||
else {
|
||||
this.logger.debug("No authentication manager set. Password won't be re-checked.");
|
||||
}
|
||||
MutableUserDetails user = this.users.get(username);
|
||||
MutableUserDetails user = this.users.get(username.toLowerCase(Locale.ROOT));
|
||||
Assert.state(user != null, "Current user doesn't exist in database.");
|
||||
user.setPassword(newPassword);
|
||||
}
|
||||
|
||||
+16
@@ -123,6 +123,22 @@ public class InMemoryUserDetailsManagerTests {
|
||||
verify(strategy).getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void changePasswordWhenCurrentUsernameIsNotInLowercaseThenChangesPassword() {
|
||||
UserDetails userNotLowerCase = User.withUserDetails(PasswordEncodedUser.user()).username("User").build();
|
||||
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(userNotLowerCase);
|
||||
Authentication authentication = new UsernamePasswordAuthenticationToken("User", userNotLowerCase.getPassword(),
|
||||
userNotLowerCase.getAuthorities());
|
||||
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
|
||||
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
|
||||
manager.setSecurityContextHolderStrategy(strategy);
|
||||
|
||||
String newPassword = "newPassword";
|
||||
manager.changePassword(userNotLowerCase.getPassword(), newPassword);
|
||||
|
||||
assertThat(manager.loadUserByUsername("User").getPassword()).isEqualTo(newPassword);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createUserWhenUserAlreadyExistsThenException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.manager.createUser(this.user))
|
||||
|
||||
Reference in New Issue
Block a user