Skip to content

Commit 3be75c9

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

File tree

3 files changed

+337
-0
lines changed

3 files changed

+337
-0
lines changed

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,103 @@ public OAuth2LoginConfigurer<HttpSecurity> oauth2Login() throws Exception {
19481948
return getOrApply(new OAuth2LoginConfigurer<>());
19491949
}
19501950

1951+
/**
1952+
* Configures authentication support using an OAuth 2.0 and/or OpenID Connect 1.0 Provider.
1953+
* <br>
1954+
* <br>
1955+
*
1956+
* The &quot;authentication flow&quot; is implemented using the <b>Authorization Code Grant</b>, as specified in the
1957+
* <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.1">OAuth 2.0 Authorization Framework</a>
1958+
* and <a target="_blank" href="https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth">OpenID Connect Core 1.0</a>
1959+
* specification.
1960+
* <br>
1961+
* <br>
1962+
*
1963+
* As a prerequisite to using this feature, you must register a client with a provider.
1964+
* The client registration information may than be used for configuring
1965+
* a {@link org.springframework.security.oauth2.client.registration.ClientRegistration} using a
1966+
* {@link org.springframework.security.oauth2.client.registration.ClientRegistration.Builder}.
1967+
* <br>
1968+
* <br>
1969+
*
1970+
* {@link org.springframework.security.oauth2.client.registration.ClientRegistration}(s) are composed within a
1971+
* {@link org.springframework.security.oauth2.client.registration.ClientRegistrationRepository},
1972+
* which is <b>required</b> and must be registered with the {@link ApplicationContext} or
1973+
* configured via <code>oauth2Login().clientRegistrationRepository(..)</code>.
1974+
* <br>
1975+
* <br>
1976+
*
1977+
* The default configuration provides an auto-generated login page at <code>&quot;/login&quot;</code> and
1978+
* redirects to <code>&quot;/login?error&quot;</code> when an authentication error occurs.
1979+
* The login page will display each of the clients with a link
1980+
* that is capable of initiating the &quot;authentication flow&quot;.
1981+
* <br>
1982+
* <br>
1983+
*
1984+
* <p>
1985+
* <h2>Example Configuration</h2>
1986+
*
1987+
* The following example shows the minimal configuration required, using Google as the Authentication Provider.
1988+
*
1989+
* <pre>
1990+
* &#064;Configuration
1991+
* public class OAuth2LoginConfig {
1992+
*
1993+
* &#064;EnableWebSecurity
1994+
* public static class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
1995+
* &#064;Override
1996+
* protected void configure(HttpSecurity http) throws Exception {
1997+
* http
1998+
* .authorizeRequests(authorizeRequests ->
1999+
* authorizeRequests
2000+
* .anyRequest().authenticated()
2001+
* )
2002+
* .oauth2Login(withDefaults());
2003+
* }
2004+
* }
2005+
*
2006+
* &#064;Bean
2007+
* public ClientRegistrationRepository clientRegistrationRepository() {
2008+
* return new InMemoryClientRegistrationRepository(this.googleClientRegistration());
2009+
* }
2010+
*
2011+
* private ClientRegistration googleClientRegistration() {
2012+
* return ClientRegistration.withRegistrationId("google")
2013+
* .clientId("google-client-id")
2014+
* .clientSecret("google-client-secret")
2015+
* .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
2016+
* .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
2017+
* .redirectUriTemplate("{baseUrl}/login/oauth2/code/{registrationId}")
2018+
* .scope("openid", "profile", "email", "address", "phone")
2019+
* .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
2020+
* .tokenUri("https://www.googleapis.com/oauth2/v4/token")
2021+
* .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
2022+
* .userNameAttributeName(IdTokenClaimNames.SUB)
2023+
* .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
2024+
* .clientName("Google")
2025+
* .build();
2026+
* }
2027+
* }
2028+
* </pre>
2029+
*
2030+
* <p>
2031+
* For more advanced configuration, see {@link OAuth2LoginConfigurer} for available options to customize the defaults.
2032+
*
2033+
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.1">Section 4.1 Authorization Code Grant</a>
2034+
* @see <a target="_blank" href="https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth">Section 3.1 Authorization Code Flow</a>
2035+
* @see org.springframework.security.oauth2.client.registration.ClientRegistration
2036+
* @see org.springframework.security.oauth2.client.registration.ClientRegistrationRepository
2037+
*
2038+
* @param oauth2LoginCustomizer the {@link Customizer} to provide more options for
2039+
* the {@link OAuth2LoginConfigurer}
2040+
* @return the {@link HttpSecurity} for further customizations
2041+
* @throws Exception
2042+
*/
2043+
public HttpSecurity oauth2Login(Customizer<OAuth2LoginConfigurer<HttpSecurity>> oauth2LoginCustomizer) throws Exception {
2044+
oauth2LoginCustomizer.customize(getOrApply(new OAuth2LoginConfigurer<>()));
2045+
return HttpSecurity.this;
2046+
}
2047+
19512048
/**
19522049
* Configures OAuth 2.0 Client support.
19532050
*

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.context.ApplicationContext;
2121
import org.springframework.core.ResolvableType;
2222
import org.springframework.security.authentication.AuthenticationProvider;
23+
import org.springframework.security.config.Customizer;
2324
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
2425
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2526
import org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer;
@@ -201,6 +202,20 @@ public AuthorizationEndpointConfig authorizationEndpoint() {
201202
return this.authorizationEndpointConfig;
202203
}
203204

205+
/**
206+
* Configures the Authorization Server's Authorization Endpoint.
207+
*
208+
* @param authorizationEndpointCustomizer the {@link Customizer} to provide more options for
209+
* the {@link AuthorizationEndpointConfig}
210+
* @return the {@link OAuth2LoginConfigurer} for further customizations
211+
* @throws Exception
212+
*/
213+
public OAuth2LoginConfigurer<B> authorizationEndpoint(Customizer<AuthorizationEndpointConfig> authorizationEndpointCustomizer)
214+
throws Exception {
215+
authorizationEndpointCustomizer.customize(this.authorizationEndpointConfig);
216+
return this;
217+
}
218+
204219
/**
205220
* Configuration options for the Authorization Server's Authorization Endpoint.
206221
*/
@@ -268,6 +283,20 @@ public TokenEndpointConfig tokenEndpoint() {
268283
return this.tokenEndpointConfig;
269284
}
270285

