|
@@ -164,8 +164,10 @@ There are two ``@Bean``s that Spring Boot generates on Resource Server's behalf.
|
|
|
The first is a `SecurityFilterChain` that configures the app as a resource server. When including `spring-security-oauth2-jose`, this `SecurityFilterChain` looks like:
|
|
|
|
|
|
.Default JWT Configuration
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -179,7 +181,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -195,15 +198,17 @@ open fun filterChain(http: HttpSecurity): SecurityFilterChain {
|
|
|
return http.build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
If the application doesn't expose a `SecurityFilterChain` bean, then Spring Boot will expose the above default one.
|
|
|
|
|
|
Replacing this is as simple as exposing the bean within the application:
|
|
|
|
|
|
.Custom JWT Configuration
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -226,7 +231,8 @@ public class MyCustomSecurityConfiguration {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -249,7 +255,7 @@ class MyCustomSecurityConfiguration {
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
The above requires the scope of `message:read` for any URL that starts with `/messages/`.
|
|
|
|
|
@@ -259,8 +265,10 @@ Methods on the `oauth2ResourceServer` DSL will also override or replace auto con
|
|
|
For example, the second `@Bean` Spring Boot creates is a `JwtDecoder`, which <<oauth2resourceserver-jwt-architecture-jwtdecoder,decodes `String` tokens into validated instances of `Jwt`>>:
|
|
|
|
|
|
.JWT Decoder
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -269,7 +277,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -277,7 +286,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return JwtDecoders.fromIssuerLocation(issuerUri)
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[NOTE]
|
|
|
Calling `{security-api-url}org/springframework/security/oauth2/jwt/JwtDecoders.html#fromIssuerLocation-java.lang.String-[JwtDecoders#fromIssuerLocation]` is what invokes the Provider Configuration or Authorization Server Metadata endpoint in order to derive the JWK Set Uri.
|
|
@@ -291,8 +300,10 @@ Or, if you're not using Spring Boot at all, then both of these components - the
|
|
|
The filter chain is specified like so:
|
|
|
|
|
|
.Default JWT Configuration
|
|
|
-====
|
|
|
-.Xml
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="primary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -302,13 +313,15 @@ The filter chain is specified like so:
|
|
|
</oauth2-resource-server>
|
|
|
</http>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
And the `JwtDecoder` like so:
|
|
|
|
|
|
.JWT Decoder
|
|
|
-====
|
|
|
-.Xml
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="primary"]
|
|
|
----
|
|
|
<bean id="jwtDecoder"
|
|
@@ -317,7 +330,7 @@ And the `JwtDecoder` like so:
|
|
|
<constructor-arg value="${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}"/>
|
|
|
</bean>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-jwkseturi-dsl]]
|
|
|
=== Using `jwkSetUri()`
|
|
@@ -325,8 +338,10 @@ And the `JwtDecoder` like so:
|
|
|
An authorization server's JWK Set Uri can be configured <<oauth2resourceserver-jwt-jwkseturi,as a configuration property>> or it can be supplied in the DSL:
|
|
|
|
|
|
.JWK Set Uri Configuration
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -348,7 +363,8 @@ public class DirectlyConfiguredJwkSetUri {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -371,7 +387,8 @@ class DirectlyConfiguredJwkSetUri {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -381,7 +398,7 @@ class DirectlyConfiguredJwkSetUri {
|
|
|
</oauth2-resource-server>
|
|
|
</http>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Using `jwkSetUri()` takes precedence over any configuration property.
|
|
|
|
|
@@ -391,8 +408,10 @@ Using `jwkSetUri()` takes precedence over any configuration property.
|
|
|
More powerful than `jwkSetUri()` is `decoder()`, which will completely replace any Boot auto configuration of <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>>:
|
|
|
|
|
|
.JWT Decoder Configuration
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -414,7 +433,8 @@ public class DirectlyConfiguredJwtDecoder {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -437,7 +457,8 @@ class DirectlyConfiguredJwtDecoder {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -447,7 +468,7 @@ class DirectlyConfiguredJwtDecoder {
|
|
|
</oauth2-resource-server>
|
|
|
</http>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
This is handy when deeper configuration, like <<oauth2resourceserver-jwt-validation,validation>>, <<oauth2resourceserver-jwt-claimsetmapping,mapping>>, or <<oauth2resourceserver-jwt-timeouts,request timeouts>>, is necessary.
|
|
|
|
|
@@ -456,8 +477,10 @@ This is handy when deeper configuration, like <<oauth2resourceserver-jwt-validat
|
|
|
|
|
|
Or, exposing a <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>> `@Bean` has the same effect as `decoder()`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -466,7 +489,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -474,7 +498,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return NimbusJwtDecoder.withJwkSetUri(jwkSetUri).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-algorithm]]
|
|
|
== Configuring Trusted Algorithms
|
|
@@ -504,8 +528,10 @@ spring:
|
|
|
|
|
|
For greater power, though, we can use a builder that ships with `NimbusJwtDecoder`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -515,7 +541,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -524,12 +551,14 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
.jwsAlgorithm(RS512).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Calling `jwsAlgorithm` more than once will configure `NimbusJwtDecoder` to trust more than one algorithm, like so:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -539,7 +568,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -548,12 +578,14 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
.jwsAlgorithm(RS512).jwsAlgorithm(ES512).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Or, you can call `jwsAlgorithms`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -566,7 +598,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -578,7 +611,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
}.build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-jwk-response]]
|
|
|
=== From JWK Set response
|
|
@@ -588,8 +621,10 @@ Since Spring Security's JWT support is based off of Nimbus, you can use all it's
|
|
|
For example, Nimbus has a `JWSKeySelector` implementation that will select the set of algorithms based on the JWK Set URI response.
|
|
|
You can use it to generate a `NimbusJwtDecoder` like so:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -606,7 +641,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -618,7 +654,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return NimbusJwtDecoder(jwtProcessor)
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-public-key]]
|
|
|
== Trusting a Single Asymmetric Key
|
|
@@ -644,8 +680,10 @@ spring:
|
|
|
|
|
|
Or, to allow for a more sophisticated lookup, you can post-process the `RsaKeyConversionServicePostProcessor`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -656,7 +694,8 @@ BeanFactoryPostProcessor conversionServiceCustomizer() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -667,7 +706,7 @@ fun conversionServiceCustomizer(): BeanFactoryPostProcessor {
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Specify your key's location:
|
|
|
|
|
@@ -678,29 +717,34 @@ key.location: hfds://my-key.pub
|
|
|
|
|
|
And then autowire the value:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Value("${key.location}")
|
|
|
RSAPublicKey key;
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Value("\${key.location}")
|
|
|
val key: RSAPublicKey? = null
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-public-key-builder]]
|
|
|
=== Using a Builder
|
|
|
|
|
|
To wire an `RSAPublicKey` directly, you can simply use the appropriate `NimbusJwtDecoder` builder, like so:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -709,7 +753,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -717,7 +762,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return NimbusJwtDecoder.withPublicKey(this.key).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-secret-key]]
|
|
|
== Trusting a Single Symmetric Key
|
|
@@ -725,8 +770,10 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
Using a single symmetric key is also simple.
|
|
|
You can simply load in your `SecretKey` and use the appropriate `NimbusJwtDecoder` builder, like so:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -735,7 +782,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -743,7 +791,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return NimbusJwtDecoder.withSecretKey(key).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-authorization]]
|
|
|
== Configuring Authorization
|
|
@@ -757,8 +805,10 @@ When this is the case, Resource Server will attempt to coerce these scopes into
|
|
|
This means that to protect an endpoint or method with a scope derived from a JWT, the corresponding expressions should include this prefix:
|
|
|
|
|
|
.Authorization Configuration
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -778,7 +828,8 @@ public class DirectlyConfiguredJwkSetUri {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -801,7 +852,8 @@ class DirectlyConfiguredJwkSetUri {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -812,25 +864,28 @@ class DirectlyConfiguredJwkSetUri {
|
|
|
</oauth2-resource-server>
|
|
|
</http>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Or similarly with method security:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@PreAuthorize("hasAuthority('SCOPE_messages')")
|
|
|
public List<Message> getMessages(...) {}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@PreAuthorize("hasAuthority('SCOPE_messages')")
|
|
|
fun getMessages(): List<Message> { }
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-authorization-extraction]]
|
|
|
=== Extracting Authorities Manually
|
|
@@ -848,8 +903,10 @@ Let's say that that your authorization server communicates authorities in a cust
|
|
|
In that case, you can configure the claim that <<oauth2resourceserver-jwt-architecture-jwtauthenticationconverter,`JwtAuthenticationConverter`>> should inspect, like so:
|
|
|
|
|
|
.Authorities Claim Configuration
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -863,7 +920,8 @@ public JwtAuthenticationConverter jwtAuthenticationConverter() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -877,7 +935,8 @@ fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -899,14 +958,16 @@ fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
|
|
<property name="authoritiesClaimName" value="authorities"/>
|
|
|
</bean>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
You can also configure the authority prefix to be different as well.
|
|
|
Instead of prefixing each authority with `SCOPE_`, you can change it to `ROLE_` like so:
|
|
|
|
|
|
.Authorities Prefix Configuration
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -920,7 +981,8 @@ public JwtAuthenticationConverter jwtAuthenticationConverter() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -934,7 +996,8 @@ fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -956,14 +1019,16 @@ fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
|
|
<property name="authorityPrefix" value="ROLE_"/>
|
|
|
</bean>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Or, you can remove the prefix altogether by calling `JwtGrantedAuthoritiesConverter#setAuthorityPrefix("")`.
|
|
|
|
|
|
For more flexibility, the DSL supports entirely replacing the converter with any class that implements `Converter<Jwt, AbstractAuthenticationToken>`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
static class CustomAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> {
|
|
@@ -993,7 +1058,8 @@ public class CustomAuthenticationConverterConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
internal class CustomAuthenticationConverter : Converter<Jwt, AbstractAuthenticationToken> {
|
|
@@ -1023,7 +1089,7 @@ class CustomAuthenticationConverterConfig {
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-validation]]
|
|
|
== Configuring Validation
|
|
@@ -1042,8 +1108,10 @@ This can cause some implementation heartburn as the number of collaborating serv
|
|
|
|
|
|
Resource Server uses `JwtTimestampValidator` to verify a token's validity window, and it can be configured with a `clockSkew` to alleviate the above problem:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1061,7 +1129,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1077,7 +1146,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return jwtDecoder
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[NOTE]
|
|
|
By default, Resource Server configures a clock skew of 60 seconds.
|
|
@@ -1087,8 +1156,10 @@ By default, Resource Server configures a clock skew of 60 seconds.
|
|
|
|
|
|
Adding a check for <<_supplying_audiences, the `aud` claim>> is simple with the `OAuth2TokenValidator` API:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
OAuth2TokenValidator<Jwt> audienceValidator() {
|
|
@@ -1096,19 +1167,22 @@ OAuth2TokenValidator<Jwt> audienceValidator() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
fun audienceValidator(): OAuth2TokenValidator<Jwt?> {
|
|
|
return JwtClaimValidator<List<String>>(AUD) { aud -> aud.contains("messaging") }
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Or, for more control you can implement your own `OAuth2TokenValidator`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
static class AudienceValidator implements OAuth2TokenValidator<Jwt> {
|
|
@@ -1131,7 +1205,8 @@ OAuth2TokenValidator<Jwt> audienceValidator() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
internal class AudienceValidator : OAuth2TokenValidator<Jwt> {
|
|
@@ -1152,12 +1227,14 @@ fun audienceValidator(): OAuth2TokenValidator<Jwt> {
|
|
|
return AudienceValidator()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Then, to add into a resource server, it's a matter of specifying the <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>> instance:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1175,7 +1252,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1191,7 +1269,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return jwtDecoder
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[TIP]
|
|
|
As stated earlier, you can instead <<_supplying_audiences, configure `aud` validation in Boot>>.
|
|
@@ -1228,8 +1306,10 @@ By default, `MappedJwtClaimSetConverter` will attempt to coerce claims into the
|
|
|
|
|
|
An individual claim's conversion strategy can be configured using `MappedJwtClaimSetConverter.withDefaults`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1244,7 +1324,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1258,7 +1339,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return jwtDecoder
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
This will keep all the defaults, except it will override the default claim converter for `sub`.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping-add]]
|
|
@@ -1266,46 +1347,54 @@ This will keep all the defaults, except it will override the default claim conve
|
|
|
|
|
|
`MappedJwtClaimSetConverter` can also be used to add a custom claim, for example, to adapt to an existing system:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
MappedJwtClaimSetConverter.withDefaults(Collections.singletonMap("custom", custom -> "value"));
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
MappedJwtClaimSetConverter.withDefaults(mapOf("custom" to Converter<Any, String> { "value" }))
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping-remove]]
|
|
|
=== Removing a Claim
|
|
|
|
|
|
And removing a claim is also simple, using the same API:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
MappedJwtClaimSetConverter.withDefaults(Collections.singletonMap("legacyclaim", legacy -> null));
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
MappedJwtClaimSetConverter.withDefaults(mapOf("legacyclaim" to Converter<Any, Any> { null }))
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping-rename]]
|
|
|
=== Renaming a Claim
|
|
|
|
|
|
In more sophisticated scenarios, like consulting multiple claims at once or renaming a claim, Resource Server accepts any class that implements `Converter<Map<String, Object>, Map<String,Object>>`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
public class UsernameSubClaimAdapter implements Converter<Map<String, Object>, Map<String, Object>> {
|
|
@@ -1323,7 +1412,8 @@ public class UsernameSubClaimAdapter implements Converter<Map<String, Object>, M
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
class UsernameSubClaimAdapter : Converter<Map<String, Any?>, Map<String, Any?>> {
|
|
@@ -1336,12 +1426,14 @@ class UsernameSubClaimAdapter : Converter<Map<String, Any?>, Map<String, Any?>>
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
And then, the instance can be supplied like normal:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1352,7 +1444,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1362,7 +1455,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return jwtDecoder
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-timeouts]]
|
|
|
== Configuring Timeouts
|
|
@@ -1374,8 +1467,10 @@ Further, it doesn't take into account more sophisticated patterns like back-off
|
|
|
|
|
|
To adjust the way in which Resource Server connects to the authorization server, `NimbusJwtDecoder` accepts an instance of `RestOperations`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1390,7 +1485,8 @@ public JwtDecoder jwtDecoder(RestTemplateBuilder builder) {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1402,15 +1498,17 @@ fun jwtDecoder(builder: RestTemplateBuilder): JwtDecoder {
|
|
|
return NimbusJwtDecoder.withJwkSetUri(jwkSetUri).restOperations(rest).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Also by default, Resource Server caches in-memory the authorization server's JWK set for 5 minutes, which you may want to adjust.
|
|
|
Further, it doesn't take into account more sophisticated caching patterns like eviction or using a shared cache.
|
|
|
|
|
|
To adjust the way in which Resource Server caches the JWK set, `NimbusJwtDecoder` accepts an instance of `Cache`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1421,7 +1519,8 @@ public JwtDecoder jwtDecoder(CacheManager cacheManager) {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1431,7 +1530,7 @@ fun jwtDecoder(cacheManager: CacheManager): JwtDecoder {
|
|
|
.build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
When given a `Cache`, Resource Server will use the JWK Set Uri as the key and the JWK Set JSON as the value.
|
|
|
|