Quick revision
The short answer
In Spring Security both roles and authorities are GrantedAuthority strings; a role is simply an authority whose name starts with the ROLE_ prefix by convention. hasRole("ADMIN") therefore looks for the exact string ROLE_ADMIN, while hasAuthority("admin") looks for exactly admin. A JWT that grants "admin" does not contain ROLE_ADMIN, and the default JWT converter only maps the scope or scp claim into SCOPE_ authorities anyway, so we must map the claim with a JwtAuthenticationConverter (or Boot's properties) or use hasAuthority.
What to remember
In this answer, we saw that roles and authorities are both GrantedAuthority strings, a role being an authority with the ROLE_ prefix. hasRole("ADMIN") checks for ROLE_ADMIN while hasAuthority("admin") checks for exactly admin. The default JWT converter only maps scope or scp into SCOPE_ authorities, so a roles claim with admin reaches neither form. We fixed it with a JwtAuthenticationConverter that reads the claim and adds the prefix, mentioned Boot's properties for the simple case, and changed the prefix with GrantedAuthorityDefaults.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In Spring Security both roles and authorities are GrantedAuthority strings; a role is simply an authority whose name starts with the ROLE_ prefix by convention. hasRole("ADMIN") therefore looks for the exact string ROLE_ADMIN, while hasAuthority("admin") looks for exactly admin. A JWT that grants "admin" does not contain ROLE_ADMIN, and the default JWT converter only maps the scope or scp claim into SCOPE_ authorities anyway, so we must map the claim with a JwtAuthenticationConverter (or Boot's properties) or use hasAuthority.
Why this question is asked
This question catches a large share of developers who move from session-based applications to JWT resource servers. Everything looks right: the token clearly says the user is an admin, the rule clearly asks for an admin, and yet every request gets a 403. Interviewers ask it because the explanation shows whether we understand what Spring Security actually compares. In this answer, we will recap GrantedAuthority, see exactly what hasRole and hasAuthority check, follow a JWT through the default converter, fix the mapping, and see how to change the prefix.
1A Quick Recap: Everything Is a GrantedAuthority
After authentication, Spring Security holds an Authentication object for the user, and it carries a collection of GrantedAuthority objects. A GrantedAuthority is little more than a string returned by getAuthority(). Spring Security does not have a separate concept of a role in its data model. A role is a naming convention: an authority whose string starts with ROLE_, such as ROLE_ADMIN.
The convention exists so that code can tell coarse job functions (roles) from fine permissions (authorities such as invoice:approve or SCOPE_orders.read) while storing both the same way. The helper methods are where the convention is applied.
2What hasRole and hasAuthority Compare
hasRole("ADMIN") adds the prefix and then compares exact strings. hasAuthority("admin") compares the string we give, unchanged. Comparisons are case-sensitive.
| Check | Authority it looks for | Matches token value admin? |
|---|---|---|
hasRole("ADMIN") | ROLE_ADMIN | No |
hasAuthority("admin") | admin | Yes |
hasAuthority("SCOPE_admin") | SCOPE_admin | Only if admin is a scope |
hasRole("ADMIN") after mapping | ROLE_ADMIN | Yes, once roles claim maps to ROLE_ |
Notice that hasRole("ADMIN") and hasAuthority("ROLE_ADMIN") are the same check. The difference between a role and an authority is only in who adds the prefix.
3What the Default JWT Converter Produces
With spring-boot-starter-oauth2-resource-server, every incoming JWT is turned into a JwtAuthenticationToken by a JwtAuthenticationConverter. Its default JwtGrantedAuthoritiesConverter reads only the scope claim, or scp if scope is absent, splits it on spaces, and prefixes each value with SCOPE_. Other claims, such as roles or groups, are ignored.
So in a typical setup hasRole("ADMIN") fails for two reasons at once: the role never reaches the authorities, and even if it did as plain admin, the ROLE_ prefix would be missing. Logging the authorities of the current Authentication in a debug endpoint or a test settles the question in seconds.
4Fixing It: Map the Claim Deliberately
We tell the converter which claim holds roles and which prefix to add. If the roles also need different case, we normalise them in the same place.
Recent Spring Boot versions also offer properties for the simple case, spring.security.oauth2.resourceserver.jwt.authorities-claim-name and authority-prefix; check your version. Note that once we replace the authorities converter, the SCOPE_ authorities are no longer produced unless we combine both converters, so if the API also checks scopes we merge the two lists. The alternative fix is to leave the mapping alone and write hasAuthority("admin") after mapping roles without a prefix, which is fine as long as the whole team uses one style consistently.
5Changing the Prefix With GrantedAuthorityDefaults
Some organisations do not use ROLE_ at all. Rather than fighting the convention in every expression, we change the prefix that hasRole adds by declaring a GrantedAuthorityDefaults bean.
The bean is static so it is available early, before method security infrastructure is created. Request authorization and method security both pick it up. Using an empty prefix makes hasRole and hasAuthority behave the same, which some teams find simpler; others keep the prefix so roles and fine permissions stay visibly different.
6Follow-up questions
Should I check roles or authorities in my code?
Prefer fine-grained authorities such as invoice:approve for checks in code, and map roles to those authorities in one place. Code that checks permissions does not need to change when the organisation reshapes its roles.
Keycloak tokens put roles under realm_access.roles. Does setAuthoritiesClaimName work?
Not for a nested claim, since it reads a top-level claim name. Either add a protocol mapper in Keycloak that emits a flat roles claim, or write a small converter that reads the realm_access map and its roles list and creates ROLE_ authorities.
What happens with hasAnyRole?
It applies the same prefix to each name, so hasAnyRole("ADMIN", "SUPPORT") looks for ROLE_ADMIN or ROLE_SUPPORT. The same mapping rules apply.
7Conclusion
In this answer, we saw that roles and authorities are both GrantedAuthority strings, a role being an authority with the ROLE_ prefix. hasRole("ADMIN") checks for ROLE_ADMIN while hasAuthority("admin") checks for exactly admin. The default JWT converter only maps scope or scp into SCOPE_ authorities, so a roles claim with admin reaches neither form. We fixed it with a JwtAuthenticationConverter that reads the claim and adds the prefix, mentioned Boot's properties for the simple case, and changed the prefix with GrantedAuthorityDefaults.
Open with
In Spring Security there is no separate role type. Both roles and authorities are GrantedAuthority strings, and a role is just an authority that starts with ROLE_ by convention. hasRole("ADMIN") adds that prefix and checks for the exact string ROLE_ADMIN, while hasAuthority("admin") checks the exact string admin, case-sensitive.
Then, if they want more
With a JWT, two things go wrong. First, the default JwtGrantedAuthoritiesConverter only reads the scope or scp claim and turns each scope into a SCOPE_ authority, so a roles claim is ignored entirely. Second, even if admin were mapped, it would not be ROLE_ADMIN.
To fix it, I configure a JwtAuthenticationConverter with a JwtGrantedAuthoritiesConverter that reads the roles claim and uses the ROLE_ prefix, normalising case if needed, and I merge in the scope authorities if the API still checks scopes. Recent Spring Boot versions have resource server properties for the claim name and prefix too.
Alternatively I could use hasAuthority with whatever string the mapping produces, as long as the team is consistent. And if the organisation does not want the ROLE_ prefix at all, a static GrantedAuthorityDefaults bean changes what hasRole adds.
About this answer
- Last updated
- 12 November 2026
- 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 "What is the difference between a role and an authority in Spring Security? Why does...": https://scrivio-interview-library.netlify.app/spring-boot/authorization/35a56bae2a/