286+
/**
287+
* Configures the Authorization Server's Token Endpoint.
288+
*
289+
* @param tokenEndpointCustomizer the {@link Customizer} to provide more options for
290+
* the {@link TokenEndpointConfig}
291+
* @return the {@link OAuth2LoginConfigurer} for further customizations
292+
* @throws Exception
293+
*/
294+
public OAuth2LoginConfigurer<B> tokenEndpoint(Customizer<TokenEndpointConfig> tokenEndpointCustomizer)
295+
throws Exception {
296+
tokenEndpointCustomizer.customize(this.tokenEndpointConfig);
297+
return this;
298+
}
299+
271300
/**
272301
* Configuration options for the Authorization Server's Token Endpoint.
273302
*/
@@ -310,6 +339,20 @@ public RedirectionEndpointConfig redirectionEndpoint() {
310339
return this.redirectionEndpointConfig;
311340
}
312341

342+
/**
343+
* Configures the Client's Redirection Endpoint.
344+
*
345+
* @param redirectionEndpointCustomizer the {@link Customizer} to provide more options for
346+
* the {@link RedirectionEndpointConfig}
347+
* @return the {@link OAuth2LoginConfigurer} for further customizations
348+
* @throws Exception
349+
*/
350+
public OAuth2LoginConfigurer<B> redirectionEndpoint(Customizer<RedirectionEndpointConfig> redirectionEndpointCustomizer)
351+
throws Exception {
352+
redirectionEndpointCustomizer.customize(this.redirectionEndpointConfig);
353+
return this;
354+
}
355+
313356
/**
314357
* Configuration options for the Client's Redirection Endpoint.
315358
*/
@@ -350,6 +393,20 @@ public UserInfoEndpointConfig userInfoEndpoint() {
350393
return this.userInfoEndpointConfig;
351394
}
352395

396+
/**
397+
* Configures the Authorization Server's UserInfo Endpoint.
398+
*
399+
* @param userInfoEndpointCustomizer the {@link Customizer} to provide more options for
400+
* the {@link UserInfoEndpointConfig}
401+
* @return the {@link OAuth2LoginConfigurer} for further customizations
402+
* @throws Exception
403+
*/
404+
public OAuth2LoginConfigurer<B> userInfoEndpoint(Customizer<UserInfoEndpointConfig> userInfoEndpointCustomizer)
405+
throws Exception {
406+
userInfoEndpointCustomizer.customize(this.userInfoEndpointConfig);
407+
return this;
408+
}
409+
353410
/**
354411
* Configuration options for the Authorization Server's UserInfo Endpoint.
355412
*/

0 commit comments

Comments
 (0)