Core Configuration

Spring Boot 2.x Sample

Spring Boot 2.x 为 OAuth 2.0 登录带来了全自动配置功能。

Spring Boot 2.x brings full auto-configuration capabilities for OAuth 2.0 Login.

本部分展示了如何通过使用 _Google_作为 _Authentication Provider_来配置 OAuth 2.0 Login WebFlux sample,并涵盖以下主题:

This section shows how to configure the OAuth 2.0 Login WebFlux sample by using Google as the Authentication Provider and covers the following topics:

Initial Setup

要使用 Google 的 OAuth 2.0 身份验证系统进行登录,你必须在 Google API 控制台中设置一个项目才能获得 OAuth 2.0 凭据。

To use Google’s OAuth 2.0 authentication system for login, you must set up a project in the Google API Console to obtain OAuth 2.0 credentials.

符合 OpenID Connect 1.0规范的 Google’s OAuth 2.0 implementation用于认证,并且是 OpenID Certified

Google’s OAuth 2.0 implementation for authentication conforms to the OpenID Connect 1.0 specification and is OpenID Certified.

从 “Setting up OAuth 2.0” 部分开始,按照 OpenID Connect 页面上的说明进行操作。

Follow the instructions on the OpenID Connect page, starting in the “Setting up OAuth 2.0” section.

完成 “Obtain OAuth 2.0 credentials” 说明后,你应该有了凭证(其中包含客户端 ID 和客户端密钥)的新 OAuth 客户端。

After completing the “Obtain OAuth 2.0 credentials” instructions, you should have a new OAuth Client with credentials that consist of a Client ID and a Client Secret.

Setting the Redirect URI

重定向 URI 是应用程序中以下路径:在他们通过 Google 认证并在同意页面上被授予对 OAuth 客户端 (created in the previous step) 的访问权限后,将用户代理重定向回该路径。

The redirect URI is the path in the application that the end-user’s user-agent is redirected back to after they have authenticated with Google and have been granted access to the OAuth Client (webflux-oauth2-login-sample-setup) on the consent page.

在 “Set a redirect URI” 小节中,确保将 Authorized redirect URIs 字段设为 http://localhost:8080/login/oauth2/code/google

In the “Set a redirect URI” sub-section, ensure that the Authorized redirect URIs field is set to http://localhost:8080/login/oauth2/code/google.

默认重定向 URI 模板是 {baseUrl}/login/oauth2/code/{registrationId}registrationIdClientRegistration 的唯一标识符。对于我们的示例,registrationIdgoogle

The default redirect URI template is {baseUrl}/login/oauth2/code/{registrationId}. The registrationId is a unique identifier for the ClientRegistration. For our example, the registrationId is google.

如果 OAuth 客户端在代理服务器后运行,建议检查 Proxy Server Configuration,以确保应用程序配置正确。同时,请参阅 redirect-uri`支持的 `URI template variables

If the OAuth Client is running behind a proxy server, it is recommended to check Proxy Server Configuration to ensure the application is correctly configured. Also, see the supported URI template variables for redirect-uri.

Configure application.yml

现在,你已拥有新的与 Google 相关的 OAuth 客户端,你需要配置应用程序以使用 OAuth 客户端进行 authentication flow。若要执行此操作,请执行以下操作:

Now that you have a new OAuth Client with Google, you need to configure the application to use the OAuth Client for the authentication flow. To do so:

  1. Go to application.yml and set the following configuration:.OAuth Client properties

spring:
  security:
    oauth2:
      client:
        registration:	1
          google:	2
            client-id: google-client-id
            client-secret: google-client-secret
1 spring.security.oauth2.client.registration is the base property prefix for OAuth Client properties.
2 Following the base property prefix is the ID for the ClientRegistration, such as google.
  1. Replace the values in the client-id and client-secret property with the OAuth 2.0 credentials you created earlier.

Boot the Application

启动 Spring Boot 2.x 示例并转到 http://localhost:8080。然后你会被重定向到默认 auto-generated 登录页面,其中显示一个适用于 Google 的链接。

Launch the Spring Boot 2.x sample and go to http://localhost:8080. You are then redirected to the default auto-generated login page, which displays a link for Google.

点击 Google 链接,然后你将被重定向到 Google 进行身份验证。

Click on the Google link, and you are then redirected to Google for authentication.

使用你的 Google 帐户凭证进行身份验证之后,会显示给你下一页,即同意屏幕。同意屏幕要求你允许或拒绝访问你之前创建的 OAuth 客户端。单击 Allow 以授权 OAuth 客户端访问你的电子邮件地址和基本个人资料信息。

