核心配置
Spring Boot 示例
Spring Boot 为 OAuth 2.0 登录提供了完整的自动配置能力。
本节展示了如何使用 Google 作为 Authentication Provider 配置 OAuth 2.0 Login WebFlux 示例,并涵盖以下主题:
初始设置
要使用 Google 的 OAuth 2.0 认证系统进行登录,您必须在 Google API Console 中设置一个项目以获取 OAuth 2.0 凭据。
Google 的 OAuth 2.0 认证实现 符合 OpenID Connect 1.0 规范,并且已获得 OpenID 认证。 |
按照 OpenID Connect 页面上的说明进行操作,从“设置 OAuth 2.0”部分开始。
完成“获取 OAuth 2.0 凭据”说明后,您应该会拥有一个新的 OAuth 客户端,其凭据包括一个 Client ID 和一个 Client Secret。
设置重定向 URI
重定向 URI 是应用中的一个路径,终端用户的用户代理在 Google 认证并被授权访问同意页面上的 OAuth 客户端(在前一步创建的)后,会被重定向回该路径。
在“设置重定向 URI”小节中,确保 Authorized redirect URIs 字段设置为 localhost:8080/login/oauth2/code/google
。
默认的重定向 URI 模板是 |
配置 application.yml
现在您已经拥有一个 Google 的新 OAuth 客户端,您需要配置应用以使用该 OAuth 客户端进行认证流程。具体操作如下:
-
转到
application.yml
并设置以下配置:示例 1. OAuth 客户端属性spring: security: oauth2: client: registration: (1) google: (2) client-id: google-client-id client-secret: google-client-secret
1 spring.security.oauth2.client.registration
是 OAuth 客户端属性的基础属性前缀。2 基础属性前缀后面是 ClientRegistration
的 ID,例如 google。 -
将
client-id
和client-secret
属性中的值替换为您之前创建的 OAuth 2.0 凭据。
启动应用
启动 Spring Boot 示例并访问 localhost:8080
。然后您会被重定向到默认的自动生成的登录页面,该页面显示一个指向 Google 的链接。
点击 Google 链接,您将重定向到 Google 进行认证。
使用您的 Google 账户凭据认证后,接下来显示的页面是 Consent 页面。Consent 页面要求您允许或拒绝访问您之前创建的 OAuth 客户端。点击 Allow 授权 OAuth 客户端访问您的电子邮件地址和基本个人资料信息。
此时,OAuth 客户端从 UserInfo Endpoint 获取您的电子邮件地址和基本个人资料信息,并建立一个认证会话。
Spring Boot 属性映射
下表概述了 Spring Boot OAuth 客户端属性到 ClientRegistration 属性的映射关系。
Spring Boot | ClientRegistration |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
可以通过发现 OpenID Connect Provider 的配置端点或 Authorization Server 的元数据端点,通过指定 spring.security.oauth2.client.provider.[providerId].issuer-uri 属性来初步配置 ClientRegistration。 |
CommonOAuth2Provider
CommonOAuth2Provider
为许多知名提供商预定义了一组默认客户端属性:Google, GitHub, Facebook 和 Okta。
例如,提供商的 authorization-uri
, token-uri
和 user-info-uri
不经常改变。因此,提供默认值以减少所需的配置是合理的。
如前所述,当我们配置 Google 客户端时,只需要 client-id
和 client-secret
属性。
以下列表显示了一个示例:
spring:
security:
oauth2:
client:
registration:
google:
client-id: google-client-id
client-secret: google-client-secret
此处,客户端属性的自动默认设置运行顺畅,因为 registrationId (google ) 与 CommonOAuth2Provider 中的 GOOGLE enum (不区分大小写) 相匹配。 |
对于您可能想指定不同的 registrationId
(例如 google-login
)的情况,您仍然可以通过配置 provider
属性来利用客户端属性的自动默认设置。
以下列表显示了一个示例:
spring:
security:
oauth2:
client:
registration:
google-login: (1)
provider: google (2)
client-id: google-client-id
client-secret: google-client-secret
1 | registrationId 设置为 google-login 。 |
2 | provider 属性设置为 google ,这将利用 CommonOAuth2Provider.GOOGLE.getBuilder() 中设置的客户端属性自动默认设置。 |
配置自定义提供商属性
有些 OAuth 2.0 提供商支持多租户,导致每个租户(或子域)的协议端点不同。
例如,注册到 Okta 的 OAuth 客户端被分配到一个特定的子域,并拥有自己的协议端点。
对于这些情况,Spring Boot 提供了以下基础属性来配置自定义提供商属性:spring.security.oauth2.client.provider.[providerId]
。
以下列表显示了一个示例:
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 | 基础属性(spring.security.oauth2.client.provider.okta )允许对协议端点位置进行自定义配置。 |
覆盖 Spring Boot 自动配置
Spring Boot OAuth 客户端支持的自动配置类是 ReactiveOAuth2ClientAutoConfiguration
。
它执行以下任务:
-
注册一个由配置的 OAuth 客户端属性组成的
ReactiveClientRegistrationRepository
@Bean
。 -
注册一个
SecurityWebFilterChain
@Bean
并通过serverHttpSecurity.oauth2Login()
启用 OAuth 2.0 登录。
如果需要根据您的具体需求覆盖自动配置,可以通过以下方式进行:
注册 ReactiveClientRegistrationRepository @Bean
以下示例展示了如何注册一个 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://#/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://#/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()
}
}
注册 SecurityWebFilterChain @Bean
以下示例展示了如何使用 @EnableWebFluxSecurity
注册一个 SecurityWebFilterChain
@Bean
并通过 serverHttpSecurity.oauth2Login()
启用 OAuth 2.0 登录:
-
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()
}
}
完全覆盖自动配置
以下示例展示了如何通过注册 ReactiveClientRegistrationRepository
@Bean
和 SecurityWebFilterChain
@Bean
来完全覆盖自动配置。
-
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://#/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://#/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()
}
}
不使用 Spring Boot 的 Java 配置
如果您无法使用 Spring Boot 并希望配置 CommonOAuth2Provider
中预定义的提供商之一(例如 Google),请应用以下配置:
-
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()
}
}