|
@@ -1,5 +1,5 @@
|
|
[[oauth2resourceserver]]
|
|
[[oauth2resourceserver]]
|
|
-== OAuth 2.0 Resource Server
|
|
|
|
|
|
+= OAuth 2.0 Resource Server
|
|
:figures: images/servlet/oauth2
|
|
:figures: images/servlet/oauth2
|
|
:icondir: images/icons
|
|
:icondir: images/icons
|
|
|
|
|
|
@@ -59,18 +59,18 @@ image:{icondir}/number_4.png[] If authentication is successful, then __Success__
|
|
* The `BearerTokenAuthenticationFilter` invokes `FilterChain.doFilter(request,response)` to continue with the rest of the application logic.
|
|
* The `BearerTokenAuthenticationFilter` invokes `FilterChain.doFilter(request,response)` to continue with the rest of the application logic.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-minimaldependencies]]
|
|
[[oauth2resourceserver-jwt-minimaldependencies]]
|
|
-=== Minimal Dependencies for JWT
|
|
|
|
|
|
+== Minimal Dependencies for JWT
|
|
|
|
|
|
Most Resource Server support is collected into `spring-security-oauth2-resource-server`.
|
|
Most Resource Server support is collected into `spring-security-oauth2-resource-server`.
|
|
However, the support for decoding and verifying JWTs is in `spring-security-oauth2-jose`, meaning that both are necessary in order to have a working resource server that supports JWT-encoded Bearer Tokens.
|
|
However, the support for decoding and verifying JWTs is in `spring-security-oauth2-jose`, meaning that both are necessary in order to have a working resource server that supports JWT-encoded Bearer Tokens.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-minimalconfiguration]]
|
|
[[oauth2resourceserver-jwt-minimalconfiguration]]
|
|
-=== Minimal Configuration for JWTs
|
|
|
|
|
|
+== Minimal Configuration for JWTs
|
|
|
|
|
|
When using https://spring.io/projects/spring-boot[Spring Boot], configuring an application as a resource server consists of two basic steps.
|
|
When using https://spring.io/projects/spring-boot[Spring Boot], configuring an application as a resource server consists of two basic steps.
|
|
First, include the needed dependencies and second, indicate the location of the authorization server.
|
|
First, include the needed dependencies and second, indicate the location of the authorization server.
|
|
|
|
|
|
-==== Specifying the Authorization Server
|
|
|
|
|
|
+=== Specifying the Authorization Server
|
|
|
|
|
|
In a Spring Boot application, to specify which authorization server to use, simply do:
|
|
In a Spring Boot application, to specify which authorization server to use, simply do:
|
|
|
|
|
|
@@ -93,7 +93,7 @@ This endpoint is referred to as a https://openid.net/specs/openid-connect-discov
|
|
|
|
|
|
And that's it!
|
|
And that's it!
|
|
|
|
|
|
-==== Startup Expectations
|
|
|
|
|
|
+=== Startup Expectations
|
|
|
|
|
|
When this property and these dependencies are used, Resource Server will automatically configure itself to validate JWT-encoded Bearer Tokens.
|
|
When this property and these dependencies are used, Resource Server will automatically configure itself to validate JWT-encoded Bearer Tokens.
|
|
|
|
|
|
@@ -109,7 +109,7 @@ A consequence of this process is that the authorization server must be up and re
|
|
[NOTE]
|
|
[NOTE]
|
|
If the authorization server is down when Resource Server queries it (given appropriate timeouts), then startup will fail.
|
|
If the authorization server is down when Resource Server queries it (given appropriate timeouts), then startup will fail.
|
|
|
|
|
|
-==== Runtime Expectations
|
|
|
|
|
|
+=== Runtime Expectations
|
|
|
|
|
|
Once the application is started up, Resource Server will attempt to process any request containing an `Authorization: Bearer` header:
|
|
Once the application is started up, Resource Server will attempt to process any request containing an `Authorization: Bearer` header:
|
|
|
|
|
|
@@ -139,7 +139,7 @@ From here, consider jumping to:
|
|
* <<oauth2resourceserver-jwt-sansboot,How to Configure without Spring Boot>>
|
|
* <<oauth2resourceserver-jwt-sansboot,How to Configure without Spring Boot>>
|
|
|
|
|
|
[[oauth2resourceserver-jwt-architecture]]
|
|
[[oauth2resourceserver-jwt-architecture]]
|
|
-=== How JWT Authentication Works
|
|
|
|
|
|
+== How JWT Authentication Works
|
|
|
|
|
|
Next, let's see the architectural components that Spring Security uses to support https://tools.ietf.org/html/rfc7519[JWT] Authentication in servlet-based applications, like the one we just saw.
|
|
Next, let's see the architectural components that Spring Security uses to support https://tools.ietf.org/html/rfc7519[JWT] Authentication in servlet-based applications, like the one we just saw.
|
|
|
|
|
|
@@ -165,7 +165,7 @@ image:{icondir}/number_5.png[] When authentication is successful, the <<servlet-
|
|
Ultimately, the returned `JwtAuthenticationToken` will be set on the <<servlet-authentication-securitycontextholder,`SecurityContextHolder`>> by the authentication `Filter`.
|
|
Ultimately, the returned `JwtAuthenticationToken` will be set on the <<servlet-authentication-securitycontextholder,`SecurityContextHolder`>> by the authentication `Filter`.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-jwkseturi]]
|
|
[[oauth2resourceserver-jwt-jwkseturi]]
|
|
-=== Specifying the Authorization Server JWK Set Uri Directly
|
|
|
|
|
|
+== Specifying the Authorization Server JWK Set Uri Directly
|
|
|
|
|
|
If the authorization server doesn't support any configuration endpoints, or if Resource Server must be able to start up independently from the authorization server, then the `jwk-set-uri` can be supplied as well:
|
|
If the authorization server doesn't support any configuration endpoints, or if Resource Server must be able to start up independently from the authorization server, then the `jwk-set-uri` can be supplied as well:
|
|
|
|
|
|
@@ -190,7 +190,7 @@ We still specify the `issuer-uri` so that Resource Server still validates the `i
|
|
This property can also be supplied directly on the <<oauth2resourceserver-jwt-jwkseturi-dsl,DSL>>.
|
|
This property can also be supplied directly on the <<oauth2resourceserver-jwt-jwkseturi-dsl,DSL>>.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-sansboot]]
|
|
[[oauth2resourceserver-jwt-sansboot]]
|
|
-=== Overriding or Replacing Boot Auto Configuration
|
|
|
|
|
|
+== Overriding or Replacing Boot Auto Configuration
|
|
|
|
|
|
There are two ``@Bean``s that Spring Boot generates on Resource Server's behalf.
|
|
There are two ``@Bean``s that Spring Boot generates on Resource Server's behalf.
|
|
|
|
|
|
@@ -343,7 +343,7 @@ And the `JwtDecoder` like so:
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-jwkseturi-dsl]]
|
|
[[oauth2resourceserver-jwt-jwkseturi-dsl]]
|
|
-==== Using `jwkSetUri()`
|
|
|
|
|
|
+=== Using `jwkSetUri()`
|
|
|
|
|
|
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:
|
|
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:
|
|
|
|
|
|
@@ -403,7 +403,7 @@ class DirectlyConfiguredJwkSetUri : WebSecurityConfigurerAdapter() {
|
|
Using `jwkSetUri()` takes precedence over any configuration property.
|
|
Using `jwkSetUri()` takes precedence over any configuration property.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-dsl]]
|
|
[[oauth2resourceserver-jwt-decoder-dsl]]
|
|
-==== Using `decoder()`
|
|
|
|
|
|
+=== Using `decoder()`
|
|
|
|
|
|
More powerful than `jwkSetUri()` is `decoder()`, which will completely replace any Boot auto configuration of <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>>:
|
|
More powerful than `jwkSetUri()` is `decoder()`, which will completely replace any Boot auto configuration of <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>>:
|
|
|
|
|
|
@@ -463,7 +463,7 @@ class DirectlyConfiguredJwtDecoder : WebSecurityConfigurerAdapter() {
|
|
This is handy when deeper configuration, like <<oauth2resourceserver-jwt-validation,validation>>, <<oauth2resourceserver-jwt-claimsetmapping,mapping>>, or <<oauth2resourceserver-jwt-timeouts,request timeouts>>, is necessary.
|
|
This is handy when deeper configuration, like <<oauth2resourceserver-jwt-validation,validation>>, <<oauth2resourceserver-jwt-claimsetmapping,mapping>>, or <<oauth2resourceserver-jwt-timeouts,request timeouts>>, is necessary.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-bean]]
|
|
[[oauth2resourceserver-jwt-decoder-bean]]
|
|
-==== Exposing a `JwtDecoder` `@Bean`
|
|
|
|
|
|
+=== Exposing a `JwtDecoder` `@Bean`
|
|
|
|
|
|
Or, exposing a <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>> `@Bean` has the same effect as `decoder()`:
|
|
Or, exposing a <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>> `@Bean` has the same effect as `decoder()`:
|
|
|
|
|
|
@@ -488,14 +488,14 @@ fun jwtDecoder(): JwtDecoder {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-algorithm]]
|
|
[[oauth2resourceserver-jwt-decoder-algorithm]]
|
|
-=== Configuring Trusted Algorithms
|
|
|
|
|
|
+== Configuring Trusted Algorithms
|
|
|
|
|
|
By default, `NimbusJwtDecoder`, and hence Resource Server, will only trust and verify tokens using `RS256`.
|
|
By default, `NimbusJwtDecoder`, and hence Resource Server, will only trust and verify tokens using `RS256`.
|
|
|
|
|
|
You can customize this via <<oauth2resourceserver-jwt-boot-algorithm,Spring Boot>>, <<oauth2resourceserver-jwt-decoder-builder,the NimbusJwtDecoder builder>>, or from the <<oauth2resourceserver-jwt-decoder-jwk-response,JWK Set response>>.
|
|
You can customize this via <<oauth2resourceserver-jwt-boot-algorithm,Spring Boot>>, <<oauth2resourceserver-jwt-decoder-builder,the NimbusJwtDecoder builder>>, or from the <<oauth2resourceserver-jwt-decoder-jwk-response,JWK Set response>>.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-boot-algorithm]]
|
|
[[oauth2resourceserver-jwt-boot-algorithm]]
|
|
-==== Via Spring Boot
|
|
|
|
|
|
+=== Via Spring Boot
|
|
|
|
|
|
The simplest way to set the algorithm is as a property:
|
|
The simplest way to set the algorithm is as a property:
|
|
|
|
|
|
@@ -511,7 +511,7 @@ spring:
|
|
----
|
|
----
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-builder]]
|
|
[[oauth2resourceserver-jwt-decoder-builder]]
|
|
-==== Using a Builder
|
|
|
|
|
|
+=== Using a Builder
|
|
|
|
|
|
For greater power, though, we can use a builder that ships with `NimbusJwtDecoder`:
|
|
For greater power, though, we can use a builder that ships with `NimbusJwtDecoder`:
|
|
|
|
|
|
@@ -592,7 +592,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-jwk-response]]
|
|
[[oauth2resourceserver-jwt-decoder-jwk-response]]
|
|
-==== From JWK Set response
|
|
|
|
|
|
+=== From JWK Set response
|
|
|
|
|
|
Since Spring Security's JWT support is based off of Nimbus, you can use all it's great features as well.
|
|
Since Spring Security's JWT support is based off of Nimbus, you can use all it's great features as well.
|
|
|
|
|
|
@@ -632,13 +632,13 @@ fun jwtDecoder(): JwtDecoder {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-public-key]]
|
|
[[oauth2resourceserver-jwt-decoder-public-key]]
|
|
-=== Trusting a Single Asymmetric Key
|
|
|
|
|
|
+== Trusting a Single Asymmetric Key
|
|
|
|
|
|
Simpler than backing a Resource Server with a JWK Set endpoint is to hard-code an RSA public key.
|
|
Simpler than backing a Resource Server with a JWK Set endpoint is to hard-code an RSA public key.
|
|
The public key can be provided via <<oauth2resourceserver-jwt-decoder-public-key-boot,Spring Boot>> or by <<oauth2resourceserver-jwt-decoder-public-key-builder,Using a Builder>>.
|
|
The public key can be provided via <<oauth2resourceserver-jwt-decoder-public-key-boot,Spring Boot>> or by <<oauth2resourceserver-jwt-decoder-public-key-builder,Using a Builder>>.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-public-key-boot]]
|
|
[[oauth2resourceserver-jwt-decoder-public-key-boot]]
|
|
-==== Via Spring Boot
|
|
|
|
|
|
+=== Via Spring Boot
|
|
|
|
|
|
Specifying a key via Spring Boot is quite simple.
|
|
Specifying a key via Spring Boot is quite simple.
|
|
The key's location can be specified like so:
|
|
The key's location can be specified like so:
|
|
@@ -705,7 +705,7 @@ val key: RSAPublicKey? = null
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-public-key-builder]]
|
|
[[oauth2resourceserver-jwt-decoder-public-key-builder]]
|
|
-==== Using a Builder
|
|
|
|
|
|
+=== Using a Builder
|
|
|
|
|
|
To wire an `RSAPublicKey` directly, you can simply use the appropriate `NimbusJwtDecoder` builder, like so:
|
|
To wire an `RSAPublicKey` directly, you can simply use the appropriate `NimbusJwtDecoder` builder, like so:
|
|
|
|
|
|
@@ -730,7 +730,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-decoder-secret-key]]
|
|
[[oauth2resourceserver-jwt-decoder-secret-key]]
|
|
-=== Trusting a Single Symmetric Key
|
|
|
|
|
|
+== Trusting a Single Symmetric Key
|
|
|
|
|
|
Using a single symmetric key is also simple.
|
|
Using a single symmetric key is also simple.
|
|
You can simply load in your `SecretKey` and use the appropriate `NimbusJwtDecoder` builder, like so:
|
|
You can simply load in your `SecretKey` and use the appropriate `NimbusJwtDecoder` builder, like so:
|
|
@@ -756,7 +756,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-authorization]]
|
|
[[oauth2resourceserver-jwt-authorization]]
|
|
-=== Configuring Authorization
|
|
|
|
|
|
+== Configuring Authorization
|
|
|
|
|
|
A JWT that is issued from an OAuth 2.0 Authorization Server will typically either have a `scope` or `scp` attribute, indicating the scopes (or authorities) it's been granted, for example:
|
|
A JWT that is issued from an OAuth 2.0 Authorization Server will typically either have a `scope` or `scp` attribute, indicating the scopes (or authorities) it's been granted, for example:
|
|
|
|
|
|
@@ -837,7 +837,7 @@ fun getMessages(): List<Message> { }
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-authorization-extraction]]
|
|
[[oauth2resourceserver-jwt-authorization-extraction]]
|
|
-==== Extracting Authorities Manually
|
|
|
|
|
|
+=== Extracting Authorities Manually
|
|
|
|
|
|
However, there are a number of circumstances where this default is insufficient.
|
|
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.
|
|
@@ -1024,14 +1024,14 @@ class CustomAuthenticationConverterConfig : WebSecurityConfigurerAdapter() {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-validation]]
|
|
[[oauth2resourceserver-jwt-validation]]
|
|
-=== Configuring Validation
|
|
|
|
|
|
+== Configuring Validation
|
|
|
|
|
|
Using <<oauth2resourceserver-jwt-minimalconfiguration,minimal Spring Boot configuration>>, indicating the authorization server's issuer uri, Resource Server will default to verifying the `iss` claim as well as the `exp` and `nbf` timestamp claims.
|
|
Using <<oauth2resourceserver-jwt-minimalconfiguration,minimal Spring Boot configuration>>, indicating the authorization server's issuer uri, Resource Server will default to verifying the `iss` claim as well as the `exp` and `nbf` timestamp claims.
|
|
|
|
|
|
In circumstances where validation needs to be customized, Resource Server ships with two standard validators and also accepts custom `OAuth2TokenValidator` instances.
|
|
In circumstances where validation needs to be customized, Resource Server ships with two standard validators and also accepts custom `OAuth2TokenValidator` instances.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-validation-clockskew]]
|
|
[[oauth2resourceserver-jwt-validation-clockskew]]
|
|
-==== Customizing Timestamp Validation
|
|
|
|
|
|
+=== Customizing Timestamp Validation
|
|
|
|
|
|
JWT's typically have a window of validity, with the start of the window indicated in the `nbf` claim and the end indicated in the `exp` claim.
|
|
JWT's typically have a window of validity, with the start of the window indicated in the `nbf` claim and the end indicated in the `exp` claim.
|
|
|
|
|
|
@@ -1081,7 +1081,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
By default, Resource Server configures a clock skew of 60 seconds.
|
|
By default, Resource Server configures a clock skew of 60 seconds.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-validation-custom]]
|
|
[[oauth2resourceserver-jwt-validation-custom]]
|
|
-==== Configuring a Custom Validator
|
|
|
|
|
|
+=== Configuring a Custom Validator
|
|
|
|
|
|
Adding a check for the `aud` claim is simple with the `OAuth2TokenValidator` API:
|
|
Adding a check for the `aud` claim is simple with the `OAuth2TokenValidator` API:
|
|
|
|
|
|
@@ -1192,7 +1192,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping]]
|
|
[[oauth2resourceserver-jwt-claimsetmapping]]
|
|
-=== Configuring Claim Set Mapping
|
|
|
|
|
|
+== Configuring Claim Set Mapping
|
|
|
|
|
|
Spring Security uses the https://bitbucket.org/connect2id/nimbus-jose-jwt/wiki/Home[Nimbus] library for parsing JWTs and validating their signatures.
|
|
Spring Security uses the https://bitbucket.org/connect2id/nimbus-jose-jwt/wiki/Home[Nimbus] library for parsing JWTs and validating their signatures.
|
|
Consequently, Spring Security is subject to Nimbus's interpretation of each field value and how to coerce each into a Java type.
|
|
Consequently, Spring Security is subject to Nimbus's interpretation of each field value and how to coerce each into a Java type.
|
|
@@ -1206,7 +1206,7 @@ Or, quite simply, a resource server may want to add or remove claims from a JWT
|
|
For these purposes, Resource Server supports mapping the JWT claim set with `MappedJwtClaimSetConverter`.
|
|
For these purposes, Resource Server supports mapping the JWT claim set with `MappedJwtClaimSetConverter`.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping-singleclaim]]
|
|
[[oauth2resourceserver-jwt-claimsetmapping-singleclaim]]
|
|
-==== Customizing the Conversion of a Single Claim
|
|
|
|
|
|
+=== Customizing the Conversion of a Single Claim
|
|
|
|
|
|
By default, `MappedJwtClaimSetConverter` will attempt to coerce claims into the following types:
|
|
By default, `MappedJwtClaimSetConverter` will attempt to coerce claims into the following types:
|
|
|
|
|
|
@@ -1257,7 +1257,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
This will keep all the defaults, except it will override the default claim converter for `sub`.
|
|
This will keep all the defaults, except it will override the default claim converter for `sub`.
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping-add]]
|
|
[[oauth2resourceserver-jwt-claimsetmapping-add]]
|
|
-==== Adding a Claim
|
|
|
|
|
|
+=== Adding a Claim
|
|
|
|
|
|
`MappedJwtClaimSetConverter` can also be used to add a custom claim, for example, to adapt to an existing system:
|
|
`MappedJwtClaimSetConverter` can also be used to add a custom claim, for example, to adapt to an existing system:
|
|
|
|
|
|
@@ -1276,7 +1276,7 @@ MappedJwtClaimSetConverter.withDefaults(mapOf("custom" to Converter<Any, String>
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping-remove]]
|
|
[[oauth2resourceserver-jwt-claimsetmapping-remove]]
|
|
-==== Removing a Claim
|
|
|
|
|
|
+=== Removing a Claim
|
|
|
|
|
|
And removing a claim is also simple, using the same API:
|
|
And removing a claim is also simple, using the same API:
|
|
|
|
|
|
@@ -1295,7 +1295,7 @@ MappedJwtClaimSetConverter.withDefaults(mapOf("legacyclaim" to Converter<Any, An
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-claimsetmapping-rename]]
|
|
[[oauth2resourceserver-jwt-claimsetmapping-rename]]
|
|
-==== Renaming a Claim
|
|
|
|
|
|
+=== 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>>`:
|
|
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>>`:
|
|
|
|
|
|
@@ -1360,7 +1360,7 @@ fun jwtDecoder(): JwtDecoder {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-jwt-timeouts]]
|
|
[[oauth2resourceserver-jwt-timeouts]]
|
|
-=== Configuring Timeouts
|
|
|
|
|
|
+== Configuring Timeouts
|
|
|
|
|
|
By default, Resource Server uses connection and socket timeouts of 30 seconds each for coordinating with the authorization server.
|
|
By default, Resource Server uses connection and socket timeouts of 30 seconds each for coordinating with the authorization server.
|
|
|
|
|
|
@@ -1436,14 +1436,14 @@ NOTE: Whether it's socket or cache timeouts, you may instead want to work with N
|
|
To do so, remember that `NimbusJwtDecoder` ships with a constructor that takes Nimbus's `JWTProcessor`.
|
|
To do so, remember that `NimbusJwtDecoder` ships with a constructor that takes Nimbus's `JWTProcessor`.
|
|
|
|
|
|
[[oauth2resourceserver-opaque-minimaldependencies]]
|
|
[[oauth2resourceserver-opaque-minimaldependencies]]
|
|
-=== Minimal Dependencies for Introspection
|
|
|
|
|
|
+== Minimal Dependencies for Introspection
|
|
As described in <<oauth2resourceserver-jwt-minimaldependencies,Minimal Dependencies for JWT>> most of Resource Server support is collected in `spring-security-oauth2-resource-server`.
|
|
As described in <<oauth2resourceserver-jwt-minimaldependencies,Minimal Dependencies for JWT>> most of Resource Server support is collected in `spring-security-oauth2-resource-server`.
|
|
However unless a custom <<oauth2resourceserver-opaque-introspector,`OpaqueTokenIntrospector`>> is provided, the Resource Server will fallback to NimbusOpaqueTokenIntrospector.
|
|
However unless a custom <<oauth2resourceserver-opaque-introspector,`OpaqueTokenIntrospector`>> is provided, the Resource Server will fallback to NimbusOpaqueTokenIntrospector.
|
|
Meaning that both `spring-security-oauth2-resource-server` and `oauth2-oidc-sdk` are necessary in order to have a working minimal Resource Server that supports opaque Bearer Tokens.
|
|
Meaning that both `spring-security-oauth2-resource-server` and `oauth2-oidc-sdk` are necessary in order to have a working minimal Resource Server that supports opaque Bearer Tokens.
|
|
Please refer to `spring-security-oauth2-resource-server` in order to determin the correct version for `oauth2-oidc-sdk`.
|
|
Please refer to `spring-security-oauth2-resource-server` in order to determin the correct version for `oauth2-oidc-sdk`.
|
|
|
|
|
|
[[oauth2resourceserver-opaque-minimalconfiguration]]
|
|
[[oauth2resourceserver-opaque-minimalconfiguration]]
|
|
-=== Minimal Configuration for Introspection
|
|
|
|
|
|
+== Minimal Configuration for Introspection
|
|
|
|
|
|
Typically, an opaque token can be verified via an https://tools.ietf.org/html/rfc7662[OAuth 2.0 Introspection Endpoint], hosted by the authorization server.
|
|
Typically, an opaque token can be verified via an https://tools.ietf.org/html/rfc7662[OAuth 2.0 Introspection Endpoint], hosted by the authorization server.
|
|
This can be handy when revocation is a requirement.
|
|
This can be handy when revocation is a requirement.
|
|
@@ -1452,7 +1452,7 @@ When using https://spring.io/projects/spring-boot[Spring Boot], configuring an a
|
|
First, include the needed dependencies and second, indicate the introspection endpoint details.
|
|
First, include the needed dependencies and second, indicate the introspection endpoint details.
|
|
|
|
|
|
[[oauth2resourceserver-opaque-introspectionuri]]
|
|
[[oauth2resourceserver-opaque-introspectionuri]]
|
|
-==== Specifying the Authorization Server
|
|
|
|
|
|
+=== Specifying the Authorization Server
|
|
|
|
|
|
To specify where the introspection endpoint is, simply do:
|
|
To specify where the introspection endpoint is, simply do:
|
|
|
|
|
|
@@ -1477,13 +1477,13 @@ If the authorization server responses that the token is valid, then it is.
|
|
|
|
|
|
And that's it!
|
|
And that's it!
|
|
|
|
|
|
-==== Startup Expectations
|
|
|
|
|
|
+=== Startup Expectations
|
|
|
|
|
|
When this property and these dependencies are used, Resource Server will automatically configure itself to validate Opaque Bearer Tokens.
|
|
When this property and these dependencies are used, Resource Server will automatically configure itself to validate Opaque Bearer Tokens.
|
|
|
|
|
|
This startup process is quite a bit simpler than for JWTs since no endpoints need to be discovered and no additional validation rules get added.
|
|
This startup process is quite a bit simpler than for JWTs since no endpoints need to be discovered and no additional validation rules get added.
|
|
|
|
|
|
-==== Runtime Expectations
|
|
|
|
|
|
+=== Runtime Expectations
|
|
|
|
|
|
Once the application is started up, Resource Server will attempt to process any request containing an `Authorization: Bearer` header:
|
|
Once the application is started up, Resource Server will attempt to process any request containing an `Authorization: Bearer` header:
|
|
|
|
|
|
@@ -1510,7 +1510,7 @@ From here, you may want to jump to:
|
|
* <<oauth2resourceserver-opaque-jwt-introspector,Using Introspection with JWTs>>
|
|
* <<oauth2resourceserver-opaque-jwt-introspector,Using Introspection with JWTs>>
|
|
|
|
|
|
[[oauth2resourceserver-opaque-architecture]]
|
|
[[oauth2resourceserver-opaque-architecture]]
|
|
-=== How Opaque Token Authentication Works
|
|
|
|
|
|
+== How Opaque Token Authentication Works
|
|
|
|
|
|
Next, let's see the architectural components that Spring Security uses to support https://tools.ietf.org/html/rfc7662[opaque token] Authentication in servlet-based applications, like the one we just saw.
|
|
Next, let's see the architectural components that Spring Security uses to support https://tools.ietf.org/html/rfc7662[opaque token] Authentication in servlet-based applications, like the one we just saw.
|
|
|
|
|
|
@@ -1532,7 +1532,7 @@ When authentication is successful, the <<servlet-authentication-authentication,`
|
|
Ultimately, the returned `BearerTokenAuthentication` will be set on the <<servlet-authentication-securitycontextholder,`SecurityContextHolder`>> by the authentication `Filter`.
|
|
Ultimately, the returned `BearerTokenAuthentication` will be set on the <<servlet-authentication-securitycontextholder,`SecurityContextHolder`>> by the authentication `Filter`.
|
|
|
|
|
|
[[oauth2resourceserver-opaque-attributes]]
|
|
[[oauth2resourceserver-opaque-attributes]]
|
|
-=== Looking Up Attributes Post-Authentication
|
|
|
|
|
|
+== Looking Up Attributes Post-Authentication
|
|
|
|
|
|
Once a token is authenticated, an instance of `BearerTokenAuthentication` is set in the `SecurityContext`.
|
|
Once a token is authenticated, an instance of `BearerTokenAuthentication` is set in the `SecurityContext`.
|
|
|
|
|
|
@@ -1580,7 +1580,7 @@ fun foo(@AuthenticationPrincipal principal: OAuth2AuthenticatedPrincipal): Strin
|
|
----
|
|
----
|
|
====
|
|
====
|
|
|
|
|
|
-==== Looking Up Attributes Via SpEL
|
|
|
|
|
|
+=== Looking Up Attributes Via SpEL
|
|
|
|
|
|
Of course, this also means that attributes can be accessed via SpEL.
|
|
Of course, this also means that attributes can be accessed via SpEL.
|
|
|
|
|
|
@@ -1607,7 +1607,7 @@ fun forFoosEyesOnly(): String {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-opaque-sansboot]]
|
|
[[oauth2resourceserver-opaque-sansboot]]
|
|
-=== Overriding or Replacing Boot Auto Configuration
|
|
|
|
|
|
+== Overriding or Replacing Boot Auto Configuration
|
|
|
|
|
|
There are two ``@Bean``s that Spring Boot generates on Resource Server's behalf.
|
|
There are two ``@Bean``s that Spring Boot generates on Resource Server's behalf.
|
|
|
|
|
|
@@ -1758,7 +1758,7 @@ And the <<oauth2resourceserver-opaque-architecture-introspector,`OpaqueTokenIntr
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-opaque-introspectionuri-dsl]]
|
|
[[oauth2resourceserver-opaque-introspectionuri-dsl]]
|
|
-==== Using `introspectionUri()`
|
|
|
|
|
|
+=== Using `introspectionUri()`
|
|
|
|
|
|
An authorization server's Introspection Uri can be configured <<oauth2resourceserver-opaque-introspectionuri,as a configuration property>> or it can be supplied in the DSL:
|
|
An authorization server's Introspection Uri can be configured <<oauth2resourceserver-opaque-introspectionuri,as a configuration property>> or it can be supplied in the DSL:
|
|
|
|
|
|
@@ -1820,7 +1820,7 @@ class DirectlyConfiguredIntrospectionUri : WebSecurityConfigurerAdapter() {
|
|
Using `introspectionUri()` takes precedence over any configuration property.
|
|
Using `introspectionUri()` takes precedence over any configuration property.
|
|
|
|
|
|
[[oauth2resourceserver-opaque-introspector-dsl]]
|
|
[[oauth2resourceserver-opaque-introspector-dsl]]
|
|
-==== Using `introspector()`
|
|
|
|
|
|
+=== Using `introspector()`
|
|
|
|
|
|
More powerful than `introspectionUri()` is `introspector()`, which will completely replace any Boot auto configuration of <<oauth2resourceserver-opaque-architecture-introspector,`OpaqueTokenIntrospector`>>:
|
|
More powerful than `introspectionUri()` is `introspector()`, which will completely replace any Boot auto configuration of <<oauth2resourceserver-opaque-architecture-introspector,`OpaqueTokenIntrospector`>>:
|
|
|
|
|
|
@@ -1880,7 +1880,7 @@ class DirectlyConfiguredIntrospector : WebSecurityConfigurerAdapter() {
|
|
This is handy when deeper configuration, like <<oauth2resourceserver-opaque-authorization-extraction,authority mapping>>, <<oauth2resourceserver-opaque-jwt-introspector,JWT revocation>>, or <<oauth2resourceserver-opaque-timeouts,request timeouts>>, is necessary.
|
|
This is handy when deeper configuration, like <<oauth2resourceserver-opaque-authorization-extraction,authority mapping>>, <<oauth2resourceserver-opaque-jwt-introspector,JWT revocation>>, or <<oauth2resourceserver-opaque-timeouts,request timeouts>>, is necessary.
|
|
|
|
|
|
[[oauth2resourceserver-opaque-introspector-bean]]
|
|
[[oauth2resourceserver-opaque-introspector-bean]]
|
|
-==== Exposing a `OpaqueTokenIntrospector` `@Bean`
|
|
|
|
|
|
+=== Exposing a `OpaqueTokenIntrospector` `@Bean`
|
|
|
|
|
|
Or, exposing a <<oauth2resourceserver-opaque-architecture-introspector,`OpaqueTokenIntrospector`>> `@Bean` has the same effect as `introspector()`:
|
|
Or, exposing a <<oauth2resourceserver-opaque-architecture-introspector,`OpaqueTokenIntrospector`>> `@Bean` has the same effect as `introspector()`:
|
|
|
|
|
|
@@ -1893,7 +1893,7 @@ public OpaqueTokenIntrospector introspector() {
|
|
----
|
|
----
|
|
|
|
|
|
[[oauth2resourceserver-opaque-authorization]]
|
|
[[oauth2resourceserver-opaque-authorization]]
|
|
-=== Configuring Authorization
|
|
|
|
|
|
+== Configuring Authorization
|
|
|
|
|
|
An OAuth 2.0 Introspection endpoint will typically return a `scope` attribute, indicating the scopes (or authorities) it's been granted, for example:
|
|
An OAuth 2.0 Introspection endpoint will typically return a `scope` attribute, indicating the scopes (or authorities) it's been granted, for example:
|
|
|
|
|
|
@@ -1974,7 +1974,7 @@ fun getMessages(): List<Message?> {}
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-opaque-authorization-extraction]]
|
|
[[oauth2resourceserver-opaque-authorization-extraction]]
|
|
-==== Extracting Authorities Manually
|
|
|
|
|
|
+=== Extracting Authorities Manually
|
|
|
|
|
|
By default, Opaque Token support will extract the scope claim from an introspection response and parse it into individual `GrantedAuthority` instances.
|
|
By default, Opaque Token support will extract the scope claim from an introspection response and parse it into individual `GrantedAuthority` instances.
|
|
|
|
|
|
@@ -2058,7 +2058,7 @@ fun introspector(): OpaqueTokenIntrospector {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-opaque-timeouts]]
|
|
[[oauth2resourceserver-opaque-timeouts]]
|
|
-=== Configuring Timeouts
|
|
|
|
|
|
+== Configuring Timeouts
|
|
|
|
|
|
By default, Resource Server uses connection and socket timeouts of 30 seconds each for coordinating with the authorization server.
|
|
By default, Resource Server uses connection and socket timeouts of 30 seconds each for coordinating with the authorization server.
|
|
|
|
|
|
@@ -2099,7 +2099,7 @@ fun introspector(builder: RestTemplateBuilder, properties: OAuth2ResourceServerP
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-opaque-jwt-introspector]]
|
|
[[oauth2resourceserver-opaque-jwt-introspector]]
|
|
-=== Using Introspection with JWTs
|
|
|
|
|
|
+== Using Introspection with JWTs
|
|
|
|
|
|
A common question is whether or not introspection is compatible with JWTs.
|
|
A common question is whether or not introspection is compatible with JWTs.
|
|
Spring Security's Opaque Token support has been designed to not care about the format of the token -- it will gladly pass any token to the introspection endpoint provided.
|
|
Spring Security's Opaque Token support has been designed to not care about the format of the token -- it will gladly pass any token to the introspection endpoint provided.
|
|
@@ -2204,7 +2204,7 @@ fun introspector(): OpaqueTokenIntrospector {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-opaque-userinfo]]
|
|
[[oauth2resourceserver-opaque-userinfo]]
|
|
-=== Calling a `/userinfo` Endpoint
|
|
|
|
|
|
+== Calling a `/userinfo` Endpoint
|
|
|
|
|
|
Generally speaking, a Resource Server doesn't care about the underlying user, but instead about the authorities that have been granted.
|
|
Generally speaking, a Resource Server doesn't care about the underlying user, but instead about the authorities that have been granted.
|
|
|
|
|
|
@@ -2324,7 +2324,7 @@ fun introspector(): OpaqueTokenIntrospector {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2reourceserver-opaqueandjwt]]
|
|
[[oauth2reourceserver-opaqueandjwt]]
|
|
-=== Supporting both JWT and Opaque Token
|
|
|
|
|
|
+== Supporting both JWT and Opaque Token
|
|
|
|
|
|
In some cases, you may have a need to access both kinds of tokens.
|
|
In some cases, you may have a need to access both kinds of tokens.
|
|
For example, you may support more than one tenant where one tenant issues JWTs and the other issues opaque tokens.
|
|
For example, you may support more than one tenant where one tenant issues JWTs and the other issues opaque tokens.
|
|
@@ -2407,7 +2407,7 @@ http {
|
|
====
|
|
====
|
|
|
|
|
|
[[oauth2resourceserver-multitenancy]]
|
|
[[oauth2resourceserver-multitenancy]]
|
|
-=== Multi-tenancy
|
|
|
|
|
|
+== Multi-tenancy
|
|
|
|
|
|
A resource server is considered multi-tenant when there are multiple strategies for verifying a bearer token, keyed by some tenant identifier.
|
|
A resource server is considered multi-tenant when there are multiple strategies for verifying a bearer token, keyed by some tenant identifier.
|
|
|
|
|
|
@@ -2419,7 +2419,7 @@ In each case, there are two things that need to be done and trade-offs associate
|
|
1. Resolve the tenant
|
|
1. Resolve the tenant
|
|
2. Propagate the tenant
|
|
2. Propagate the tenant
|
|
|
|
|
|
-==== Resolving the Tenant By Claim
|
|
|
|
|
|
+=== Resolving the Tenant By Claim
|
|
|
|
|
|
One way to differentiate tenants is by the issuer claim. Since the issuer claim accompanies signed JWTs, this can be done with the `JwtIssuerAuthenticationManagerResolver`, like so:
|
|
One way to differentiate tenants is by the issuer claim. Since the issuer claim accompanies signed JWTs, this can be done with the `JwtIssuerAuthenticationManagerResolver`, like so:
|
|
|
|
|
|
@@ -2478,7 +2478,7 @@ This is nice because the issuer endpoints are loaded lazily.
|
|
In fact, the corresponding `JwtAuthenticationProvider` is instantiated only when the first request with the corresponding issuer is sent.
|
|
In fact, the corresponding `JwtAuthenticationProvider` is instantiated only when the first request with the corresponding issuer is sent.
|
|
This allows for an application startup that is independent from those authorization servers being up and available.
|
|
This allows for an application startup that is independent from those authorization servers being up and available.
|
|
|
|
|
|
-===== Dynamic Tenants
|
|
|
|
|
|
+==== Dynamic Tenants
|
|
|
|
|
|
Of course, you may not want to restart the application each time a new tenant is added.
|
|
Of course, you may not want to restart the application each time a new tenant is added.
|
|
In this case, you can configure the `JwtIssuerAuthenticationManagerResolver` with a repository of `AuthenticationManager` instances, which you can edit at runtime, like so:
|
|
In this case, you can configure the `JwtIssuerAuthenticationManagerResolver` with a repository of `AuthenticationManager` instances, which you can edit at runtime, like so:
|
|
@@ -2538,7 +2538,7 @@ This approach allows us to add and remove elements from the repository (shown as
|
|
NOTE: It would be unsafe to simply take any issuer and construct an `AuthenticationManager` from it.
|
|
NOTE: It would be unsafe to simply take any issuer and construct an `AuthenticationManager` from it.
|
|
The issuer should be one that the code can verify from a trusted source like a list of allowed issuers.
|
|
The issuer should be one that the code can verify from a trusted source like a list of allowed issuers.
|
|
|
|
|
|
-===== Parsing the Claim Only Once
|
|
|
|
|
|
+==== Parsing the Claim Only Once
|
|
|
|
|
|
You may have observed that this strategy, while simple, comes with the trade-off that the JWT is parsed once by the `AuthenticationManagerResolver` and then again by the <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>> later on in the request.
|
|
You may have observed that this strategy, while simple, comes with the trade-off that the JWT is parsed once by the `AuthenticationManagerResolver` and then again by the <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>> later on in the request.
|
|
|
|
|
|
@@ -2768,12 +2768,12 @@ For example, if you are resolving it by subdomain, you may need to address the d
|
|
However, if you resolve it by a claim in the bearer token, read on to learn about <<oauth2resourceserver-bearertoken-resolver,Spring Security's support for bearer token propagation>>.
|
|
However, if you resolve it by a claim in the bearer token, read on to learn about <<oauth2resourceserver-bearertoken-resolver,Spring Security's support for bearer token propagation>>.
|
|
|
|
|
|
[[oauth2resourceserver-bearertoken-resolver]]
|
|
[[oauth2resourceserver-bearertoken-resolver]]
|
|
-=== Bearer Token Resolution
|
|
|
|
|
|
+== Bearer Token Resolution
|
|
|
|
|
|
By default, Resource Server looks for a bearer token in the `Authorization` header.
|
|
By default, Resource Server looks for a bearer token in the `Authorization` header.
|
|
This, however, can be customized in a handful of ways.
|
|
This, however, can be customized in a handful of ways.
|
|
|
|
|
|
-==== Reading the Bearer Token from a Custom Header
|
|
|
|
|
|
+=== Reading the Bearer Token from a Custom Header
|
|
|
|
|
|
For example, you may have a need to read the bearer token from a custom header.
|
|
For example, you may have a need to read the bearer token from a custom header.
|
|
To achieve this, you can expose a `DefaultBearerTokenResolver` as a bean, or wire an instance into the DSL, as you can see in the following example:
|
|
To achieve this, you can expose a `DefaultBearerTokenResolver` as a bean, or wire an instance into the DSL, as you can see in the following example:
|
|
@@ -2818,7 +2818,7 @@ fun bearerTokenResolver(): BearerTokenResolver {
|
|
|
|
|
|
Or, in circumstances where a provider is using both a custom header and value, you can use `HeaderBearerTokenResolver` instead.
|
|
Or, in circumstances where a provider is using both a custom header and value, you can use `HeaderBearerTokenResolver` instead.
|
|
|
|
|
|
-==== Reading the Bearer Token from a Form Parameter
|
|
|
|
|
|
+=== Reading the Bearer Token from a Form Parameter
|
|
|
|
|
|
Or, you may wish to read the token from a form parameter, which you can do by configuring the `DefaultBearerTokenResolver`, as you can see below:
|
|
Or, you may wish to read the token from a form parameter, which you can do by configuring the `DefaultBearerTokenResolver`, as you can see below:
|
|
|
|
|
|
@@ -2861,7 +2861,7 @@ http {
|
|
----
|
|
----
|
|
====
|
|
====
|
|
|
|
|
|
-=== Bearer Token Propagation
|
|
|
|
|
|
+== Bearer Token Propagation
|
|
|
|
|
|
Now that you're resource server has validated the token, it might be handy to pass it to downstream services.
|
|
Now that you're resource server has validated the token, it might be handy to pass it to downstream services.
|
|
This is quite simple with `{security-api-url}org/springframework/security/oauth2/server/resource/web/reactive/function/client/ServletBearerExchangeFilterFunction.html[ServletBearerExchangeFilterFunction]`, which you can see in the following example:
|
|
This is quite simple with `{security-api-url}org/springframework/security/oauth2/server/resource/web/reactive/function/client/ServletBearerExchangeFilterFunction.html[ServletBearerExchangeFilterFunction]`, which you can see in the following example:
|
|
@@ -2951,7 +2951,7 @@ In this case, the filter will fall back and simply forward the request onto the
|
|
Unlike the {security-api-url}org/springframework/security/oauth2/client/web/reactive/function/client/ServletOAuth2AuthorizedClientExchangeFilterFunction.html[OAuth 2.0 Client filter function], this filter function makes no attempt to renew the token, should it be expired.
|
|
Unlike the {security-api-url}org/springframework/security/oauth2/client/web/reactive/function/client/ServletOAuth2AuthorizedClientExchangeFilterFunction.html[OAuth 2.0 Client filter function], this filter function makes no attempt to renew the token, should it be expired.
|
|
To obtain this level of support, please use the OAuth 2.0 Client filter.
|
|
To obtain this level of support, please use the OAuth 2.0 Client filter.
|
|
|
|
|
|
-==== `RestTemplate` support
|
|
|
|
|
|
+=== `RestTemplate` support
|
|
|
|
|
|
There is no `RestTemplate` equivalent for `ServletBearerExchangeFilterFunction` at the moment, but you can propagate the request's bearer token quite simply with your own interceptor:
|
|
There is no `RestTemplate` equivalent for `ServletBearerExchangeFilterFunction` at the moment, but you can propagate the request's bearer token quite simply with your own interceptor:
|
|
|
|
|
|
@@ -3011,7 +3011,7 @@ Unlike the {security-api-url}org/springframework/security/oauth2/client/OAuth2Au
|
|
To obtain this level of support, please create an interceptor using the <<oauth2client,OAuth 2.0 Authorized Client Manager>>.
|
|
To obtain this level of support, please create an interceptor using the <<oauth2client,OAuth 2.0 Authorized Client Manager>>.
|
|
|
|
|
|
[[oauth2resourceserver-bearertoken-failure]]
|
|
[[oauth2resourceserver-bearertoken-failure]]
|
|
-=== Bearer Token Failure
|
|
|
|
|
|
+== Bearer Token Failure
|
|
|
|
|
|
A bearer token may be invalid for a number of reasons. For example, the token may no longer be active.
|
|
A bearer token may be invalid for a number of reasons. For example, the token may no longer be active.
|
|
|
|
|