Scrivio Interview library
DesignIntermediate5 min read · updated 12 November 2026

Your application has roles ADMIN, MANAGER and USER where each higher role includes the lower ones' permissions, and teams keep writing hasAnyRole("ADMIN","MANAGER","USER"). How would you model this better?

Instead of listing every higher role in every check, we declare the hierarchy once with a RoleHierarchy bean, for example RoleHierarchyImpl.fromHierarchy("ROLE_ADMIN > ROLE_MANAGER > ROLE_USER"), which request and method security use so that hasRole("USER") also accepts managers and admins. Better still, code checks permissions such as order:approve, and roles are mapped to permissions in one central place, possibly fed by the identity provider. Role definitions can then change without code changes; check how your Spring Security version picks up the hierarchy.

Why this question is asked

Strings like hasAnyRole("ADMIN","MANAGER","USER") look harmless, but they spread through a codebase and quietly encode an organisation chart in hundreds of places. When a new role arrives, someone must find every copy, and one missed copy is either a broken feature or a security hole. Interviewers ask this design question to see whether we can move knowledge about roles to one place. In this answer, we will see why the current approach breaks down, model the hierarchy with RoleHierarchy, move from roles to permissions, keep the mapping central, and check how the configuration is picked up.

1Why hasAnyRole Everywhere Breaks Down

The expression hasAnyRole("ADMIN","MANAGER","USER") says, in effect, that anyone at USER level or above may do this. The knowledge that ADMIN includes MANAGER and MANAGER includes USER is repeated in every annotation. Suppose the business adds a SUPERVISOR role between MANAGER and USER. Every check that should accept supervisors must be edited, and a search for hasAnyRole finds 140 matches, some of which mean something slightly different.

The deeper problem is that the code asks who the user is, when it really wants to know what the user may do. Those are different questions, and mixing them is why role changes require code changes.

2Declaring the Hierarchy Once

A RoleHierarchy tells Spring Security which authorities imply others. When a check runs, the user's authorities are expanded with everything they imply before comparing.

Java
@Bean
static RoleHierarchy roleHierarchy() {
return RoleHierarchyImpl.fromHierarchy("""
ROLE_ADMIN > ROLE_MANAGER
ROLE_MANAGER > ROLE_USER
""");
}
// User with [ROLE_MANAGER] is treated as [ROLE_MANAGER, ROLE_USER]
// hasRole("USER") -> true
// hasRole("MANAGER") -> true
// hasRole("ADMIN") -> false
Effective authorities with the hierarchy
Assigned roleEffective authoritieshasRole("MANAGER")
ROLE_ADMINROLE_ADMIN, ROLE_MANAGER, ROLE_USERtrue
ROLE_MANAGERROLE_MANAGER, ROLE_USERtrue
ROLE_USERROLE_USERfalse
The expansion happens at check time; the stored assignments do not change.

The single line form "ROLE_ADMIN > ROLE_MANAGER > ROLE_USER" works too. Spring Security 6.3 added fromHierarchy() and a builder, RoleHierarchyImpl.withDefaultRolePrefix().role("ADMIN").implies("MANAGER"); older versions used setHierarchy() on a new RoleHierarchyImpl, now deprecated. Our checks shrink to the lowest role that is enough: hasRole("USER") instead of the three-role list.

3How the Hierarchy Is Picked Up

Declaring the bean is not always enough on its own, and this changed across versions, so it is worth checking. In current Spring Security 6.x and 7, a RoleHierarchy bean is used by authorizeHttpRequests rules and by method security's expression handler automatically. In older versions it had to be set on the method security expression handler, or on each AuthorityAuthorizationManager, by hand. Declaring the bean as static avoids it being created too late for method security infrastructure.

The only reliable proof is a test: give a user only ROLE_ADMIN and call an endpoint protected by hasRole("USER"), both a URL-protected one and a method-protected one. If either returns 403, the hierarchy is not wired into that part.

4Better: Check Permissions, Map Roles to Them

