@@ -57,12 +57,15 @@ public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
57
57
/**
58
58
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
59
59
* authority.
60
- * @param role the authority to check for prefixed with "ROLE_"
60
+ * @param role the authority to check for prefixed with "ROLE_". Role should not start
61
+ * with "ROLE_" since it is automatically prepended already.
61
62
* @param <T> the type of object being authorized
62
63
* @return the new instance
63
64
*/
64
65
public static <T > AuthorityAuthorizationManager <T > hasRole (String role ) {
65
66
Assert .notNull (role , "role cannot be null" );
67
+ Assert .isTrue (!role .startsWith (ROLE_PREFIX ), () -> role + " should not start with " + ROLE_PREFIX + " since "
68
+ + ROLE_PREFIX + " is automatically prepended when using hasRole. Consider using hasAuthority instead." );
66
69
return hasAuthority (ROLE_PREFIX + role );
67
70
}
68
71
@@ -81,7 +84,8 @@ public static <T> AuthorityAuthorizationManager<T> hasAuthority(String authority
81
84
/**
82
85
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
83
86
* authorities.
84
- * @param roles the authorities to check for prefixed with "ROLE_"
87
+ * @param roles the authorities to check for prefixed with "ROLE_". Each role should
88
+ * not start with "ROLE_" since it is automatically prepended already.
85
89
* @param <T> the type of object being authorized
86
90
* @return the new instance
87
91
*/
@@ -120,7 +124,11 @@ public static <T> AuthorityAuthorizationManager<T> hasAnyAuthority(String... aut
120
124
private static String [] toNamedRolesArray (String rolePrefix , String [] roles ) {
121
125
String [] result = new String [roles .length ];
122
126
for (int i = 0 ; i < roles .length ; i ++) {
123
- result [i ] = rolePrefix + roles [i ];
127
+ String role = roles [i ];
128
+ Assert .isTrue (!role .startsWith (rolePrefix ), () -> role + " should not start with " + rolePrefix + " since "
129
+ + rolePrefix
130
+ + " is automatically prepended when using hasAnyRole. Consider using hasAnyAuthority instead." );
131
+ result [i ] = rolePrefix + role ;
124
132
}
125
133
return result ;
126
134
}
0 commit comments