Skip to content

Implement UserDetailsPasswordService in JdbcUserDetailsManager #17071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.cache.NullUserCache;
import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;
import org.springframework.util.Assert;
Expand All @@ -63,9 +64,11 @@
* using this implementation for managing your users.
*
* @author Luke Taylor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add your name on here?

* @author Junhyeok Lee
* @since 2.0
*/
public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsManager, GroupManager {
public class JdbcUserDetailsManager extends JdbcDaoImpl
implements UserDetailsManager, GroupManager, UserDetailsPasswordService {

public static final String DEF_CREATE_USER_SQL = "insert into users (username, password, enabled) values (?,?,?)";

Expand Down Expand Up @@ -162,6 +165,8 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa

private RowMapper<GrantedAuthority> grantedAuthorityMapper = this::mapToGrantedAuthority;

private boolean enableUpdatePassword = false;

public JdbcUserDetailsManager() {
}

Expand Down Expand Up @@ -590,6 +595,20 @@ public void setUserCache(UserCache userCache) {
this.userCache = userCache;
}

/**
* Sets whether the {@link #updatePassword(UserDetails, String)} method should
* actually update the password.
* <p>
* Defaults to {@code false} to prevent accidental password updates that might produce
* passwords that are too large for the current database schema. Users must explicitly
* set this to {@code true} to enable password updates.
* @param enableUpdatePassword {@code true} to enable password updates, {@code false}
* otherwise.
*/
public void setEnableUpdatePassword(boolean enableUpdatePassword) {
this.enableUpdatePassword = enableUpdatePassword;
}

private void validateUserDetails(UserDetails user) {
Assert.hasText(user.getUsername(), "Username may not be empty or null");
validateAuthorities(user.getAuthorities());
Expand All @@ -603,4 +622,18 @@ private void validateAuthorities(Collection<? extends GrantedAuthority> authorit
}
}

/**
* Conditionally updates password based on the setting from
* {@link #setEnableUpdatePassword(boolean)}. {@inheritDoc}
*/
@Override
public UserDetails updatePassword(UserDetails user, String newPassword) {
Comment on lines +629 to +630

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Override
public UserDetails updatePassword(UserDetails user, String newPassword) {
@Override
/**
* comments like "updatePassword only when {@link #enableUpdatePassword} is true?
*/
public UserDetails updatePassword(UserDetails user, String newPassword) {

nit. how about adding comment about updatePassword flag?

if (this.enableUpdatePassword) {
UserDetails updated = User.withUserDetails(user).password(newPassword).build();
updateUser(updated);
return updated;
}
return user;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
*
* @author Luke Taylor
* @author dae won
* @author Junhyeok Lee
*/
public class JdbcUserDetailsManagerTests {

Expand Down Expand Up @@ -405,6 +406,23 @@ public void setGrantedAuthorityMapperWithMockMapper() throws SQLException {
verify(mockMapper).mapRow(any(), anyInt());
}

@Test
void updatePasswordWhenDisabledReturnOriginalUser() {
insertJoe();
this.manager.updatePassword(joe, "new");
UserDetails newJoe = this.manager.loadUserByUsername("joe");
assertThat(newJoe.getPassword()).isEqualTo("password");
}

@Test
void updatePasswordWhenEnabledShouldUpdatePassword() {
insertJoe();
this.manager.setEnableUpdatePassword(true);
this.manager.updatePassword(joe, "new");
UserDetails newJoe = this.manager.loadUserByUsername("joe");
assertThat(newJoe.getPassword()).isEqualTo("new");
}

private Authentication authenticateJoe() {
UsernamePasswordAuthenticationToken auth = UsernamePasswordAuthenticationToken.authenticated("joe", "password",
joe.getAuthorities());
Expand Down