Skip to content

Commit ca43063

Browse files
committed
Impl custom rolePrefix in LdapUserDetailsManager
Closes spring-projectsgh-2083
1 parent fd3de41 commit ca43063

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

ldap/src/integration-test/java/org/springframework/security/ldap/userdetails/LdapUserDetailsManagerTests.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
* Copyright 2004-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@
4949
/**
5050
* @author Luke Taylor
5151
* @author Eddú Meléndez
52+
* @author Roman Zabaluev
5253
*/
5354
@ExtendWith(SpringExtension.class)
5455
@ContextConfiguration(classes = ApacheDsContainerConfig.class)
@@ -60,6 +61,8 @@ public class LdapUserDetailsManagerTests {
6061
private static final List<GrantedAuthority> TEST_AUTHORITIES = AuthorityUtils.createAuthorityList("ROLE_CLOWNS",
6162
"ROLE_ACROBATS");
6263

64+
private static final String DEFAULT_ROLE_PREFIX = "ROLE_";
65+
6366
private LdapUserDetailsManager mgr;
6467

6568
private SpringSecurityLdapTemplate template;
@@ -248,4 +251,35 @@ public void testPasswordChangeWithWrongOldPasswordFails() {
248251
.isThrownBy(() -> this.mgr.changePassword("wrongpassword", "yossariansnewpassword"));
249252
}
250253

254+
@Test
255+
public void testRoleNamesStartWithDefaultRolePrefix() {
256+
this.mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=people", "uid"));
257+
this.mgr.setGroupSearchBase("ou=groups");
258+
LdapUserDetails bob = (LdapUserDetails) this.mgr.loadUserByUsername("bob");
259+
260+
assertThat(bob.getAuthorities()).isNotEmpty();
261+
262+
bob.getAuthorities()
263+
.stream()
264+
.map(GrantedAuthority::getAuthority)
265+
.forEach((authority) -> assertThat(authority).startsWith(DEFAULT_ROLE_PREFIX));
266+
}
267+
268+
@Test
269+
public void testRoleNamesStartWithCustomRolePrefix() {
270+
var customPrefix = "GROUP_";
271+
this.mgr.setRolePrefix(customPrefix);
272+
273+
this.mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=people", "uid"));
274+
this.mgr.setGroupSearchBase("ou=groups");
275+
LdapUserDetails bob = (LdapUserDetails) this.mgr.loadUserByUsername("bob");
276+
277+
assertThat(bob.getAuthorities()).isNotEmpty();
278+
279+
bob.getAuthorities()
280+
.stream()
281+
.map(GrantedAuthority::getAuthority)
282+
.forEach((authority) -> assertThat(authority).startsWith(customPrefix));
283+
}
284+
251285
}

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,7 +104,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
104104
/** The attribute which contains members of a group */
105105
private String groupMemberAttributeName = "uniquemember";
106106

107-
private final String rolePrefix = "ROLE_";
107+
private String rolePrefix = "ROLE_";
108108

109109
/** The pattern to be used for the user search. {0} is the user's DN */
110110
private String groupSearchFilter = "(uniquemember={0})";
@@ -403,6 +403,16 @@ public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy secur
403403
this.securityContextHolderStrategy = securityContextHolderStrategy;
404404
}
405405

406+
/**
407+
* Sets the role prefix used when converting authorities. The default value is "ROLE_"
408+
* @param rolePrefix role prefix
409+
* @since 6.3
410+
*/
411+
public void setRolePrefix(String rolePrefix) {
412+
Assert.notNull(rolePrefix, "A rolePrefix must be supplied");
413+
this.rolePrefix = rolePrefix;
414+
}
415+
406416
private void changePasswordUsingAttributeModification(DistinguishedName userDn, String oldPassword,
407417
String newPassword) {
408418
ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE,

0 commit comments

Comments
 (0)