After authenticating with your Google account credentials, the next page presented to you is the Consent screen. The Consent screen asks you to either allow or deny access to the OAuth Client you created earlier. Click Allow to authorize the OAuth Client to access your email address and basic profile information.

此时,OAuth 客户端从 UserInfo Endpoint 中检索你的电子邮件地址和基本个人资料,并建立经过验证的会话。

At this point, the OAuth Client retrieves your email address and basic profile information from the UserInfo Endpoint and establishes an authenticated session.

Spring Boot 2.x Property Mappings

下表概述了 Spring Boot 2.x OAuth 客户端属性到 ClientRegistration属性的映射。

The following table outlines the mapping of the Spring Boot 2.x OAuth Client properties to the ClientRegistration properties.

Spring Boot 2.x ClientRegistration

spring.security.oauth2.client.registration.[registrationId]

registrationId

spring.security.oauth2.client.registration.[registrationId].client-id

clientId

spring.security.oauth2.client.registration.[registrationId].client-secret

clientSecret

spring.security.oauth2.client.registration.[registrationId].client-authentication-method

clientAuthenticationMethod

spring.security.oauth2.client.registration.[registrationId].authorization-grant-type

authorizationGrantType

spring.security.oauth2.client.registration.[registrationId].redirect-uri

redirectUri

spring.security.oauth2.client.registration.[registrationId].scope

scopes

spring.security.oauth2.client.registration.[registrationId].client-name

clientName

spring.security.oauth2.client.provider.[providerId].authorization-uri

providerDetails.authorizationUri

spring.security.oauth2.client.provider.[providerId].token-uri

providerDetails.tokenUri

spring.security.oauth2.client.provider.[providerId].jwk-set-uri

providerDetails.jwkSetUri

spring.security.oauth2.client.provider.[providerId].issuer-uri

providerDetails.issuerUri

spring.security.oauth2.client.provider.[providerId].user-info-uri

providerDetails.userInfoEndpoint.uri

spring.security.oauth2.client.provider.[providerId].user-info-authentication-method

providerDetails.userInfoEndpoint.authenticationMethod

spring.security.oauth2.client.provider.[providerId].user-name-attribute

providerDetails.userInfoEndpoint.userNameAttributeName

可以通过指定 spring.security.oauth2.client.provider.[providerId].issuer-uri 属性,使用对 OpenID Connect 提供程序的 Configuration endpoint 或授权服务器的 Metadata endpoint 的发现来初始配置 ClientRegistration

A ClientRegistration can be initially configured using discovery of an OpenID Connect Provider’s Configuration endpoint or an Authorization Server’s Metadata endpoint, by specifying the spring.security.oauth2.client.provider.[providerId].issuer-uri property.

CommonOAuth2Provider

CommonOAuth2Provider 为许多知名提供商(包括 Google、GitHub、Facebook 和 Okta)预定义了一组默认客户端属性。

CommonOAuth2Provider pre-defines a set of default client properties for a number of well known providers: Google, GitHub, Facebook, and Okta.

例如,authorization-uritoken-uriuser-info-uri 对于提供程序而言不常更改。因此,为了减少所需的配置,提供默认值是有意义的。

For example, the authorization-uri, token-uri, and user-info-uri do not change often for a Provider. Therefore, it makes sense to provide default values in order to reduce the required configuration.

如下所示,当我们 configured a Google client 时,只需要 client-idclient-secret 属性。

As demonstrated previously, when we webflux-oauth2-login-sample-config, only the client-id and client-secret properties are required.

下面的清单显示了一个示例:

The following listing shows an example:

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: google-client-id
            client-secret: google-client-secret

客户端属性的自动默认值在这里正常工作,因为 issuer-uri (google) 与 CommonOAuth2Provider 中的 issuer enum (不区分大小写) 匹配。

The auto-defaulting of client properties works seamlessly here because the registrationId (google) matches the GOOGLE enum (case-insensitive) in CommonOAuth2Provider.

对于可能希望指定不同的 registrationId(例如 google-login)的情况,你仍可以通过配置 provider 属性来利用客户端属性的自动默认值。

For cases where you may want to specify a different registrationId, such as google-login, you can still leverage auto-defaulting of client properties by configuring the provider property.

下面的清单显示了一个示例:

The following listing shows an example:

spring:
  security:
    oauth2:
      client:
        registration:
          google-login:	1
            provider: google	2
            client-id: google-client-id
            client-secret: google-client-secret
1 The registrationId is set to google-login.
2 The provider property is set to google, which will leverage the auto-defaulting of client properties set in CommonOAuth2Provider.GOOGLE.getBuilder().

