mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-24 03:03:44 +00:00
Add JwtAuthenticationConverter docs
Issue gh-8185
This commit is contained in:
parent
a70d55552b
commit
13b2b00093
@ -699,9 +699,11 @@ However, there are a number of circumstances where this default is insufficient.
|
|||||||
For example, some authorization servers don't use the `scope` attribute, but instead have their own custom attribute.
|
For example, some authorization servers don't use the `scope` attribute, but instead have their own custom attribute.
|
||||||
Or, at other times, the resource server may need to adapt the attribute or a composition of attributes into internalized authorities.
|
Or, at other times, the resource server may need to adapt the attribute or a composition of attributes into internalized authorities.
|
||||||
|
|
||||||
To this end, the DSL exposes `jwtAuthenticationConverter()`, which is responsible for <<oauth2resourceserver-jwt-architecture-jwtauthenticationconverter,converting a `Jwt` into an `Authentication`>>.
|
To this end, Spring Security ships with `JwtAuthenticationConverter`, which is responsible for <<oauth2resourceserver-jwt-architecture-jwtauthenticationconverter,converting a `Jwt` into an `Authentication`>>.
|
||||||
|
By default, Spring Security will wire the `JwtAuthenticationProvider` with a default instance of `JwtAuthenticationConverter`.
|
||||||
|
|
||||||
|
As part of configuring a `JwtAuthenticationConverter`, you can supply a subsidiary converter to go from `Jwt` to a `Collection` of granted authorities.
|
||||||
|
|
||||||
As part of its configuration, we can supply a subsidiary converter to go from `Jwt` to a `Collection` of granted authorities.
|
|
||||||
Let's say that that your authorization server communicates authorities in a custom claim called `authorities`.
|
Let's say that that your authorization server communicates authorities in a custom claim called `authorities`.
|
||||||
In that case, you can configure the claim that <<oauth2resourceserver-jwt-architecture-jwtauthenticationconverter,`JwtAuthenticationConverter`>> should inspect, like so:
|
In that case, you can configure the claim that <<oauth2resourceserver-jwt-architecture-jwtauthenticationconverter,`JwtAuthenticationConverter`>> should inspect, like so:
|
||||||
|
|
||||||
@ -710,22 +712,8 @@ In that case, you can configure the claim that <<oauth2resourceserver-jwt-archit
|
|||||||
.Java
|
.Java
|
||||||
[source,java,role="primary"]
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
@EnableWebSecurity
|
@Bean
|
||||||
public class CustomAuthoritiesClaimName extends WebSecurityConfigurerAdapter {
|
public JwtAuthenticationConverter jwtAuthenticationConverter() {
|
||||||
protected void configure(HttpSecurity http) {
|
|
||||||
http
|
|
||||||
.authorizeRequests(authorize -> authorize
|
|
||||||
.anyRequest().authenticated()
|
|
||||||
)
|
|
||||||
.oauth2ResourceServer(oauth2 -> oauth2
|
|
||||||
.jwt(jwt -> jwt
|
|
||||||
.jwtAuthenticationConverter(jwtAuthenticationConverter())
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
JwtAuthenticationConverter jwtAuthenticationConverter() {
|
|
||||||
JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||||
grantedAuthoritiesConverter.setAuthoritiesClaimName("authorities");
|
grantedAuthoritiesConverter.setAuthoritiesClaimName("authorities");
|
||||||
|
|
||||||
@ -767,7 +755,8 @@ Instead of prefixing each authority with `SCOPE_`, you can change it to `ROLE_`
|
|||||||
.Java
|
.Java
|
||||||
[source,java,role="primary"]
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
JwtAuthenticationConverter jwtAuthenticationConverter() {
|
@Bean
|
||||||
|
public JwtAuthenticationConverter jwtAuthenticationConverter() {
|
||||||
JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||||
grantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
grantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
||||||
|
|
||||||
@ -812,6 +801,23 @@ static class CustomAuthenticationConverter implements Converter<Jwt, AbstractAut
|
|||||||
return new CustomAuthenticationToken(jwt);
|
return new CustomAuthenticationToken(jwt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
@EnableWebSecurity
|
||||||
|
public class CustomAuthenticationConverterConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
protected void configure(HttpSecurity http) {
|
||||||
|
http
|
||||||
|
.authorizeRequests(authorize -> authorize
|
||||||
|
.anyRequest().authenticated()
|
||||||
|
)
|
||||||
|
.oauth2ResourceServer(oauth2 -> oauth2
|
||||||
|
.jwt(jwt -> jwt
|
||||||
|
.jwtAuthenticationConverter(new CustomAuthenticationConverter())
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
[[oauth2resourceserver-jwt-validation]]
|
[[oauth2resourceserver-jwt-validation]]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user