|
@@ -140,8 +140,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
|
|
@@ -155,7 +157,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -171,15 +174,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"]
|
|
|
----
|
|
|
@EnableWebSecurity
|
|
@@ -201,7 +206,8 @@ public class MyCustomSecurityConfiguration {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@EnableWebSecurity
|
|
@@ -223,7 +229,7 @@ class MyCustomSecurityConfiguration {
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
The above requires the scope of `message:read` for any URL that starts with `/messages/`.
|
|
|
|
|
@@ -233,8 +239,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
|
|
@@ -243,7 +251,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -251,7 +260,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.
|
|
@@ -265,8 +274,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>
|
|
@@ -276,13 +287,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"
|
|
@@ -291,7 +304,7 @@ And the `JwtDecoder` like so:
|
|
|
<constructor-arg value="${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}"/>
|
|
|
</bean>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-jwkseturi-dsl]]
|
|
|
=== Using `jwkSetUri()`
|
|
@@ -299,8 +312,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"]
|
|
|
----
|
|
|
@EnableWebSecurity
|
|
@@ -321,7 +336,8 @@ public class DirectlyConfiguredJwkSetUri {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@EnableWebSecurity
|
|
@@ -343,7 +359,8 @@ class DirectlyConfiguredJwkSetUri {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -353,7 +370,7 @@ class DirectlyConfiguredJwkSetUri {
|
|
|
</oauth2-resource-server>
|
|
|
</http>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Using `jwkSetUri()` takes precedence over any configuration property.
|
|
|
|
|
@@ -363,8 +380,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"]
|
|
|
----
|
|
|
@EnableWebSecurity
|
|
@@ -385,7 +404,8 @@ public class DirectlyConfiguredJwtDecoder {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@EnableWebSecurity
|
|
@@ -407,7 +427,8 @@ class DirectlyConfiguredJwtDecoder {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -417,7 +438,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.
|
|
|
|
|
@@ -426,8 +447,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
|
|
@@ -436,7 +459,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -444,7 +468,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return NimbusJwtDecoder.withJwkSetUri(jwkSetUri).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-algorithm]]
|
|
|
== Configuring Trusted Algorithms
|
|
@@ -474,8 +498,10 @@ spring:
|
|
|
|
|
|
For greater power, though, we can use a builder that ships with `NimbusJwtDecoder`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -485,7 +511,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -494,12 +521,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
|
|
@@ -509,7 +538,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -518,12 +548,14 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
.jwsAlgorithm(RS512).jwsAlgorithm(ES512).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Or, you can call `jwsAlgorithms`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -536,7 +568,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -548,7 +581,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
}.build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-jwk-response]]
|
|
|
=== From JWK Set response
|
|
@@ -558,8 +591,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
|
|
@@ -576,7 +611,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -588,7 +624,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return NimbusJwtDecoder(jwtProcessor)
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-public-key]]
|
|
|
== Trusting a Single Asymmetric Key
|
|
@@ -614,8 +650,10 @@ spring:
|
|
|
|
|
|
Or, to allow for a more sophisticated lookup, you can post-process the `RsaKeyConversionServicePostProcessor`:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -626,7 +664,8 @@ BeanFactoryPostProcessor conversionServiceCustomizer() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -637,7 +676,7 @@ fun conversionServiceCustomizer(): BeanFactoryPostProcessor {
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
Specify your key's location:
|
|
|
|
|
@@ -648,29 +687,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
|
|
@@ -679,7 +723,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -687,7 +732,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return NimbusJwtDecoder.withPublicKey(this.key).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-secret-key]]
|
|
|
== Trusting a Single Symmetric Key
|
|
@@ -695,8 +740,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
|
|
@@ -705,7 +752,8 @@ public JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -713,7 +761,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return NimbusJwtDecoder.withSecretKey(key).build()
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-authorization]]
|
|
|
== Configuring Authorization
|
|
@@ -727,8 +775,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"]
|
|
|
----
|
|
|
@EnableWebSecurity
|
|
@@ -747,7 +797,8 @@ public class DirectlyConfiguredJwkSetUri {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@EnableWebSecurity
|
|
@@ -769,7 +820,8 @@ class DirectlyConfiguredJwkSetUri {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -780,25 +832,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
|
|
@@ -816,8 +871,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
|
|
@@ -831,7 +888,8 @@ public JwtAuthenticationConverter jwtAuthenticationConverter() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -845,7 +903,8 @@ fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -867,14 +926,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
|
|
@@ -888,7 +949,8 @@ public JwtAuthenticationConverter jwtAuthenticationConverter() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -902,7 +964,8 @@ fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Xml
|
|
|
+Xml::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -924,14 +987,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> {
|
|
@@ -960,7 +1025,8 @@ public class CustomAuthenticationConverterConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
internal class CustomAuthenticationConverter : Converter<Jwt, AbstractAuthenticationToken> {
|
|
@@ -989,7 +1055,7 @@ class CustomAuthenticationConverterConfig {
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-validation]]
|
|
|
== Configuring Validation
|
|
@@ -1008,8 +1074,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
|
|
@@ -1027,7 +1095,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1043,7 +1112,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return jwtDecoder
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[NOTE]
|
|
|
By default, Resource Server configures a clock skew of 60 seconds.
|
|
@@ -1053,8 +1122,10 @@ By default, Resource Server configures a clock skew of 60 seconds.
|
|
|
|
|
|
Adding a check for the `aud` claim is simple with the `OAuth2TokenValidator` API:
|
|
|
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
OAuth2TokenValidator<Jwt> audienceValidator() {
|
|
@@ -1062,19 +1133,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> {
|
|
@@ -1097,7 +1171,8 @@ OAuth2TokenValidator<Jwt> audienceValidator() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
internal class AudienceValidator : OAuth2TokenValidator<Jwt> {
|
|
@@ -1118,12 +1193,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
|
|
@@ -1141,7 +1218,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1157,7 +1235,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return jwtDecoder
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping]]
|
|
|
== Configuring Claim Set Mapping
|
|
@@ -1191,8 +1269,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
|
|
@@ -1207,7 +1287,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1221,7 +1302,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]]
|
|
@@ -1229,46 +1310,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>> {
|
|
@@ -1286,7 +1375,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?>> {
|
|
@@ -1299,12 +1389,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
|
|
@@ -1315,7 +1407,8 @@ JwtDecoder jwtDecoder() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1325,7 +1418,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
|
return jwtDecoder
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[oauth2resourceserver-jwt-timeouts]]
|
|
|
== Configuring Timeouts
|
|
@@ -1337,8 +1430,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
|
|
@@ -1353,7 +1448,8 @@ public JwtDecoder jwtDecoder(RestTemplateBuilder builder) {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1365,15 +1461,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
|
|
@@ -1384,7 +1482,8 @@ public JwtDecoder jwtDecoder(CacheManager cacheManager) {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
@@ -1394,7 +1493,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.
|
|
|
|