Skip to content

Commit 4b2539d

Browse files
committed
Allow configuration of oauth2 resource server through nested builder
Issue: gh-5557
1 parent 4157608 commit 4b2539d

File tree

3 files changed

+272
-5
lines changed

3 files changed

+272
-5
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,55 @@ public OAuth2ResourceServerConfigurer<HttpSecurity> oauth2ResourceServer() throw
21082108
return configurer;
21092109
}
21102110

2111+
/**
2112+
* Configures OAuth 2.0 Resource Server support.
2113+
*
2114+
* <h2>Example Configuration</h2>
2115+
*
2116+
* The following example demonstrates how to configure a custom JWT authentication converter.
2117+
*
2118+
* <pre>
2119+
* &#064;Configuration
2120+
* &#064;EnableWebSecurity
2121+
* public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
2122+
* &#064;Override
2123+
* protected void configure(HttpSecurity http) throws Exception {
2124+
* http
2125+
* .authorizeRequests(authorizeRequests ->
2126+
* authorizeRequests
2127+
* .anyRequest().authenticated()
2128+
* )
2129+
* .oauth2ResourceServer(oauth2ResourceServer ->
2130+
* oauth2ResourceServer
2131+
* .jwt(jwt ->
2132+
* jwt
2133+
* .jwtAuthenticationConverter(jwtDecoder())
2134+
* )
2135+
* );
2136+
* }
2137+
*
2138+
* &#064;Bean
2139+
* public JwtDecoder jwtDecoder() {
2140+
* return JwtDecoders.fromOidcIssuerLocation(issuerUri);
2141+
* }
2142+
* }
2143+
* </pre>
2144+
*
2145+
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-1.1">OAuth 2.0 Authorization Framework</a>
2146+
*
2147+
* @param oauth2ResourceServerCustomizer the {@link Customizer} to provide more options for
2148+
* the {@link OAuth2ResourceServerConfigurer}
2149+
* @return the {@link HttpSecurity} for further customizations
2150+
* @throws Exception
2151+
*/
2152+
public HttpSecurity oauth2ResourceServer(Customizer<OAuth2ResourceServerConfigurer<HttpSecurity>> oauth2ResourceServerCustomizer)
2153+
throws Exception {
2154+
OAuth2ResourceServerConfigurer<HttpSecurity> configurer = getOrApply(new OAuth2ResourceServerConfigurer<>(getContext()));
2155+
this.postProcess(configurer);
2156+
oauth2ResourceServerCustomizer.customize(configurer);
2157+
return HttpSecurity.this;
2158+
}
2159+
21112160
/**
21122161
* Configures channel security. In order for this configuration to be useful at least
21132162
* one mapping to a required channel must be provided.

config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.security.authentication.AuthenticationManager;
2626
import org.springframework.security.authentication.AuthenticationManagerResolver;
2727
import org.springframework.security.authentication.AuthenticationProvider;
28+
import org.springframework.security.config.Customizer;
2829
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
2930
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
3031
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
@@ -65,11 +66,12 @@
6566
* <li>{@link #accessDeniedHandler(AccessDeniedHandler)}</li> - customizes how access denied errors are handled
6667
* <li>{@link #authenticationEntryPoint(AuthenticationEntryPoint)}</li> - customizes how authentication failures are handled
6768
* <li>{@link #bearerTokenResolver(BearerTokenResolver)} - customizes how to resolve a bearer token from the request</li>
68-
* <li>{@link #jwt()} - enables Jwt-encoded bearer token support</li>
69+
* <li>{@link #jwt(Customizer)} - enables Jwt-encoded bearer token support</li>
70+
* <li>{@link #opaqueToken(Customizer)} - enables opaque bearer token support</li>
6971
* </ul>
7072
*
7173
* <p>
72-
* When using {@link #jwt()}, either
74+
* When using {@link #jwt(Customizer)}, either
7375
*
7476
* <ul>
7577
* <li>
@@ -83,7 +85,7 @@
8385
* </li>
8486
* </ul>
8587
*
86-
* Also with {@link #jwt()} consider
88+
* Also with {@link #jwt(Customizer)} consider
8789
*
8890
* <ul>
8991
* <li>
@@ -93,12 +95,12 @@
9395
* </ul>
9496
*
9597
* <p>
96-
* When using {@link #opaque()}, supply an introspection endpoint and its authentication configuration
98+
* When using {@link #opaqueToken(Customizer)}, supply an introspection endpoint and its authentication configuration
9799
* </p>
98100
*
99101
* <h2>Security Filters</h2>
100102
*
101-
* The following {@code Filter}s are populated when {@link #jwt()} is configured:
103+
* The following {@code Filter}s are populated when {@link #jwt(Customizer)} is configured:
102104
*
103105
* <ul>
104106
* <li>{@link BearerTokenAuthenticationFilter}</li>
@@ -180,6 +182,22 @@ public JwtConfigurer jwt() {
180182
return this.jwtConfigurer;
181183
}
182184

185+
/**
186+
* Enables Jwt-encoded bearer token support.
187+
*
188+
* @param jwtCustomizer the {@link Customizer} to provide more options for
189+
* the {@link JwtConfigurer}
190+
* @return the {@link OAuth2ResourceServerConfigurer} for further customizations
191+
* @throws Exception
192+
*/
193+
public OAuth2ResourceServerConfigurer<H> jwt(Customizer<JwtConfigurer> jwtCustomizer) throws Exception {
194+
if ( this.jwtConfigurer == null ) {
195+
this.jwtConfigurer = new JwtConfigurer(this.context);
196+
}
197+
jwtCustomizer.customize(this.jwtConfigurer);
198+
return this;
199+
}
200+
183201
public OpaqueTokenConfigurer opaqueToken() {
184202
if (this.opaqueTokenConfigurer == null) {
185203
this.opaqueTokenConfigurer = new OpaqueTokenConfigurer(this.context);
@@ -188,6 +206,23 @@ public OpaqueTokenConfigurer opaqueToken() {
188206
return this.opaqueTokenConfigurer;
189207
}
190208

209+
/**
210+
* Enables opaque bearer token support.
211+
*
212+
* @param opaqueTokenCustomizer the {@link Customizer} to provide more options for
213+
* the {@link OpaqueTokenConfigurer}
214+
* @return the {@link OAuth2ResourceServerConfigurer} for further customizations
215+
* @throws Exception
216+
*/
217+
public OAuth2ResourceServerConfigurer<H> opaqueToken(Customizer<OpaqueTokenConfigurer> opaqueTokenCustomizer)
218+
throws Exception {
219+
if (this.opaqueTokenConfigurer == null) {
220+
this.opaqueTokenConfigurer = new OpaqueTokenConfigurer(this.context);
221+
}
222+
opaqueTokenCustomizer.customize(this.opaqueTokenConfigurer);
223+
return this;
224+
}
225+
191226
@Override
192227
public void init(H http) throws Exception {
193228
registerDefaultAccessDeniedHandler(http);

0 commit comments

Comments
 (0)