Skip to content

Commit c85902f

Browse files
eleftheriaskostya05983
authored andcommitted
Allow configuration of oauth2 client through nested builder
Issue: spring-projectsgh-5557
1 parent 3be75c9 commit c85902f

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,41 @@ public OAuth2ClientConfigurer<HttpSecurity> oauth2Client() throws Exception {
20592059
return configurer;
20602060
}
20612061

2062+
/**
2063+
* Configures OAuth 2.0 Client support.
2064+
*
2065+
* <h2>Example Configuration</h2>
2066+
*
2067+
* The following example demonstrates how to enable OAuth 2.0 Client support for all endpoints.
2068+
*
2069+
* <pre>
2070+
* &#064;Configuration
2071+
* &#064;EnableWebSecurity
2072+
* public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
2073+
* &#064;Override
2074+
* protected void configure(HttpSecurity http) throws Exception {
2075+
* http
2076+
* .authorizeRequests(authorizeRequests ->
2077+
* authorizeRequests
2078+
* .anyRequest().authenticated()
2079+
* )
2080+
* .oauth2Client(withDefaults());
2081+
* }
2082+
* }
2083+
* </pre>
2084+
*
2085+
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-1.1">OAuth 2.0 Authorization Framework</a>
2086+
*
2087+
* @param oauth2ClientCustomizer the {@link Customizer} to provide more options for
2088+
* the {@link OAuth2ClientConfigurer}
2089+
* @return the {@link HttpSecurity} for further customizations
2090+
* @throws Exception
2091+
*/
2092+
public HttpSecurity oauth2Client(Customizer<OAuth2ClientConfigurer<HttpSecurity>> oauth2ClientCustomizer) throws Exception {
2093+
oauth2ClientCustomizer.customize(getOrApply(new OAuth2ClientConfigurer<>()));
2094+
return HttpSecurity.this;
2095+
}
2096+
20622097
/**
20632098
* Configures OAuth 2.0 Resource Server support.
20642099
*

config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2ClientConfigurer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package org.springframework.security.config.annotation.web.configurers.oauth2.client;
1717

1818
import org.springframework.security.authentication.AuthenticationManager;
19+
import org.springframework.security.config.Customizer;
1920
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
2021
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
2122
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
@@ -135,6 +136,20 @@ public AuthorizationCodeGrantConfigurer authorizationCodeGrant() {
135136
return this.authorizationCodeGrantConfigurer;
136137
}
137138

139+
/**
140+
* Configures the OAuth 2.0 Authorization Code Grant.
141+
*
142+
* @param authorizationCodeGrantCustomizer the {@link Customizer} to provide more options for
143+
* the {@link AuthorizationCodeGrantConfigurer}
144+
* @return the {@link OAuth2ClientConfigurer} for further customizations
145+
* @throws Exception
146+
*/
147+
public OAuth2ClientConfigurer<B> authorizationCodeGrant(Customizer<AuthorizationCodeGrantConfigurer> authorizationCodeGrantCustomizer)
148+
throws Exception {
149+
authorizationCodeGrantCustomizer.customize(this.authorizationCodeGrantConfigurer);
150+
return this;
151+
}
152+
138153
/**
139154
* Configuration options for the OAuth 2.0 Authorization Code Grant.
140155
*/

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2ClientConfigurerTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import static org.assertj.core.api.Assertions.assertThat;
6666
import static org.mockito.ArgumentMatchers.any;
6767
import static org.mockito.Mockito.*;
68+
import static org.springframework.security.config.Customizer.withDefaults;
6869
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
6970
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
7071
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -141,6 +142,19 @@ public void configureWhenAuthorizationCodeRequestThenRedirectForAuthorization()
141142
"redirect_uri=http://localhost/client-1");
142143
}
143144

145+
@Test
146+
public void configureWhenOauth2ClientInLambdaThenRedirectForAuthorization() throws Exception {
147+
this.spring.register(OAuth2ClientInLambdaConfig.class).autowire();
148+
149+
MvcResult mvcResult = this.mockMvc.perform(get("/oauth2/authorization/registration-1"))
150+
.andExpect(status().is3xxRedirection())
151+
.andReturn();
152+
assertThat(mvcResult.getResponse().getRedirectedUrl()).matches("https://provider.com/oauth2/authorize\\?" +
153+
"response_type=code&client_id=client-1&" +
154+
"scope=user&state=.{15,}&" +
155+
"redirect_uri=http://localhost/client-1");
156+
}
157+
144158
@Test
145159
public void configureWhenAuthorizationCodeResponseSuccessThenAuthorizedClientSaved() throws Exception {
146160
this.spring.register(OAuth2ClientConfig.class).autowire();
@@ -248,4 +262,30 @@ public String resource1(@RegisteredOAuth2AuthorizedClient("registration-1") OAut
248262
}
249263
}
250264
}
265+
266+
@EnableWebSecurity
267+
@EnableWebMvc
268+
static class OAuth2ClientInLambdaConfig extends WebSecurityConfigurerAdapter {
269+
@Override
270+
protected void configure(HttpSecurity http) throws Exception {
271+
// @formatter:off
272+
http
273+
.authorizeRequests(authorizeRequests ->
274+
authorizeRequests
275+
.anyRequest().authenticated()
276+
)
277+
.oauth2Client(withDefaults());
278+
// @formatter:on
279+
}
280+
281+
@Bean
282+
public ClientRegistrationRepository clientRegistrationRepository() {
283+
return clientRegistrationRepository;
284+
}
285+
286+
@Bean
287+
public OAuth2AuthorizedClientRepository authorizedClientRepository() {
288+
return authorizedClientRepository;
289+
}
290+
}
251291
}

0 commit comments

Comments
 (0)