Configuring Custom Provider Properties

有一些支持多租户的 OAuth 2.0 提供程序,这会导致每个租户(或子域)具有不同的协议端点。

There are some OAuth 2.0 Providers that support multi-tenancy, which results in different protocol endpoints for each tenant (or sub-domain).

例如,注册到 Okta 的 OAuth 客户端被分配到特定的子域,并有自己的协议端点。

For example, an OAuth Client registered with Okta is assigned to a specific sub-domain and have their own protocol endpoints.

针对这些情况,Spring Boot 2.x 为配置自定义提供程序属性提供了以下基本属性: spring.security.oauth2.client.provider.[providerId]

For these cases, Spring Boot 2.x provides the following base property for configuring custom provider properties: spring.security.oauth2.client.provider.[providerId].

下面的清单显示了一个示例:

The following listing shows an example:

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
        provider:
          okta:	1
            authorization-uri: https://your-subdomain.oktapreview.com/oauth2/v1/authorize
            token-uri: https://your-subdomain.oktapreview.com/oauth2/v1/token
            user-info-uri: https://your-subdomain.oktapreview.com/oauth2/v1/userinfo
            user-name-attribute: sub
            jwk-set-uri: https://your-subdomain.oktapreview.com/oauth2/v1/keys
1 The base property (spring.security.oauth2.client.provider.okta) allows for custom configuration of protocol endpoint locations.

Overriding Spring Boot 2.x Auto-configuration

Spring Boot 2.x OAuth 客户端支持的自动配置类是 ReactiveOAuth2ClientAutoConfiguration

The Spring Boot 2.x auto-configuration class for OAuth Client support is ReactiveOAuth2ClientAutoConfiguration.

它执行以下任务:

It performs the following tasks:

  • Registers a ReactiveClientRegistrationRepository @Bean composed of ClientRegistration(s) from the configured OAuth Client properties.

  • Registers a SecurityWebFilterChain @Bean and enables OAuth 2.0 Login through serverHttpSecurity.oauth2Login().

如果您需要根据您的特定要求覆盖自动配置,您可通过以下方式进行:

If you need to override the auto-configuration based on your specific requirements, you may do so in the following ways:

Register a ReactiveClientRegistrationRepository @Bean

以下示例演示如何注册 ReactiveClientRegistrationRepository @Bean

The following example shows how to register a ReactiveClientRegistrationRepository @Bean:

  • Java

  • Kotlin

@Configuration
public class OAuth2LoginConfig {

	@Bean
	public ReactiveClientRegistrationRepository clientRegistrationRepository() {
		return new InMemoryReactiveClientRegistrationRepository(this.googleClientRegistration());
	}

	private ClientRegistration googleClientRegistration() {
		return ClientRegistration.withRegistrationId("google")
				.clientId("google-client-id")
				.clientSecret("google-client-secret")
				.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
				.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
				.redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
				.scope("openid", "profile", "email", "address", "phone")
				.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
				.tokenUri("https://www.googleapis.com/oauth2/v4/token")
				.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
				.userNameAttributeName(IdTokenClaimNames.SUB)
				.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
				.clientName("Google")
				.build();
	}
}
@Configuration
class OAuth2LoginConfig {

    @Bean
    fun clientRegistrationRepository(): ReactiveClientRegistrationRepository {
        return InMemoryReactiveClientRegistrationRepository(googleClientRegistration())
    }

    private fun googleClientRegistration(): ClientRegistration {
        return ClientRegistration.withRegistrationId("google")
                .clientId("google-client-id")
                .clientSecret("google-client-secret")
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
                .scope("openid", "profile", "email", "address", "phone")
                .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
                .tokenUri("https://www.googleapis.com/oauth2/v4/token")
                .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
                .userNameAttributeName(IdTokenClaimNames.SUB)
                .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
                .clientName("Google")
                .build()
    }
}

Register a SecurityWebFilterChain @Bean

以下示例演示如何注册带有 @EnableWebFluxSecuritySecurityWebFilterChain @Bean,并通过 serverHttpSecurity.oauth2Login() 启用 OAuth 2.0 登录:

The following example shows how to register a SecurityWebFilterChain @Bean with @EnableWebFluxSecurity and enable OAuth 2.0 login through serverHttpSecurity.oauth2Login():

OAuth2 Login Configuration
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			.authorizeExchange(authorize -> authorize
				.anyExchange().authenticated()
			)
			.oauth2Login(withDefaults());

		return http.build();
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            authorizeExchange {
                authorize(anyExchange, authenticated)
            }
            oauth2Login { }
        }

        return http.build()
    }
}

