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.
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}
0 commit comments