Skip to content

Commit 5d0e80c

Browse files
committed
Polish BearerTokenResolver Docs
Issue gh-6254
1 parent 69a4848 commit 5d0e80c

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

docs/manual/src/docs/asciidoc/_includes/reactive/oauth2/access-token.adoc

-43
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,3 @@ SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
3333
----
3434

3535
You can now leverage Spring Security's <<webclient>> or <<webflux-roac,@RegisteredOAuth2AuthorizedClient>> support to obtain and use the access token.
36-
37-
== BearerTokenResolver
38-
39-
With interface BearerTokenResolver you can provide a strategy to resolve a bearer token.
40-
41-
The interface provides the next method:
42-
43-
[source,java]
44-
----
45-
/**
46-
* Resolve any <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer Token</a>
47-
* value from the request.
48-
*
49-
* @param request the request
50-
* @return the Bearer Token value or {@code null} if none found
51-
* @throws OAuth2AuthenticationException if the found token is invalid
52-
*/
53-
String resolve(HttpServletRequest request);
54-
----
55-
56-
In code base, you can find two implementation of this interface:
57-
HeaderBearerTokenResolver and DefaultBearerTokenResolver (based on RFC 6750).
58-
59-
Below you can see HeaderBearerTokenResolver, it takes a bearer token from request by header
60-
which was passed in constructor
61-
62-
[source,java]
63-
----
64-
public class HeaderBearerTokenResolver implements BearerTokenResolver {
65-
66-
private String header;
67-
68-
public HeaderBearerTokenResolver(String header) {
69-
Assert.hasText(header, "header cannot be empty");
70-
this.header = header;
71-
}
72-
73-
@Override
74-
public String resolve(HttpServletRequest request) {
75-
return request.getHeader(this.header);
76-
}
77-
}
78-
----

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

+31
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,37 @@ OpaqueTokenIntrospector introspector() {
11511151
Thus far we have only taken a look at the most basic authentication configuration.
11521152
Let's take a look at a few slightly more advanced options for configuring authentication.
11531153

1154+
[[oauth2resourceserver-bearertoken-resolver]]
1155+
=== Bearer Token Resolution
1156+
1157+
By default, Resource Server looks for a bearer token in the `Authorization` header.
1158+
This, however, can be customized in a couple of ways.
1159+
1160+
==== Reading the Bearer Token from a Custom Header
1161+
1162+
For example, you may have a need to read the bearer token from a custom header.
1163+
To achieve this, you can wire a `HeaderBearerTokenResolver` instance into the DSL, as you can see in the following example:
1164+
1165+
[source,java]
1166+
----
1167+
http
1168+
.oauth2ResourceServer()
1169+
.bearerTokenResolver(new HeaderBearerTokenResolver("x-goog-iap-jwt-assertion"));
1170+
----
1171+
1172+
==== Reading the Bearer Token from a Form Parameter
1173+
1174+
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:
1175+
1176+
[source,java]
1177+
----
1178+
DefaultBearerTokenResolver resolver = new DefaultBearerTokenResolver();
1179+
resolver.setAllowFormEncodedBodyParameter(true);
1180+
http
1181+
.oauth2ResourceServer()
1182+
.bearerTokenResolver(resolver);
1183+
----
1184+
11541185
=== Bearer Token Propagation
11551186

11561187
Now that you're in possession of a bearer token, it might be handy to pass that to downstream services.

0 commit comments

Comments
 (0)