Sub topic 9
From claims to GrantedAuthorities
This chapter assumes you have a working JWT resource server (see the setup chapters) and know what a claim is. It covers how Spring Security turns claims in a validated token into the authorities that your authorization rules check.
What the default does
After the JwtDecoder validates a token, Spring Security must build an Authentication object. It needs a list of GrantedAuthority values, because every rule such as hasAuthority(...) compares against that list.
By default, JwtGrantedAuthoritiesConverter reads the scope claim (or scp if scope is missing). It splits the value on spaces and prefixes each item with SCOPE_. A token with "scope": "orders.read orders.write" becomes two authorities: SCOPE_orders.read and SCOPE_orders.write.
That is why the earlier rules used hasAuthority("SCOPE_admin").
Reading a different claim
Many authorization servers put permissions in a custom claim such as roles, not in scope. To read it, configure the granted-authorities converter and wrap it in a JwtAuthenticationConverter.
@Bean
JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter authorities = new JwtGrantedAuthoritiesConverter();
authorities.setAuthoritiesClaimName("roles");
authorities.setAuthorityPrefix("ROLE_");
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
converter.setJwtGrantedAuthoritiesConverter(authorities);
return converter;
}
// In the filter chain:
// .oauth2ResourceServer(oauth2 -> oauth2.jwt(jwt -> jwt.jwtAuthenticationConverter(jwtAuthenticationConverter())))With this bean, "roles": ["admin", "auditor"] yields ROLE_admin and ROLE_auditor. The converter accepts either a space-separated string or a collection of strings.
You can now write hasRole("admin"). Spring adds the ROLE_ prefix for you when it evaluates hasRole. Use hasAuthority("ROLE_admin") if you prefer to be explicit.
Prefix choices
| Setting | Claim value | Resulting authority |
|---|---|---|
| Default | `scope: "read write"` | `SCOPE_read`, `SCOPE_write` |
| Prefix `ROLE_`, claim `roles` | `roles: ["admin"]` | `ROLE_admin` |
| Prefix `""`, claim `roles` | `roles: ["admin"]` | `admin` |
Combining scopes and roles
setAuthoritiesClaimName reads only one claim. If your tokens carry both scopes and roles, supply your own converter function. It can combine the output of the default converter with extra authorities.
@Bean
JwtAuthenticationConverter combinedConverter() {
JwtGrantedAuthoritiesConverter scopes = new JwtGrantedAuthoritiesConverter();
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
converter.setJwtGrantedAuthoritiesConverter(jwt -> {
Collection<GrantedAuthority> result = new ArrayList<>(scopes.convert(jwt));
List<String> roles = jwt.getClaimAsStringList("roles");
if (roles != null) {
roles.forEach(r -> result.add(new SimpleGrantedAuthority("ROLE_" + r)));
}
return result;
});
return converter;
}The converter must be registered in the filter chain as shown above. Declaring the bean alone does not always apply it, so wire it explicitly.
Nested claims
Some providers nest roles, for example Keycloak's realm_access.roles. The built-in converter cannot read nested paths. Use a custom lambda like the one above and read the nested map with jwt.getClaimAsMap("realm_access"). Check for null at each level, because a token without that claim must not cause a server error.
Common errors
- Authorities do not match: you changed the prefix but the rules still say
SCOPE_.... Printauthentication.getAuthorities()to see the real values. hasRolefails: the authority lacks theROLE_prefix. Set the prefix or usehasAuthority.- Converter ignored: it was declared as a bean but not passed to
oauth2.jwt(...). - Trusting any claim: only map claims from tokens that passed signature, issuer, and audience checks. Mapping happens after validation, never instead of it.
Exercise
A token contains "scope": "orders.read" and "roles": ["manager"]. You use the default converter, and your rule is hasRole("manager"). Will the request be allowed? Why, and what is the smallest change that fixes it?
Show answer
It will be denied with 403. The default converter reads only scope, so the only authority is SCOPE_orders.read. There is no ROLE_manager. Set setAuthoritiesClaimName("roles") and setAuthorityPrefix("ROLE_") on the converter and register it with jwtAuthenticationConverter(...). If you also need the scope authority, combine both sources in a custom converter.