mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-05 18:22:26 +00:00
Support whitespace characters using RoleHierarchyImpl
This commit is contained in:
parent
4bfce2a591
commit
6adbe8dae0
@ -15,6 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.security.access.hierarchicalroles;
|
package org.springframework.security.access.hierarchicalroles;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -22,12 +25,9 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.AuthorityUtils;
|
import org.springframework.security.core.authority.AuthorityUtils;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
@ -176,34 +176,33 @@ public class RoleHierarchyImpl implements RoleHierarchy {
|
|||||||
* will become a key that references a set of the reachable lower roles.
|
* will become a key that references a set of the reachable lower roles.
|
||||||
*/
|
*/
|
||||||
private void buildRolesReachableInOneStepMap() {
|
private void buildRolesReachableInOneStepMap() {
|
||||||
Pattern pattern = Pattern.compile("(\\s*([^\\s>]+)\\s*>\\s*([^\\s>]+))");
|
|
||||||
|
|
||||||
Matcher roleHierarchyMatcher = pattern
|
|
||||||
.matcher(this.roleHierarchyStringRepresentation);
|
|
||||||
this.rolesReachableInOneStepMap = new HashMap<GrantedAuthority, Set<GrantedAuthority>>();
|
this.rolesReachableInOneStepMap = new HashMap<GrantedAuthority, Set<GrantedAuthority>>();
|
||||||
|
try (BufferedReader bufferedReader = new BufferedReader(
|
||||||
while (roleHierarchyMatcher.find()) {
|
new StringReader(this.roleHierarchyStringRepresentation))) {
|
||||||
|
for (String readLine; (readLine = bufferedReader.readLine()) != null;) {
|
||||||
|
String[] roles = readLine.split(" > ");
|
||||||
|
for (int i = 1; i < roles.length; i++) {
|
||||||
GrantedAuthority higherRole = new SimpleGrantedAuthority(
|
GrantedAuthority higherRole = new SimpleGrantedAuthority(
|
||||||
roleHierarchyMatcher.group(2));
|
roles[i - 1].replaceAll("^\\s+|\\s+$", ""));
|
||||||
GrantedAuthority lowerRole = new SimpleGrantedAuthority(
|
GrantedAuthority lowerRole = new SimpleGrantedAuthority(roles[i].replaceAll("^\\s+|\\s+$", ""));
|
||||||
roleHierarchyMatcher.group(3));
|
|
||||||
Set<GrantedAuthority> rolesReachableInOneStepSet;
|
Set<GrantedAuthority> rolesReachableInOneStepSet;
|
||||||
|
|
||||||
if (!this.rolesReachableInOneStepMap.containsKey(higherRole)) {
|
if (!this.rolesReachableInOneStepMap.containsKey(higherRole)) {
|
||||||
rolesReachableInOneStepSet = new HashSet<>();
|
rolesReachableInOneStepSet = new HashSet<GrantedAuthority>();
|
||||||
this.rolesReachableInOneStepMap.put(higherRole,
|
this.rolesReachableInOneStepMap.put(higherRole, rolesReachableInOneStepSet);
|
||||||
rolesReachableInOneStepSet);
|
} else {
|
||||||
}
|
rolesReachableInOneStepSet = this.rolesReachableInOneStepMap.get(higherRole);
|
||||||
else {
|
|
||||||
rolesReachableInOneStepSet = this.rolesReachableInOneStepMap
|
|
||||||
.get(higherRole);
|
|
||||||
}
|
}
|
||||||
addReachableRoles(rolesReachableInOneStepSet, lowerRole);
|
addReachableRoles(rolesReachableInOneStepSet, lowerRole);
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("buildRolesReachableInOneStepMap() - From role " + higherRole
|
logger.debug("buildRolesReachableInOneStepMap() - From role " + higherRole
|
||||||
+ " one can reach role " + lowerRole + " in one step.");
|
+ " one can reach role " + lowerRole + " in one step.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For every higher role from rolesReachableInOneStepMap store all roles that are
|
* For every higher role from rolesReachableInOneStepMap store all roles that are
|
||||||
|
@ -210,4 +210,27 @@ public class RoleHierarchyImplTests {
|
|||||||
roleHierarchyImpl.getReachableGrantedAuthorities(authorities2),
|
roleHierarchyImpl.getReachableGrantedAuthorities(authorities2),
|
||||||
authorities2)).isTrue();
|
authorities2)).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWhitespaceRoleHierarchies() {
|
||||||
|
List<GrantedAuthority> authorities1 = AuthorityUtils.createAuthorityList(
|
||||||
|
"ROLE A");
|
||||||
|
List<GrantedAuthority> authorities2 = AuthorityUtils.createAuthorityList("ROLE A",
|
||||||
|
"ROLE B", "ROLE>C");
|
||||||
|
List<GrantedAuthority> authorities3 = AuthorityUtils.createAuthorityList("ROLE A",
|
||||||
|
"ROLE B", "ROLE>C", "ROLE D");
|
||||||
|
|
||||||
|
RoleHierarchyImpl roleHierarchyImpl = new RoleHierarchyImpl();
|
||||||
|
|
||||||
|
roleHierarchyImpl.setHierarchy("ROLE A > ROLE B\nROLE B > ROLE>C");
|
||||||
|
assertThat(HierarchicalRolesTestHelper.containTheSameGrantedAuthorities(
|
||||||
|
roleHierarchyImpl.getReachableGrantedAuthorities(authorities1),
|
||||||
|
authorities2)).isTrue();
|
||||||
|
|
||||||
|
roleHierarchyImpl.setHierarchy(
|
||||||
|
"ROLE A > ROLE B\nROLE B > ROLE>C\nROLE>C > ROLE D");
|
||||||
|
assertThat(HierarchicalRolesTestHelper.containTheSameGrantedAuthorities(
|
||||||
|
roleHierarchyImpl.getReachableGrantedAuthorities(authorities1),
|
||||||
|
authorities3)).isTrue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user