Skip to content

Commit 13b2b00

Browse files
committed
Add JwtAuthenticationConverter docs
Issue gh-8185
1 parent a70d555 commit 13b2b00

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/oauth2/oauth2-resourceserver.adoc

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,11 @@ However, there are a number of circumstances where this default is insufficient.
699699
For example, some authorization servers don't use the `scope` attribute, but instead have their own custom attribute.
700700
Or, at other times, the resource server may need to adapt the attribute or a composition of attributes into internalized authorities.
701701

702-
To this end, the DSL exposes `jwtAuthenticationConverter()`, which is responsible for <<oauth2resourceserver-jwt-architecture-jwtauthenticationconverter,converting a `Jwt` into an `Authentication`>>.
702+
To this end, Spring Security ships with `JwtAuthenticationConverter`, which is responsible for <<oauth2resourceserver-jwt-architecture-jwtauthenticationconverter,converting a `Jwt` into an `Authentication`>>.
703+
By default, Spring Security will wire the `JwtAuthenticationProvider` with a default instance of `JwtAuthenticationConverter`.
704+
705+
As part of configuring a `JwtAuthenticationConverter`, you can supply a subsidiary converter to go from `Jwt` to a `Collection` of granted authorities.
703706

704-
As part of its configuration, we can supply a subsidiary converter to go from `Jwt` to a `Collection` of granted authorities.
705707
Let's say that that your authorization server communicates authorities in a custom claim called `authorities`.
706708
In that case, you can configure the claim that <<oauth2resourceserver-jwt-architecture-jwtauthenticationconverter,`JwtAuthenticationConverter`>> should inspect, like so:
707709

@@ -710,22 +712,8 @@ In that case, you can configure the claim that <<oauth2resourceserver-jwt-archit
710712
.Java
711713
[source,java,role="primary"]
712714
----
713-
@EnableWebSecurity
714-
public class CustomAuthoritiesClaimName extends WebSecurityConfigurerAdapter {
715-
protected void configure(HttpSecurity http) {
716-
http
717-
.authorizeRequests(authorize -> authorize
718-
.anyRequest().authenticated()
719-
)
720-
.oauth2ResourceServer(oauth2 -> oauth2
721-
.jwt(jwt -> jwt
722-
.jwtAuthenticationConverter(jwtAuthenticationConverter())
723-
)
724-
);
725-
}
726-
}
727-
728-
JwtAuthenticationConverter jwtAuthenticationConverter() {
715+
@Bean
716+
public JwtAuthenticationConverter jwtAuthenticationConverter() {
729717
JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
730718
grantedAuthoritiesConverter.setAuthoritiesClaimName("authorities");
731719
@@ -767,7 +755,8 @@ Instead of prefixing each authority with `SCOPE_`, you can change it to `ROLE_`
767755
.Java
768756
[source,java,role="primary"]
769757
----
770-
JwtAuthenticationConverter jwtAuthenticationConverter() {
758+
@Bean
759+
public JwtAuthenticationConverter jwtAuthenticationConverter() {
771760
JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
772761
grantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
773762
@@ -812,6 +801,23 @@ static class CustomAuthenticationConverter implements Converter<Jwt, AbstractAut
812801
return new CustomAuthenticationToken(jwt);
813802
}
814803
}
804+
805+
// ...
806+
807+
@EnableWebSecurity
808+
public class CustomAuthenticationConverterConfig extends WebSecurityConfigurerAdapter {
809+
protected void configure(HttpSecurity http) {
810+
http
811+
.authorizeRequests(authorize -> authorize
812+
.anyRequest().authenticated()
813+
)
814+
.oauth2ResourceServer(oauth2 -> oauth2
815+
.jwt(jwt -> jwt
816+
.jwtAuthenticationConverter(new CustomAuthenticationConverter())
817+
)
818+
);
819+
}
820+
}
815821
----
816822

817823
[[oauth2resourceserver-jwt-validation]]

0 commit comments

Comments
 (0)