A hierarchy fixes the duplication but still checks roles in code. The stronger model is to check permissions, fine-grained authorities that name an action, and to translate roles into permissions in one place.

Java
// In code: ask what the user may do
@PreAuthorize("hasAuthority('order:approve')")
public void approve(long orderId) { ... }
// One place: what each role means
static final Map<String, Set<String>> ROLE_PERMISSIONS = Map.of(
"ROLE_USER", Set.of("order:read", "order:create"),
"ROLE_MANAGER", Set.of("order:read", "order:create", "order:approve"),
"ROLE_ADMIN", Set.of("order:read", "order:create", "order:approve", "user:manage"));
// When authentication completes, add the role's permissions
// alice [ROLE_MANAGER] -> [ROLE_MANAGER, order:read, order:create, order:approve]
// approve(42) as alice -> allowed

The mapping is applied once per login, for example in the UserDetailsService, in a JwtAuthenticationConverter for tokens, or with a RoleHierarchy, since a hierarchy can also say "ROLE_MANAGER > order:approve". Now adding SUPERVISOR means adding one line to the mapping, and no annotation changes. Notice also that code now documents itself: hasAuthority('order:approve') says why the check exists, which hasAnyRole never did.

5Where the Mapping Should Live

Inside one application, the mapping can be a configuration file or a small table that the security team reviews. Across many applications, there are two choices. The identity provider can issue permissions directly in the token, which centralises everything but makes tokens large and couples the identity provider to each application's vocabulary. Or the identity provider issues business roles, and each application maps them to its own permissions in one place it owns. The second choice usually ages better: the identity provider knows people and jobs, the application knows its actions.

Whichever we choose, the rule is one mapping per application, owned, versioned and tested, never scattered through annotations.

6Follow-up questions

Does the hierarchy work with hasAuthority checks too?

Yes. RoleHierarchy works on authority strings in general, so lines like ROLE_MANAGER > order:approve are valid, and hasAuthority checks see the implied authorities as well.

Is a hierarchy always the right model?

Only when roles really nest. Many organisations have parallel roles, such as finance and support, that do not include each other. Forcing them into a hierarchy creates admins with far more access than they need. Permissions mapped per role handle both cases.

How do you migrate 140 hasAnyRole checks safely?

Introduce the permissions and the mapping first, so both styles work at once. Then replace checks one module at a time with tests for each role, and finally add a build rule that rejects new hasAnyRole uses.

7Conclusion

In this answer, we replaced repeated hasAnyRole lists with a RoleHierarchy declared once, which makes higher roles imply lower ones in both request and method security, and we noted that how the bean is picked up depends on your version. We then went further, checking permissions such as order:approve in code and mapping roles to permissions in one owned place, so role definitions can change without touching the code.

What to say in the interviewAbout 80 seconds aloud

Open with

The repeated hasAnyRole lists encode the role ladder in every annotation, so changes need edits everywhere. As a first step, I would declare a RoleHierarchy bean, for example RoleHierarchyImpl.fromHierarchy with ROLE_ADMIN above ROLE_MANAGER above ROLE_USER.

Then, if they want more

Spring Security then expands a user's authorities with the roles they imply, so a check can simply say hasRole USER and managers and admins pass too.

I would verify how the bean is picked up in our version, because older versions needed it set on the method security expression handler manually, and I would write a test where an admin calls endpoints protected by the lowest role through both URL and method security. The better model, though, is to stop checking roles in code at all.

Code should check permissions such as order:approve, and one central mapping translates roles into permissions when the user authenticates, either in the authentication converter or through the hierarchy itself. Then adding a supervisor role or changing what managers can do is a change to the mapping, not to the code.

Across many applications, I would have the identity provider issue business roles and let each application own its own role to permission mapping in one place.

About this answer

Last updated
12 November 2026
Versions it names
Spring Security 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Security reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your application has roles ADMIN, MANAGER and USER where each higher role includes the...": https://scrivio-interview-library.netlify.app/spring-boot/authorization/93e6fd5eb3/