Quick revision
The short answer
We set spring.security.oauth2.resourceserver.jwt.issuer-uri to https://keycloak.example.com/realms/shop, and Spring discovers the realm's keys from its OpenID configuration. Keycloak puts realm roles in realm_access.roles, which the default converter does not read because it only looks at scopes, so we give a JwtAuthenticationConverter a custom Converter<Jwt, Collection<GrantedAuthority>> that reads those roles and adds the ROLE_ prefix, and we turn on @EnableMethodSecurity so @PreAuthorize("hasRole('ADMIN')") is checked.
What to remember
In this answer, we configured a Spring Boot resource server for the Keycloak realm shop with issuer-uri, letting Spring discover the keys. Keycloak keeps realm roles in realm_access.roles and client roles in resource_access, while Spring's default converter only reads scopes, so hasRole('ADMIN') failed. A custom converter that adds the ROLE_ prefix, plugged into JwtAuthenticationConverter, together with @EnableMethodSecurity, makes the check work, and the same converter can add one client's roles. We used Spring Security's standard support because the Keycloak adapters are gone.
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.
We set spring.security.oauth2.resourceserver.jwt.issuer-uri to https://keycloak.example.com/realms/shop, and Spring discovers the realm's keys from its OpenID configuration. Keycloak puts realm roles in realm_access.roles, which the default converter does not read because it only looks at scopes, so we give a JwtAuthenticationConverter a custom Converter<Jwt, Collection<GrantedAuthority>> that reads those roles and adds the ROLE_ prefix, and we turn on @EnableMethodSecurity so @PreAuthorize("hasRole('ADMIN')") is checked.
Why this question is asked
Keycloak is one of the most common identity providers in Spring shops, and nearly every team hits the same surprise: the token clearly contains the ADMIN role, yet hasRole('ADMIN') denies access. Interviewers ask this to see whether we know where Keycloak puts roles, how Spring turns claims into authorities, and how to plug in our own mapping cleanly. In this answer, we will configure the resource server, look at a Keycloak token, see why the default mapping misses the roles, write the converter, enable method security, and extend it to client roles.
1Configuring the Resource Server
A resource server only needs to know who issues its tokens. Each Keycloak realm is its own issuer, with the realm name in the path.
At startup, Spring reads https://keycloak.example.com/realms/shop/.well-known/openid-configuration, finds the jwks_uri there, and uses it to download the realm's public keys. It also checks that every token's iss equals this value exactly. The dependency is spring-boot-starter-oauth2-resource-server. We do not need any Keycloak library: the old Keycloak Spring Boot and Spring Security adapters were deprecated years ago and have since been removed, and Spring Security's standard OAuth2 support replaces them.
2Where Keycloak Puts Roles
Here is the relevant part of a decoded access token from the shop realm.
Realm roles, defined for the whole realm, sit in realm_access.roles. Client roles, defined for one client, sit in resource_access.<client-id>.roles. Neither is a standard claim; they are Keycloak's own layout.
3Why hasRole('ADMIN') Fails by Default
Spring's default JwtAuthenticationConverter uses a JwtGrantedAuthoritiesConverter that reads only the scope or scp claim and turns each value into an authority with the SCOPE_ prefix. For the token above, the authentication gets SCOPE_openid, SCOPE_profile and SCOPE_email, and nothing else.
hasRole('ADMIN') checks for an authority named ROLE_ADMIN, because hasRole adds the ROLE_ prefix for us. No such authority exists, so access is denied with 403, even though the token says the user is an admin.
| Converter | Authorities produced | hasRole('ADMIN') |
|---|---|---|
| Default (scope claim) | SCOPE_openid, SCOPE_profile, SCOPE_email | denied (403) |
| Custom realm roles | ROLE_ADMIN, ROLE_offline_access, ROLE_default-roles-shop | allowed |
| Custom realm + client | the above + ROLE_orders-export | allowed |
4Writing the Converter
We write a small converter that reads realm_access.roles and prefixes each role with ROLE_, and give it to a JwtAuthenticationConverter.
Notice that role names are case-sensitive. Keycloak roles are often lower case, such as admin, and hasRole('ADMIN') would not match ROLE_admin. We either name the roles in Keycloak exactly as we check them or upper-case them in the converter, and we pick one convention for all services.
Without @EnableMethodSecurity, the @PreAuthorize annotation is silently ignored, so the endpoint would be open to any authenticated user. That is a dangerous failure, so it is worth a test that calls the endpoint with a token lacking the role and expects 403.
5Follow-up questions
How would you also accept client roles for one specific client?
We read resource_access, take the entry for our client id, for example shop-api, read its roles list and map those to authorities as well, either with the same ROLE_ prefix or a distinct one such as ROLE_shop-api_ to avoid clashes with realm roles. We combine them with the realm roles in the same converter. Only our own client's roles should be read, because roles granted for other clients mean nothing to this API.
What if I still want the SCOPE_ authorities too?
We create the default JwtGrantedAuthoritiesConverter as well and, in our converter, return the union of its result and the roles. Then rules like hasAuthority('SCOPE_orders.read') and hasRole('ADMIN') both work.
Why read realm_access instead of adding a roles claim with a Keycloak mapper?
Both work. A protocol mapper in Keycloak can put roles into a flat claim such as roles, which simplifies the converter and makes tokens match an organisation-wide claims contract. Reading realm_access needs no Keycloak changes, which is convenient when we do not own the realm.
6Conclusion
In this answer, we configured a Spring Boot resource server for the Keycloak realm shop with issuer-uri, letting Spring discover the keys. Keycloak keeps realm roles in realm_access.roles and client roles in resource_access, while Spring's default converter only reads scopes, so hasRole('ADMIN') failed. A custom converter that adds the ROLE_ prefix, plugged into JwtAuthenticationConverter, together with @EnableMethodSecurity, makes the check work, and the same converter can add one client's roles. We used Spring Security's standard support because the Keycloak adapters are gone.
Open with
I would set spring.security.oauth2.resourceserver.jwt.issuer-uri to https://keycloak.example.com/realms/shop. Spring reads the realm's OpenID configuration, gets the JWKS URI from it, downloads the keys and validates the issuer on every token.
Then, if they want more
I would not use the Keycloak adapters; they were deprecated and removed, and Spring Security's standard resource server support is the replacement. The catch is roles. Keycloak puts realm roles under realm_access.roles and client roles under resource_access, then the client id, then roles.
Spring's default converter only reads the scope claim and creates SCOPE_ authorities, and hasRole('ADMIN') looks for ROLE_ADMIN, so access is denied. So I write a Converter from Jwt to a collection of GrantedAuthority that reads realm_access.roles and maps each role to a SimpleGrantedAuthority with the ROLE_ prefix, set it on a JwtAuthenticationConverter, and register that in oauth2ResourceServer jwt.
I might also set the principal claim to preferred_username. Then I add @EnableMethodSecurity, because without it @PreAuthorize is ignored, and I write a test that a token without the role gets 403. I watch for case, since Keycloak roles are often lower case. For client roles I read only our own client's entry in resource_access.
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
- RFC 6749, The OAuth 2.0 Authorization Framework, Spring Security reference, OAuth2
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 "Configure a Spring Boot 3 API as a resource server for tokens issued by a Keycloak realm...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/a14d460eb9/