Skip to content

Commit d506c1a

Browse files
committed
Add grantedAuthorityMapper as a class member
- Add unit tests for setGrantedAuthorityMapper method Signed-off-by: dae won <[email protected]>
1 parent a427643 commit d506c1a

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

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

+19-3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
159159

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

162+
private RowMapper<GrantedAuthority> grantedAuthorityMapper = this::mapToGrantedAuthority;
163+
162164
public JdbcUserDetailsManager() {
163165
}
164166

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

187+
/**
188+
* Sets the {@code RowMapper} to convert each authority result row into a
189+
* {@link GrantedAuthority} object.
190+
*
191+
* The default mapper expects columns with names like 'authority' or 'role', and maps
192+
* them directly to SimpleGrantedAuthority objects.
193+
* @param mapper the {@code RowMapper} to use for mapping rows in the database to
194+
* GrantedAuthority objects, must not be null
195+
* @since 6.5
196+
*/
197+
public void setGrantedAuthorityMapper(RowMapper<GrantedAuthority> mapper) {
198+
Assert.notNull(mapper, "grantedAuthorityMapper cannot be null");
199+
this.grantedAuthorityMapper = mapper;
200+
}
201+
185202
@Override
186203
protected void initDao() throws ApplicationContextException {
187204
if (this.authenticationManager == null) {
@@ -405,11 +422,10 @@ public void removeUserFromGroup(final String username, final String groupName) {
405422
public List<GrantedAuthority> findGroupAuthorities(String groupName) {
406423
this.logger.debug("Loading authorities for group '" + groupName + "'");
407424
Assert.hasText(groupName, "groupName should have text");
408-
return getJdbcTemplate().query(this.groupAuthoritiesSql, new String[] { groupName },
409-
this::mapToGrantedAuthority);
425+
return getJdbcTemplate().query(this.groupAuthoritiesSql, new String[] { groupName }, grantedAuthorityMapper);
410426
}
411427

412-
protected GrantedAuthority mapToGrantedAuthority(ResultSet rs, int rowNum) throws SQLException {
428+
private GrantedAuthority mapToGrantedAuthority(ResultSet rs, int rowNum) throws SQLException {
413429
String roleName = getRolePrefix() + rs.getString(3);
414430
return new SimpleGrantedAuthority(roleName);
415431
}

core/src/test/java/org/springframework/security/provisioning/JdbcUserDetailsManagerTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import static org.mockito.ArgumentMatchers.anyInt;
5454
import static org.mockito.BDDMockito.given;
5555
import static org.mockito.Mockito.mock;
56+
import static org.mockito.Mockito.times;
5657
import static org.mockito.Mockito.verify;
5758
import static org.mockito.Mockito.when;
5859

@@ -388,6 +389,24 @@ public void setUserDetailsMapperWithMockMapper() throws SQLException {
388389
verify(mockMapper).mapRow(any(), anyInt());
389390
}
390391

392+
@Test
393+
public void setGrantedAuthorityMapperWithNullMapperThrowsException() {
394+
assertThatExceptionOfType(IllegalArgumentException.class)
395+
.isThrownBy(() -> this.manager.setGrantedAuthorityMapper(null))
396+
.withMessage("grantedAuthorityMapper cannot be null");
397+
}
398+
399+
@Test
400+
public void setGrantedAuthorityMapperWithMockMapper() throws SQLException {
401+
RowMapper<GrantedAuthority> mockMapper = mock(RowMapper.class);
402+
GrantedAuthority mockAuthority = new SimpleGrantedAuthority("ROLE_MOCK");
403+
when(mockMapper.mapRow(any(), anyInt())).thenReturn(mockAuthority);
404+
this.manager.setGrantedAuthorityMapper(mockMapper);
405+
List<GrantedAuthority> authGroup = this.manager.findGroupAuthorities("GROUP_0");
406+
assertThat(authGroup.get(0)).isEqualTo(mockAuthority);
407+
verify(mockMapper).mapRow(any(), anyInt());
408+
}
409+
391410
private Authentication authenticateJoe() {
392411
UsernamePasswordAuthenticationToken auth = UsernamePasswordAuthenticationToken.authenticated("joe", "password",
393412
joe.getAuthorities());

0 commit comments

Comments
 (0)