Flatten ServerHttpSecurity.oauth2()

Fixes: gh-5712
This commit is contained in:
Rob Winch 2018-08-21 15:48:21 -05:00
parent 59cdfc7d6e
commit 0dc80aed40
4 changed files with 156 additions and 207 deletions

View File

@ -207,7 +207,9 @@ public class ServerHttpSecurity {
private OAuth2LoginSpec oauth2Login; private OAuth2LoginSpec oauth2Login;
private OAuth2Spec oauth2; private OAuth2ResourceServerSpec resourceServer;
private OAuth2ClientSpec client;
private LogoutSpec logout = new LogoutSpec(); private LogoutSpec logout = new LogoutSpec();
@ -572,41 +574,6 @@ public class ServerHttpSecurity {
private OAuth2LoginSpec() {} private OAuth2LoginSpec() {}
} }
/**
* Configures OAuth2 support. An example configuration is provided below:
*
* <pre class="code">
* &#064;Bean
* public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
* http
* // ...
* .oauth2()
* .resourceServer()
* .jwt()
* .jwkSetUri(jwkSetUri);
* return http.build();
* }
* </pre>
*
* @return the {@link HttpBasicSpec} to customize
*/
public OAuth2Spec oauth2() {
if (this.oauth2 == null) {
this.oauth2 = new OAuth2Spec();
}
return this.oauth2;
}
/**
* Configures OAuth2 Support
*
* @since 5.1
*/
public class OAuth2Spec {
private OAuth2ResourceServerSpec resourceServer;
private OAuth2ClientSpec client;
/** /**
* Configures the OAuth2 client. * Configures the OAuth2 client.
* *
@ -615,8 +582,7 @@ public class ServerHttpSecurity {
* public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { * public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
* http * http
* // ... * // ...
* .oauth2() * .oauth2Client()
* .client()
* .clientRegistrationRepository(clientRegistrationRepository) * .clientRegistrationRepository(clientRegistrationRepository)
* .authorizedClientRepository(authorizedClientRepository); * .authorizedClientRepository(authorizedClientRepository);
* return http.build(); * return http.build();
@ -626,7 +592,7 @@ public class ServerHttpSecurity {
* *
* @return the {@link OAuth2ClientSpec} to customize * @return the {@link OAuth2ClientSpec} to customize
*/ */
public OAuth2ClientSpec client() { public OAuth2ClientSpec oauth2Client() {
if (this.client == null) { if (this.client == null) {
this.client = new OAuth2ClientSpec(); this.client = new OAuth2ClientSpec();
} }
@ -705,7 +671,7 @@ public class ServerHttpSecurity {
private OAuth2ClientSpec() {} private OAuth2ClientSpec() {}
} }
public OAuth2ResourceServerSpec resourceServer() { public OAuth2ResourceServerSpec oauth2ResourceServer() {
if (this.resourceServer == null) { if (this.resourceServer == null) {
this.resourceServer = new OAuth2ResourceServerSpec(); this.resourceServer = new OAuth2ResourceServerSpec();
} }
@ -788,25 +754,9 @@ public class ServerHttpSecurity {
} }
} }
public OAuth2Spec and() {
return OAuth2Spec.this;
}
}
public ServerHttpSecurity and() { public ServerHttpSecurity and() {
return ServerHttpSecurity.this; return ServerHttpSecurity.this;
} }
protected void configure(ServerHttpSecurity http) {
if (this.resourceServer != null) {
this.resourceServer.configure(http);
}
if (this.client != null) {
this.client.configure(http);
}
}
private OAuth2Spec() {}
} }
/** /**
@ -1009,8 +959,11 @@ public class ServerHttpSecurity {
if (this.oauth2Login != null) { if (this.oauth2Login != null) {
this.oauth2Login.configure(this); this.oauth2Login.configure(this);
} }
if (this.oauth2 != null) { if (this.resourceServer != null) {
this.oauth2.configure(this); this.resourceServer.configure(this);
}
if (this.client != null) {
this.client.configure(this);
} }
this.loginPage.configure(this); this.loginPage.configure(this);
if(this.logout != null) { if(this.logout != null) {

View File

@ -95,8 +95,7 @@ public class OAuth2ClientSpecTests {
@Bean @Bean
SecurityWebFilterChain springSecurity(ServerHttpSecurity http) { SecurityWebFilterChain springSecurity(ServerHttpSecurity http) {
http http
.oauth2() .oauth2Client();
.client();
return http.build(); return http.build();
} }

View File

@ -40,8 +40,7 @@ public class SecurityConfig {
.and() .and()
.formLogin() .formLogin()
.and() .and()
.oauth2() .oauth2Client();
.client();
return http.build(); return http.build();
} }

View File

@ -45,8 +45,7 @@ public class SecurityConfig {
.authorizeExchange() .authorizeExchange()
.anyExchange().authenticated() .anyExchange().authenticated()
.and() .and()
.oauth2() .oauth2ResourceServer()
.resourceServer()
.jwt() .jwt()
.jwkSetUri(jwkSetUri); .jwkSetUri(jwkSetUri);
return http.build(); return http.build();
@ -59,8 +58,7 @@ public class SecurityConfig {
.authorizeExchange() .authorizeExchange()
.anyExchange().authenticated() .anyExchange().authenticated()
.and() .and()
.oauth2() .oauth2ResourceServer()
.resourceServer()
.jwt() .jwt()
.publicKey(publicKey()); .publicKey(publicKey());
return http.build(); return http.build();