1
1
/*
2
- * Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2
+ * Copyright 2004-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
49
49
/**
50
50
* @author Luke Taylor
51
51
* @author Eddú Meléndez
52
+ * @author Roman Zabaluev
52
53
*/
53
54
@ ExtendWith (SpringExtension .class )
54
55
@ ContextConfiguration (classes = ApacheDsContainerConfig .class )
@@ -60,6 +61,8 @@ public class LdapUserDetailsManagerTests {
60
61
private static final List <GrantedAuthority > TEST_AUTHORITIES = AuthorityUtils .createAuthorityList ("ROLE_CLOWNS" ,
61
62
"ROLE_ACROBATS" );
62
63
64
+ private static final String DEFAULT_ROLE_PREFIX = "ROLE_" ;
65
+
63
66
private LdapUserDetailsManager mgr ;
64
67
65
68
private SpringSecurityLdapTemplate template ;
@@ -248,4 +251,35 @@ public void testPasswordChangeWithWrongOldPasswordFails() {
248
251
.isThrownBy (() -> this .mgr .changePassword ("wrongpassword" , "yossariansnewpassword" ));
249
252
}
250
253
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
+
251
285
}
0 commit comments