1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 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.
@@ -62,12 +62,15 @@ public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
62
62
/**
63
63
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
64
64
* authority.
65
- * @param role the authority to check for prefixed with "ROLE_"
65
+ * @param role the authority to check for prefixed with "ROLE_". Role should not start
66
+ * with "ROLE_" since it is automatically prepended already.
66
67
* @param <T> the type of object being authorized
67
68
* @return the new instance
68
69
*/
69
70
public static <T > AuthorityAuthorizationManager <T > hasRole (String role ) {
70
71
Assert .notNull (role , "role cannot be null" );
72
+ Assert .isTrue (!role .startsWith (ROLE_PREFIX ), () -> role + " should not start with " + ROLE_PREFIX + " since "
73
+ + ROLE_PREFIX + " is automatically prepended when using hasRole. Consider using hasAuthority instead." );
71
74
return hasAuthority (ROLE_PREFIX + role );
72
75
}
73
76
@@ -86,7 +89,8 @@ public static <T> AuthorityAuthorizationManager<T> hasAuthority(String authority
86
89
/**
87
90
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
88
91
* authorities.
89
- * @param roles the authorities to check for prefixed with "ROLE_"
92
+ * @param roles the authorities to check for prefixed with "ROLE_". Each role should
93
+ * not start with "ROLE_" since it is automatically prepended already.
90
94
* @param <T> the type of object being authorized
91
95
* @return the new instance
92
96
*/
@@ -125,7 +129,11 @@ public static <T> AuthorityAuthorizationManager<T> hasAnyAuthority(String... aut
125
129
private static String [] toNamedRolesArray (String rolePrefix , String [] roles ) {
126
130
String [] result = new String [roles .length ];
127
131
for (int i = 0 ; i < roles .length ; i ++) {
128
- result [i ] = rolePrefix + roles [i ];
132
+ String role = roles [i ];
133
+ Assert .isTrue (!role .startsWith (rolePrefix ), () -> role + " should not start with " + rolePrefix + " since "
134
+ + rolePrefix
135
+ + " is automatically prepended when using hasAnyRole. Consider using hasAnyAuthority instead." );
136
+ result [i ] = rolePrefix + role ;
129
137
}
130
138
return result ;
131
139
}
0 commit comments