Skip to content

Commit

Permalink
Add grantedAuthorityMapper as a class member
Browse files Browse the repository at this point in the history
- Add unit tests for setGrantedAuthorityMapper method

Signed-off-by: dae won <[email protected]>
  • Loading branch information
big-cir authored and jzheaux committed Feb 24, 2025
1 parent cb07031 commit 45b51fe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa

private RowMapper<UserDetails> userDetailsMapper = this::mapToUser;

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

public JdbcUserDetailsManager() {
}

Expand All @@ -182,6 +184,21 @@ public void setUserDetailsMapper(RowMapper<UserDetails> mapper) {
this.userDetailsMapper = mapper;
}

/**
* Sets the {@code RowMapper} to convert each authority result row into a
* {@link GrantedAuthority} object.
*
* The default mapper expects columns with names like 'authority' or 'role', and maps
* them directly to SimpleGrantedAuthority objects.
* @param mapper the {@code RowMapper} to use for mapping rows in the database to
* GrantedAuthority objects, must not be null
* @since 6.5
*/
public void setGrantedAuthorityMapper(RowMapper<GrantedAuthority> mapper) {
Assert.notNull(mapper, "grantedAuthorityMapper cannot be null");
this.grantedAuthorityMapper = mapper;
}

@Override
protected void initDao() throws ApplicationContextException {
if (this.authenticationManager == null) {
Expand All @@ -197,7 +214,7 @@ protected void initDao() throws ApplicationContextException {
*/
@Override
protected List<UserDetails> loadUsersByUsername(String username) {
return getJdbcTemplate().query(getUsersByUsernameQuery(), userDetailsMapper, username);
return getJdbcTemplate().query(getUsersByUsernameQuery(), this.userDetailsMapper, username);
}

private UserDetails mapToUser(ResultSet rs, int rowNum) throws SQLException {
Expand Down Expand Up @@ -406,10 +423,10 @@ public List<GrantedAuthority> findGroupAuthorities(String groupName) {
this.logger.debug("Loading authorities for group '" + groupName + "'");
Assert.hasText(groupName, "groupName should have text");
return getJdbcTemplate().query(this.groupAuthoritiesSql, new String[] { groupName },
this::mapToGrantedAuthority);
this.grantedAuthorityMapper);
}

protected GrantedAuthority mapToGrantedAuthority(ResultSet rs, int rowNum) throws SQLException {
private GrantedAuthority mapToGrantedAuthority(ResultSet rs, int rowNum) throws SQLException {
String roleName = getRolePrefix() + rs.getString(3);
return new SimpleGrantedAuthority(roleName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.verify;

/**
* Tests for {@link JdbcUserDetailsManager}
Expand Down Expand Up @@ -373,21 +372,39 @@ public void createNewAuthenticationUsesNullPasswordToKeepPassordsSave() {
@Test
public void setUserDetailsMapperWithNullMapperThrowsException() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> this.manager.setUserDetailsMapper(null))
.withMessage("userDetailsMapper cannot be null");
.isThrownBy(() -> this.manager.setUserDetailsMapper(null))
.withMessage("userDetailsMapper cannot be null");
}

@Test
public void setUserDetailsMapperWithMockMapper() throws SQLException {
RowMapper<UserDetails> mockMapper = mock(RowMapper.class);
when(mockMapper.mapRow(any(), anyInt())).thenReturn(joe);
given(mockMapper.mapRow(any(), anyInt())).willReturn(joe);
this.manager.setUserDetailsMapper(mockMapper);
insertJoe();
UserDetails newJoe = this.manager.loadUserByUsername("joe");
assertThat(joe).isEqualTo(newJoe);
verify(mockMapper).mapRow(any(), anyInt());
}

@Test
public void setGrantedAuthorityMapperWithNullMapperThrowsException() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> this.manager.setGrantedAuthorityMapper(null))
.withMessage("grantedAuthorityMapper cannot be null");
}

@Test
public void setGrantedAuthorityMapperWithMockMapper() throws SQLException {
RowMapper<GrantedAuthority> mockMapper = mock(RowMapper.class);
GrantedAuthority mockAuthority = new SimpleGrantedAuthority("ROLE_MOCK");
given(mockMapper.mapRow(any(), anyInt())).willReturn(mockAuthority);
this.manager.setGrantedAuthorityMapper(mockMapper);
List<GrantedAuthority> authGroup = this.manager.findGroupAuthorities("GROUP_0");
assertThat(authGroup.get(0)).isEqualTo(mockAuthority);
verify(mockMapper).mapRow(any(), anyInt());
}

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

0 comments on commit 45b51fe

Please sign in to comment.