RoleHierarchy is a Spring Security interface that defines which roles imply other roles. If ADMIN implies MANAGER and MANAGER implies USER, a user holding only ADMIN passes checks that require MANAGER or USER. It removes the need to assign every lower role to higher-privileged users.
The default implementation is RoleHierarchyImpl. You build it once as a bean, and Spring Security uses it when it evaluates authorization rules and method security.
Without a hierarchy, authorities are flat. A user with ROLE_ADMIN fails a check for hasRole("USER") unless ROLE_USER was also granted. RoleHierarchy computes the set of reachable authorities from the ones the user has, so authorization decisions see the expanded set.
The interface has one core method, getReachableGrantedAuthorities(Collection<? extends GrantedAuthority>), which returns the original authorities plus everything they imply. You rarely call it directly.
Since Spring Security 6.3, RoleHierarchyImpl has a fluent builder, and the old string format with newlines is deprecated. A hierarchy is a bean:
withDefaultRolePrefix() adds the ROLE_ prefix for you, so you write ADMIN and not ROLE_ADMIN. On earlier versions use the string form:
Check the version you run before choosing between the two APIs.
Declaring the bean is not always enough. In recent Spring Security versions, the AuthorizationManager used by authorizeHttpRequests and by method security picks up a RoleHierarchy bean automatically. If you build your own method security expression handler, set it explicitly:
Declare that bean as static so the method security infrastructure can be created early. Then the rules below work with the hierarchy.
An ADMIN user can open /reports/** and call monthlyReport(), while a plain USER cannot.
Test the hierarchy itself with a unit test, so a typo in the configuration is caught without starting the application.
hasRole or hasAuthority checks. It does not rewrite the stored authorities of the user.report:read, and map roles to permissions.@PostAuthorize, PermissionEvaluator or query filters.No. It only expands the authorities at decision time, so the stored data stays minimal.
Yes. Add several implies entries, or chain multiple roles with the builder, and the reachable set is the union.
Yes, because the check uses the expanded authority set. Remember to include the ROLE_ prefix if you call hasAuthority("ROLE_USER").
Usually the bean is not wired into a custom expression handler or voter. Confirm that your security configuration uses the RoleHierarchy bean.
RoleHierarchy lets higher roles inherit the access of lower roles, so you assign one role per user instead of several. Define a RoleHierarchyImpl bean, use the builder on Spring Security 6.3 or later, and verify with a small unit test. Keep it for coarse role levels and use permissions or object-level checks for the rest.