Talk about spring security’s role hierarchy.



This article will study the role hierarchy of spring security.


By default, the userDetailsService establishes users whose permissions have no inheritance relationship.

    protected UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        return manager;

For example, these two

    public String admin(){
        return "admin";

    public String user(){
        return "user";

Admin login can only access /admin, not/user; While user login can only access /user

This is usually not in line with our business requirements. Generally admin has all the permissions, that is, it should be able to access /user. The extension of this problem is the inheritance of role permissions, role hierarchy.


spring-security-core-4.2.3.RELEASE-sources.jar! /org/springframework/security/access/hierarchicalroles/

            ROLE_STAFF > ROLE_USER
            ROLE_USER > ROLE_GUEST

Spring security provides RoleHierarchy, which allows you to define the hierarchical relationships of various roles.


        securedEnabled = true,
        jsr250Enabled = true,
        prePostEnabled = true
public class RoleConfig extends GlobalMethodSecurityConfiguration{

    protected AccessDecisionManager accessDecisionManager() {
        List<AccessDecisionVoter<? extends Object>> decisionVoters = new ArrayList<AccessDecisionVoter<? extends Object>>();
        ExpressionBasedPreInvocationAdvice expressionAdvice = new ExpressionBasedPreInvocationAdvice();
//        if (prePostEnabled()) {
                    .add(new PreInvocationAuthorizationAdviceVoter(expressionAdvice));
//        }
//        if (jsr250Enabled()) {
            decisionVoters.add(new Jsr250Voter());
//        }
//        decisionVoters.add(new RoleVoter());
        decisionVoters.add(new AuthenticatedVoter());

        return new AffirmativeBased(decisionVoters);

    public RoleHierarchyVoter roleHierarchyVoter() {
        return new RoleHierarchyVoter(roleHierarchy());

    public RoleHierarchy roleHierarchy(){
        RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
                "ROLE_ADMIN > ROLE_USER\n"+
                        " ROLE_USER > ROLE_ANONYMOUS\n"
        return roleHierarchy;

Here, roleHierarchyVoter is added to decisionVoters by rewriting the accessDecisionManager method of globalmethodsecurity configuration.
The default is to use RoleVoter, which does not support inheritance relations, and is replaced by roleHierarchyVoter here.
This completes the task, and admin can also access the user permission page/interface.


spring-security-core-4.2.3.RELEASE-sources.jar! /org/springframework/security/access/vote/

 * Extended RoleVoter which uses a {@link RoleHierarchy} definition to determine the roles
 * allocated to the current user before voting.
 * @author Luke Taylor
 * @since 2.0.4
public class RoleHierarchyVoter extends RoleVoter {
    private RoleHierarchy roleHierarchy = null;

    public RoleHierarchyVoter(RoleHierarchy roleHierarchy) {
        Assert.notNull(roleHierarchy, "RoleHierarchy must not be null");
        this.roleHierarchy = roleHierarchy;

     * Calls the <tt>RoleHierarchy</tt> to obtain the complete set of user authorities.
    Collection<? extends GrantedAuthority> extractAuthorities(
            Authentication authentication) {
        return roleHierarchy.getReachableGrantedAuthorities(authentication

This class inherits RoleVoter, rewrites extractAuthorities, and uses roleHierarchy to obtain grantedAuthorities.

Construction of Inheritance Relationship

spring-security-core-4.2.3.RELEASE-sources.jar! /org/springframework/security/access/hierarchicalroles/

     * Set the role hierarchy and pre-calculate for every role the set of all reachable
     * roles, i.e. all roles lower in the hierarchy of every given role. Pre-calculation
     * is done for performance reasons (reachable roles can then be calculated in O(1)
     * time). During pre-calculation, cycles in role hierarchy are detected and will cause
     * a <tt>CycleInRoleHierarchyException</tt> to be thrown.
     * @param roleHierarchyStringRepresentation - String definition of the role hierarchy.
    public void setHierarchy(String roleHierarchyStringRepresentation) {
        this.roleHierarchyStringRepresentation = roleHierarchyStringRepresentation;

        logger.debug("setHierarchy() - The following role hierarchy was set: "
                + roleHierarchyStringRepresentation);


After the hierarchy is set up, the mapping is constructed by two methods: BuildRoleESREACHABLEINONESEPMAP and BuildRoleESREACHABLEINONEEORESEPMAP


     * rolesReachableInOneStepMap is a Map that under the key of a specific role name
     * contains a set of all roles reachable from this role in 1 step
    private Map<GrantedAuthority, Set<GrantedAuthority>> rolesReachableInOneStepMap = null;

     * Parse input and build the map for the roles reachable in one step: the higher role
     * will become a key that references a set of the reachable lower roles.
    private void buildRolesReachableInOneStepMap() {
        Pattern pattern = Pattern.compile("(\\s*([^\\s>]+)\\s*>\\s*([^\\s>]+))");

        Matcher roleHierarchyMatcher = pattern
        this.rolesReachableInOneStepMap = new HashMap<GrantedAuthority, Set<GrantedAuthority>>();

        while (roleHierarchyMatcher.find()) {
            GrantedAuthority higherRole = new SimpleGrantedAuthority(
            GrantedAuthority lowerRole = new SimpleGrantedAuthority(
            Set<GrantedAuthority> rolesReachableInOneStepSet;

            if (!this.rolesReachableInOneStepMap.containsKey(higherRole)) {
                rolesReachableInOneStepSet = new HashSet<GrantedAuthority>();
            else {
                rolesReachableInOneStepSet = this.rolesReachableInOneStepMap
            addReachableRoles(rolesReachableInOneStepSet, lowerRole);

            logger.debug("buildRolesReachableInOneStepMap() - From role " + higherRole
                    + " one can reach role " + lowerRole + " in one step.");

    private void addReachableRoles(Set<GrantedAuthority> reachableRoles,
            GrantedAuthority authority) {

        for (GrantedAuthority testAuthority : reachableRoles) {
            String testKey = testAuthority.getAuthority();
            if ((testKey != null) && (testKey.equals(authority.getAuthority()))) {

Map < Granted Authority, Set < Granted Authority > > ROLESREACHABLEINNONEST EPMAP, where the first-level direct connection is stored in this MAP

Suppose the cascade relation is

A > B
B > C
C > D
D > E
D > F

So this map is similar to

A --> [B]
B --> [C]
C --> [D]
D --> [E,F]


     * rolesReachableInOneOrMoreStepsMap is a Map that under the key of a specific role
     * name contains a set of all roles reachable from this role in 1 or more steps
    private Map<GrantedAuthority, Set<GrantedAuthority>> rolesReachableInOneOrMoreStepsMap = null;

     * For every higher role from rolesReachableInOneStepMap store all roles that are
     * reachable from it in the map of roles reachable in one or more steps. (Or throw a
     * CycleInRoleHierarchyException if a cycle in the role hierarchy definition is
     * detected)
    private void buildRolesReachableInOneOrMoreStepsMap() {
        this.rolesReachableInOneOrMoreStepsMap = new HashMap<GrantedAuthority, Set<GrantedAuthority>>();
        // iterate over all higher roles from rolesReachableInOneStepMap

        for (GrantedAuthority role : this.rolesReachableInOneStepMap.keySet()) {
            Set<GrantedAuthority> rolesToVisitSet = new HashSet<GrantedAuthority>();

            if (this.rolesReachableInOneStepMap.containsKey(role)) {

            Set<GrantedAuthority> visitedRolesSet = new HashSet<GrantedAuthority>();

            while (!rolesToVisitSet.isEmpty()) {
                // take a role from the rolesToVisit set
                GrantedAuthority aRole = rolesToVisitSet.iterator().next();
                addReachableRoles(visitedRolesSet, aRole);
                if (this.rolesReachableInOneStepMap.containsKey(aRole)) {
                    Set<GrantedAuthority> newReachableRoles = this.rolesReachableInOneStepMap

                    // definition of a cycle: you can reach the role you are starting from
                    if (rolesToVisitSet.contains(role)
                            || visitedRolesSet.contains(role)) {
                        throw new CycleInRoleHierarchyException();
                    else {
                        // no cycle
            this.rolesReachableInOneOrMoreStepsMap.put(role, visitedRolesSet);

            logger.debug("buildRolesReachableInOneOrMoreStepsMap() - From role " + role
                    + " one can reach " + visitedRolesSet + " in one or more steps.");


MAP < Granted Authority, Set < Granted Authority > > ROLESREACHABLEINONEORMORESTEPSMAP-This evens out the indirect hierarchical relationship
In this case, recursion is actually used to complete the mapping of all cascading relationships in the hierarchy. rolestovisiteset is continuously remove and conditionally added. the recursion termination condition is that rolestovisiteset is empty

The first level map is as follows

A --> [B]
B --> [C]
C --> [D]
D --> [E,F]

After construction, the following

A --> [B,C,D,E,F]
B --> [C,D,E,F]
C --> [D,E,F]
D --> [E,F]


spring-security-core-4.2.3.RELEASE-sources.jar! /org/springframework/security/access/hierarchicalroles/

public Collection<GrantedAuthority> getReachableGrantedAuthorities(
            Collection<? extends GrantedAuthority> authorities) {
        if (authorities == null || authorities.isEmpty()) {
            return AuthorityUtils.NO_AUTHORITIES;

        Set<GrantedAuthority> reachableRoles = new HashSet<GrantedAuthority>();

        for (GrantedAuthority authority : authorities) {
            addReachableRoles(reachableRoles, authority);
            Set<GrantedAuthority> additionalReachableRoles = getRolesReachableInOneOrMoreSteps(
            if (additionalReachableRoles != null) {

        if (logger.isDebugEnabled()) {
            logger.debug("getReachableGrantedAuthorities() - From the roles "
                    + authorities + " one can reach " + reachableRoles
                    + " in zero or more steps.");

        List<GrantedAuthority> reachableRoleList = new ArrayList<GrantedAuthority>(

        return reachableRoleList;
    // SEC-863
    private Set<GrantedAuthority> getRolesReachableInOneOrMoreSteps(
            GrantedAuthority authority) {

        if (authority.getAuthority() == null) {
            return null;

        for (GrantedAuthority testAuthority : this.rolesReachableInOneOrMoreStepsMap
                .keySet()) {
            String testKey = testAuthority.getAuthority();
            if ((testKey != null) && (testKey.equals(authority.getAuthority()))) {
                return this.rolesReachableInOneOrMoreStepsMap.get(testAuthority);

        return null;

The getReachableGrantedAuthorities method obtains all cascading hierarchical relationships through the previously constructed RolesearchableInorneoreStepsMap.

This is the end of the story.