Skip to content

Commit ebabcaa

Browse files
committed
Merge branch '5.7.x' into 5.8.x
2 parents e7b14b3 + 094bf1b commit ebabcaa

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

core/src/main/java/org/springframework/security/authorization/AuthorityAuthorizationManager.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -62,12 +62,15 @@ public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
6262
/**
6363
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
6464
* 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.
6667
* @param <T> the type of object being authorized
6768
* @return the new instance
6869
*/
6970
public static <T> AuthorityAuthorizationManager<T> hasRole(String role) {
7071
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.");
7174
return hasAuthority(ROLE_PREFIX + role);
7275
}
7376

@@ -86,7 +89,8 @@ public static <T> AuthorityAuthorizationManager<T> hasAuthority(String authority
8689
/**
8790
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
8891
* 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.
9094
* @param <T> the type of object being authorized
9195
* @return the new instance
9296
*/
@@ -125,7 +129,11 @@ public static <T> AuthorityAuthorizationManager<T> hasAnyAuthority(String... aut
125129
private static String[] toNamedRolesArray(String rolePrefix, String[] roles) {
126130
String[] result = new String[roles.length];
127131
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;
129137
}
130138
return result;
131139
}

core/src/test/java/org/springframework/security/authorization/AuthorityAuthorizationManagerTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -44,6 +44,15 @@ public void hasRoleWhenNullThenException() {
4444
.withMessage("role cannot be null");
4545
}
4646

47+
@Test
48+
public void hasRoleWhenContainRoleWithRolePrefixThenException() {
49+
String ROLE_PREFIX = "ROLE_";
50+
String ROLE_USER = ROLE_PREFIX + "USER";
51+
assertThatIllegalArgumentException().isThrownBy(() -> AuthorityAuthorizationManager.hasRole(ROLE_USER))
52+
.withMessage(ROLE_USER + " should not start with " + ROLE_PREFIX + " since " + ROLE_PREFIX
53+
+ " is automatically prepended when using hasRole. Consider using hasAuthority instead.");
54+
}
55+
4756
@Test
4857
public void hasAuthorityWhenNullThenException() {
4958
assertThatIllegalArgumentException().isThrownBy(() -> AuthorityAuthorizationManager.hasAuthority(null))
@@ -76,6 +85,16 @@ public void hasAnyRoleWhenCustomRolePrefixNullThenException() {
7685
.withMessage("rolePrefix cannot be null");
7786
}
7887

88+
@Test
89+
public void hasAnyRoleWhenContainRoleWithRolePrefixThenException() {
90+
String ROLE_PREFIX = "ROLE_";
91+
String ROLE_USER = ROLE_PREFIX + "USER";
92+
assertThatIllegalArgumentException()
93+
.isThrownBy(() -> AuthorityAuthorizationManager.hasAnyRole(new String[] { ROLE_USER }))
94+
.withMessage(ROLE_USER + " should not start with " + ROLE_PREFIX + " since " + ROLE_PREFIX
95+
+ " is automatically prepended when using hasAnyRole. Consider using hasAnyAuthority instead.");
96+
}
97+
7998
@Test
8099
public void hasAnyAuthorityWhenNullThenException() {
81100
assertThatIllegalArgumentException().isThrownBy(() -> AuthorityAuthorizationManager.hasAnyAuthority(null))

0 commit comments

Comments
 (0)