@@ -1948,6 +1948,103 @@ public OAuth2LoginConfigurer<HttpSecurity> oauth2Login() throws Exception {
1948
1948
return getOrApply (new OAuth2LoginConfigurer <>());
1949
1949
}
1950
1950
1951
+ /**
1952
+ * Configures authentication support using an OAuth 2.0 and/or OpenID Connect 1.0 Provider.
1953
+ * <br>
1954
+ * <br>
1955
+ *
1956
+ * The "authentication flow" 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>"/login"</code> and
1978
+ * redirects to <code>"/login?error"</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 "authentication flow".
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
+ * @Configuration
1991
+ * public class OAuth2LoginConfig {
1992
+ *
1993
+ * @EnableWebSecurity
1994
+ * public static class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
1995
+ * @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
+ * @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
+
1951
2048
/**
1952
2049
* Configures OAuth 2.0 Client support.
1953
2050
*
0 commit comments