Add Docs for Custom Bearer Token Header

Issue gh-8337
This commit is contained in:
Josh Cummings 2020-04-09 10:33:03 -06:00
parent 2f8eb16d76
commit 419d7264f9
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
2 changed files with 35 additions and 8 deletions

View File

@ -1077,6 +1077,29 @@ 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 `ReactiveAuthenticationManager` from it. NOTE: It would be unsafe to simply take any issuer and construct an `ReactiveAuthenticationManager` from it.
The issuer should be one that the code can verify from a trusted source like a whitelist. The issuer should be one that the code can verify from a trusted source like a whitelist.
[[webflux-oauth2resourceserver-bearertoken-resolver]]
== Bearer Token Resolution
By default, Resource Server looks for a bearer token in the `Authorization` header.
This, however, can be customized.
For example, you may have a need to read the bearer token from a custom header.
To achieve this, you can wire an instance of `ServerBearerTokenAuthenticationConverter` into the DSL, as you can see in the following example:
.Custom Bearer Token Header
====
.Java
[source,java,role="primary"]
----
ServerBearerTokenAuthenticationConverter converter = new ServerBearerTokenAuthenticationConverter();
converter.setBearerTokenHeaderName(HttpHeaders.PROXY_AUTHORIZATION);
http
.oauth2ResourceServer(oauth2 -> oauth2
.bearerTokenConverter(converter)
);
----
====
== Bearer Token Propagation == Bearer Token Propagation
Now that you're in possession of a bearer token, it might be handy to pass that to downstream services. Now that you're in possession of a bearer token, it might be handy to pass that to downstream services.

View File

@ -1955,22 +1955,24 @@ However, if you resolve it by a claim in the bearer token, read on to learn abou
=== 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 couple 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 wire a `HeaderBearerTokenResolver` 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:
.Custom Bearer Token Header .Custom Bearer Token Header
==== ====
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
http @Bean
.oauth2ResourceServer(oauth2 -> oauth2 BearerTokenResolver bearerTokenResolver() {
.bearerTokenResolver(new HeaderBearerTokenResolver("x-goog-iap-jwt-assertion")) DefaultBearerTokenResolver bearerTokenResolver = new DefaultBearerTokenResolver();
); bearerTokenResolver.setBearerTokenHeaderName(HttpHeaders.PROXY_AUTHORIZATION);
return bearerTokenResolver;
}
---- ----
.Xml .Xml
@ -1981,12 +1983,14 @@ http
</http> </http>
<bean id="bearerTokenResolver" <bean id="bearerTokenResolver"
class="org.springframework.security.oauth2.server.resource.web.HeaderBearerTokenResolver"> class="org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver">
<constructor-arg value="x-goog-iap-jwt-assertion"/> <property name="bearerTokenHeaderName" value="Proxy-Authorization"/>
</bean> </bean>
---- ----
==== ====
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: