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.
@@ -46,12 +46,15 @@ private AuthorityAuthorizationManager(String... authorities) {
46
46
/**
47
47
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
48
48
* authority.
49
- * @param role the authority to check for prefixed with "ROLE_"
49
+ * @param role the authority to check for prefixed with "ROLE_". Role should not start
50
+ * with "ROLE_" since it is automatically prepended already.
50
51
* @param <T> the type of object being authorized
51
52
* @return the new instance
52
53
*/
53
54
public static <T > AuthorityAuthorizationManager <T > hasRole (String role ) {
54
55
Assert .notNull (role , "role cannot be null" );
56
+ Assert .isTrue (!role .startsWith (ROLE_PREFIX ), () -> role + " should not start with " + ROLE_PREFIX + " since "
57
+ + ROLE_PREFIX + " is automatically prepended when using hasRole. Consider using hasAuthority instead." );
55
58
return hasAuthority (ROLE_PREFIX + role );
56
59
}
57
60
@@ -70,7 +73,8 @@ public static <T> AuthorityAuthorizationManager<T> hasAuthority(String authority
70
73
/**
71
74
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
72
75
* authorities.
73
- * @param roles the authorities to check for prefixed with "ROLE_"
76
+ * @param roles the authorities to check for prefixed with "ROLE_". Each role should
77
+ * not start with "ROLE_" since it is automatically prepended already.
74
78
* @param <T> the type of object being authorized
75
79
* @return the new instance
76
80
*/
@@ -109,7 +113,11 @@ public static <T> AuthorityAuthorizationManager<T> hasAnyAuthority(String... aut
109
113
private static String [] toNamedRolesArray (String rolePrefix , String [] roles ) {
110
114
String [] result = new String [roles .length ];
111
115
for (int i = 0 ; i < roles .length ; i ++) {
112
- result [i ] = rolePrefix + roles [i ];
116
+ String role = roles [i ];
117
+ Assert .isTrue (!role .startsWith (rolePrefix ), () -> role + " should not start with " + rolePrefix + " since "
118
+ + rolePrefix
119
+ + " is automatically prepended when using hasAnyRole. Consider using hasAnyAuthority instead." );
120
+ result [i ] = rolePrefix + role ;
113
121
}
114
122
return result ;
115
123
}
0 commit comments