Completely Override the Auto-configuration

以下示例演示如何通过注册 ReactiveClientRegistrationRepository @BeanSecurityWebFilterChain @Bean 来完全覆盖自动配置。

The following example shows how to completely override the auto-configuration by registering a ReactiveClientRegistrationRepository @Bean and a SecurityWebFilterChain @Bean.

Overriding the auto-configuration
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			.authorizeExchange(authorize -> authorize
				.anyExchange().authenticated()
			)
			.oauth2Login(withDefaults());

		return http.build();
	}

	@Bean
	public ReactiveClientRegistrationRepository clientRegistrationRepository() {
		return new InMemoryReactiveClientRegistrationRepository(this.googleClientRegistration());
	}

	private ClientRegistration googleClientRegistration() {
		return ClientRegistration.withRegistrationId("google")
				.clientId("google-client-id")
				.clientSecret("google-client-secret")
				.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
				.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
				.redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
				.scope("openid", "profile", "email", "address", "phone")
				.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
				.tokenUri("https://www.googleapis.com/oauth2/v4/token")
				.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
				.userNameAttributeName(IdTokenClaimNames.SUB)
				.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
				.clientName("Google")
				.build();
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2LoginConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            authorizeExchange {
                authorize(anyExchange, authenticated)
            }
            oauth2Login { }
        }

        return http.build()
    }

    @Bean
    fun clientRegistrationRepository(): ReactiveClientRegistrationRepository {
        return InMemoryReactiveClientRegistrationRepository(googleClientRegistration())
    }

    private fun googleClientRegistration(): ClientRegistration {
        return ClientRegistration.withRegistrationId("google")
                .clientId("google-client-id")
                .clientSecret("google-client-secret")
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
                .scope("openid", "profile", "email", "address", "phone")
                .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
                .tokenUri("https://www.googleapis.com/oauth2/v4/token")
                .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
                .userNameAttributeName(IdTokenClaimNames.SUB)
                .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
                .clientName("Google")
                .build()
    }
}

Java Configuration without Spring Boot 2.x

如果您无法使用 Spring Boot 2.x,并且想要在 CommonOAuth2Provider 中配置一个预定义的提供程序(例如,Google),请应用以下配置:

If you are not able to use Spring Boot 2.x and would like to configure one of the pre-defined providers in CommonOAuth2Provider (for example, Google), apply the following configuration:

OAuth2 Login Configuration
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			.authorizeExchange(authorize -> authorize
				.anyExchange().authenticated()
			)
			.oauth2Login(withDefaults());

		return http.build();
	}

	@Bean
	public ReactiveClientRegistrationRepository clientRegistrationRepository() {
		return new InMemoryReactiveClientRegistrationRepository(this.googleClientRegistration());
	}

	@Bean
	public ReactiveOAuth2AuthorizedClientService authorizedClientService(
			ReactiveClientRegistrationRepository clientRegistrationRepository) {
		return new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrationRepository);
	}

	@Bean
	public ServerOAuth2AuthorizedClientRepository authorizedClientRepository(
			ReactiveOAuth2AuthorizedClientService authorizedClientService) {
		return new AuthenticatedPrincipalServerOAuth2AuthorizedClientRepository(authorizedClientService);
	}

	private ClientRegistration googleClientRegistration() {
		return CommonOAuth2Provider.GOOGLE.getBuilder("google")
				.clientId("google-client-id")
				.clientSecret("google-client-secret")
				.build();
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2LoginConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            authorizeExchange {
                authorize(anyExchange, authenticated)
            }
            oauth2Login { }
        }

        return http.build()
    }

    @Bean
    fun clientRegistrationRepository(): ReactiveClientRegistrationRepository {
        return InMemoryReactiveClientRegistrationRepository(googleClientRegistration())
    }

    @Bean
    fun authorizedClientService(
        clientRegistrationRepository: ReactiveClientRegistrationRepository
    ): ReactiveOAuth2AuthorizedClientService {
        return InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrationRepository)
    }

    @Bean
    fun authorizedClientRepository(
        authorizedClientService: ReactiveOAuth2AuthorizedClientService
    ): ServerOAuth2AuthorizedClientRepository {
        return AuthenticatedPrincipalServerOAuth2AuthorizedClientRepository(authorizedClientService)
    }

    private fun googleClientRegistration(): ClientRegistration {
        return CommonOAuth2Provider.GOOGLE.getBuilder("google")
                .clientId("google-client-id")
                .clientSecret("google-client-secret")
                .build()
    }
}