高级配置

OAuth 2.0 授权框架定义了以下 协议端点

授权流程使用两个授权服务器端点(HTTP 资源)

  • 授权端点:客户端用于通过用户代理重定向从资源所有者获取授权。

  • 令牌端点:客户端用于将授权授予交换为访问令牌,通常使用客户端身份验证。

以及一个客户端端点

  • 重定向端点:授权服务器用于通过资源所有者用户代理将包含授权凭据的响应返回给客户端。

OpenID Connect Core 1.0 规范定义了以下 用户信息端点

用户信息端点是一个 OAuth 2.0 受保护资源,它返回有关已认证最终用户的声明。为了获取有关最终用户的请求声明,客户端使用通过 OpenID Connect 身份验证获得的访问令牌向用户信息端点发出请求。这些声明通常由一个 JSON 对象表示,该对象包含有关声明的名称-值对的集合。

ServerHttpSecurity.oauth2Login() 提供了许多配置选项,用于自定义 OAuth 2.0 登录。

以下代码显示了 oauth2Login() DSL 可用的完整配置选项。

OAuth2 登录配置选项
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
		http
			.oauth2Login(oauth2 -> oauth2
				.authenticationConverter(this.authenticationConverter())
				.authenticationMatcher(this.authenticationMatcher())
				.authenticationManager(this.authenticationManager())
				.authenticationSuccessHandler(this.authenticationSuccessHandler())
				.authenticationFailureHandler(this.authenticationFailureHandler())
				.clientRegistrationRepository(this.clientRegistrationRepository())
				.authorizedClientRepository(this.authorizedClientRepository())
				.authorizedClientService(this.authorizedClientService())
				.authorizationRequestResolver(this.authorizationRequestResolver())
				.authorizationRequestRepository(this.authorizationRequestRepository())
				.securityContextRepository(this.securityContextRepository())
			);

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

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            oauth2Login {
                authenticationConverter = authenticationConverter()
                authenticationMatcher = authenticationMatcher()
                authenticationManager = authenticationManager()
                authenticationSuccessHandler = authenticationSuccessHandler()
                authenticationFailureHandler = authenticationFailureHandler()
                clientRegistrationRepository = clientRegistrationRepository()
                authorizedClientRepository = authorizedClientRepository()
                authorizedClientService = authorizedClientService()
                authorizationRequestResolver = authorizationRequestResolver()
                authorizationRequestRepository = authorizationRequestRepository()
                securityContextRepository = securityContextRepository()
            }
        }

        return http.build()
    }
}

以下部分将更详细地介绍每个可用的配置选项。

OAuth 2.0 登录页面

默认情况下,OAuth 2.0 登录页面由 LoginPageGeneratingWebFilter 自动生成。默认登录页面显示每个配置的 OAuth 客户端,其 ClientRegistration.clientName 作为链接,能够启动授权请求(或 OAuth 2.0 登录)。

为了使 LoginPageGeneratingWebFilter 显示配置的 OAuth 客户端的链接,注册的 ReactiveClientRegistrationRepository 也需要实现 Iterable<ClientRegistration>。有关参考,请参阅 InMemoryReactiveClientRegistrationRepository

每个 OAuth 客户端的链接目标默认为以下内容

"/oauth2/authorization/{registrationId}"

以下行显示了一个示例

<a href="/oauth2/authorization/google">Google</a>

要覆盖默认登录页面,请配置 exceptionHandling().authenticationEntryPoint() 和(可选)oauth2Login().authorizationRequestResolver()

以下清单显示了一个示例

OAuth2 登录页面配置
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			.exceptionHandling(exceptionHandling -> exceptionHandling
				.authenticationEntryPoint(new RedirectServerAuthenticationEntryPoint("/login/oauth2"))
			)
			.oauth2Login(oauth2 -> oauth2
				.authorizationRequestResolver(this.authorizationRequestResolver())
			);

		return http.build();
	}

	private ServerOAuth2AuthorizationRequestResolver authorizationRequestResolver() {
		ServerWebExchangeMatcher authorizationRequestMatcher =
				new PathPatternParserServerWebExchangeMatcher(
						"/login/oauth2/authorization/{registrationId}");

		return new DefaultServerOAuth2AuthorizationRequestResolver(
				this.clientRegistrationRepository(), authorizationRequestMatcher);
	}

	...
}
@Configuration
@EnableWebFluxSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            exceptionHandling {
                authenticationEntryPoint = RedirectServerAuthenticationEntryPoint("/login/oauth2")
            }
            oauth2Login {
                authorizationRequestResolver = authorizationRequestResolver()
            }
        }

        return http.build()
    }

    private fun authorizationRequestResolver(): ServerOAuth2AuthorizationRequestResolver {
        val authorizationRequestMatcher: ServerWebExchangeMatcher = PathPatternParserServerWebExchangeMatcher(
            "/login/oauth2/authorization/{registrationId}"
        )

        return DefaultServerOAuth2AuthorizationRequestResolver(
            clientRegistrationRepository(), authorizationRequestMatcher
        )
    }

    ...
}
您需要提供一个带有 @RequestMapping("/login/oauth2")@Controller,它能够渲染自定义登录页面。

如前所述,配置 oauth2Login().authorizationRequestResolver() 是可选的。但是,如果您选择自定义它,请确保每个 OAuth 客户端的链接与通过 ServerWebExchangeMatcher 提供的模式匹配。

以下行显示了一个示例

<a href="/login/oauth2/authorization/google">Google</a>

重定向端点

重定向端点由授权服务器用于通过资源所有者用户代理将授权响应(包含授权凭据)返回给客户端。

OAuth 2.0 登录利用授权码授予。因此,授权凭据是授权码。

默认授权响应重定向端点为 /login/oauth2/code/{registrationId}

如果您想自定义授权响应重定向端点,请按照以下示例进行配置

重定向端点配置
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			.oauth2Login(oauth2 -> oauth2
				.authenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/login/oauth2/callback/{registrationId}"))
			);

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

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            oauth2Login {
                authenticationMatcher = PathPatternParserServerWebExchangeMatcher("/login/oauth2/callback/{registrationId}")
            }
        }

        return http.build()
    }
}

您还需要确保ClientRegistration.redirectUri与自定义授权响应重定向端点匹配。

以下清单显示了一个示例

  • Java

  • Kotlin

return CommonOAuth2Provider.GOOGLE.getBuilder("google")
	.clientId("google-client-id")
	.clientSecret("google-client-secret")
	.redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
	.build();
return CommonOAuth2Provider.GOOGLE.getBuilder("google")
    .clientId("google-client-id")
    .clientSecret("google-client-secret")
    .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
    .build()

用户信息端点

用户信息端点包含许多配置选项,如以下小节所述。

映射用户权限

用户成功通过 OAuth 2.0 提供商进行身份验证后,OAuth2User.getAuthorities()(或OidcUser.getAuthorities())将包含一个授予的权限列表,该列表从OAuth2UserRequest.getAccessToken().getScopes()填充,并以SCOPE_为前缀。这些授予的权限可以映射到一组新的GrantedAuthority实例,这些实例将在完成身份验证时提供给OAuth2AuthenticationToken

OAuth2AuthenticationToken.getAuthorities()用于授权请求,例如在hasRole('USER')hasRole('ADMIN')中。

在映射用户权限时,您可以选择以下几种方法。

使用 GrantedAuthoritiesMapper

GrantedAuthoritiesMapper将获得一个授予的权限列表,该列表包含一个类型为OAuth2UserAuthority的特殊权限和权限字符串OAUTH2_USER(或OidcUserAuthority和权限字符串OIDC_USER)。

注册一个GrantedAuthoritiesMapper @Bean,使其自动应用于配置,如以下示例所示。

授予的权限映射器配置
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			...
			.oauth2Login(withDefaults());

		return http.build();
	}

	@Bean
	public GrantedAuthoritiesMapper userAuthoritiesMapper() {
		return (authorities) -> {
			Set<GrantedAuthority> mappedAuthorities = new HashSet<>();

			authorities.forEach(authority -> {
				if (OidcUserAuthority.class.isInstance(authority)) {
					OidcUserAuthority oidcUserAuthority = (OidcUserAuthority)authority;

					OidcIdToken idToken = oidcUserAuthority.getIdToken();
					OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();

					// Map the claims found in idToken and/or userInfo
					// to one or more GrantedAuthority's and add it to mappedAuthorities

				} else if (OAuth2UserAuthority.class.isInstance(authority)) {
					OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;

					Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();

					// Map the attributes found in userAttributes
					// to one or more GrantedAuthority's and add it to mappedAuthorities

				}
			});

			return mappedAuthorities;
		};
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            oauth2Login { }
        }

        return http.build()
    }

    @Bean
    fun userAuthoritiesMapper(): GrantedAuthoritiesMapper = GrantedAuthoritiesMapper { authorities: Collection<GrantedAuthority> ->
        val mappedAuthorities = emptySet<GrantedAuthority>()

        authorities.forEach { authority ->
            if (authority is OidcUserAuthority) {
                val idToken = authority.idToken
                val userInfo = authority.userInfo
                // Map the claims found in idToken and/or userInfo
                // to one or more GrantedAuthority's and add it to mappedAuthorities
            } else if (authority is OAuth2UserAuthority) {
                val userAttributes = authority.attributes
                // Map the attributes found in userAttributes
                // to one or more GrantedAuthority's and add it to mappedAuthorities
            }
        }

        mappedAuthorities
    }
}

基于委托的策略,使用 ReactiveOAuth2UserService

与使用GrantedAuthoritiesMapper相比,这种策略更高级,但它也更灵活,因为它允许您访问OAuth2UserRequestOAuth2User(使用 OAuth 2.0 用户服务时)或OidcUserRequestOidcUser(使用 OpenID Connect 1.0 用户服务时)。

OAuth2UserRequest(和OidcUserRequest)允许您访问关联的OAuth2AccessToken,这在委托者需要从受保护的资源中获取权限信息才能为用户映射自定义权限的情况下非常有用。

以下示例展示了如何使用 OpenID Connect 1.0 用户服务实现和配置基于委托的策略。

ReactiveOAuth2UserService 配置
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			...
			.oauth2Login(withDefaults());

		return http.build();
	}

	@Bean
	public ReactiveOAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
		final OidcReactiveOAuth2UserService delegate = new OidcReactiveOAuth2UserService();

		return (userRequest) -> {
			// Delegate to the default implementation for loading a user
			return delegate.loadUser(userRequest)
					.flatMap((oidcUser) -> {
						OAuth2AccessToken accessToken = userRequest.getAccessToken();
						Set<GrantedAuthority> mappedAuthorities = new HashSet<>();

						// TODO
						// 1) Fetch the authority information from the protected resource using accessToken
						// 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities

						// 3) Create a copy of oidcUser but use the mappedAuthorities instead
						ProviderDetails providerDetails = userRequest.getClientRegistration().getProviderDetails();
						String userNameAttributeName = providerDetails.getUserInfoEndpoint().getUserNameAttributeName();
						if (StringUtils.hasText(userNameAttributeName)) {
							oidcUser = new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo(), userNameAttributeName);
						} else {
							oidcUser = new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo());
						}

						return Mono.just(oidcUser);
					});
		};
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            oauth2Login { }
        }

        return http.build()
    }

    @Bean
    fun oidcUserService(): ReactiveOAuth2UserService<OidcUserRequest, OidcUser> {
        val delegate = OidcReactiveOAuth2UserService()

        return ReactiveOAuth2UserService { userRequest ->
            // Delegate to the default implementation for loading a user
            delegate.loadUser(userRequest)
                .flatMap { oidcUser ->
                    val accessToken = userRequest.accessToken
                    val mappedAuthorities = mutableSetOf<GrantedAuthority>()

                    // TODO
                    // 1) Fetch the authority information from the protected resource using accessToken
                    // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
                    // 3) Create a copy of oidcUser but use the mappedAuthorities instead
                    val providerDetails = userRequest.getClientRegistration().getProviderDetails()
                    val userNameAttributeName = providerDetails.getUserInfoEndpoint().getUserNameAttributeName()
                    val mappedOidcUser = if (StringUtils.hasText(userNameAttributeName)) {
                        DefaultOidcUser(mappedAuthorities, oidcUser.idToken, oidcUser.userInfo, userNameAttributeName)
                    } else {
                        DefaultOidcUser(mappedAuthorities, oidcUser.idToken, oidcUser.userInfo)
                    }

                    Mono.just(mappedOidcUser)
                }
        }
    }
}

OAuth 2.0 用户服务

DefaultReactiveOAuth2UserServiceReactiveOAuth2UserService的实现,它支持标准的 OAuth 2.0 提供商。

ReactiveOAuth2UserService 从 UserInfo 端点获取最终用户(资源所有者)的用户属性(使用授权流程期间授予客户端的访问令牌),并以 OAuth2User 形式返回 AuthenticatedPrincipal

DefaultReactiveOAuth2UserService 在请求 UserInfo 端点的用户属性时使用 WebClient

如果您需要自定义 UserInfo 请求的预处理和/或 UserInfo 响应的后处理,则需要使用自定义配置的 WebClient 提供 DefaultReactiveOAuth2UserService.setWebClient()

无论您自定义 DefaultReactiveOAuth2UserService 还是提供您自己的 ReactiveOAuth2UserService 实现,您都需要按照以下示例进行配置

  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			...
			.oauth2Login(withDefaults());

		return http.build();
	}

	@Bean
	public ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService() {
		...
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            oauth2Login { }
        }

        return http.build()
    }

    @Bean
    fun oauth2UserService(): ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> {
        // ...
    }
}

OpenID Connect 1.0 用户服务

OidcReactiveOAuth2UserServiceReactiveOAuth2UserService 的实现,支持 OpenID Connect 1.0 提供者。

OidcReactiveOAuth2UserService 在请求 UserInfo 端点的用户属性时利用 DefaultReactiveOAuth2UserService

如果您需要自定义 UserInfo 请求的预处理和/或 UserInfo 响应的后处理,则需要使用自定义配置的 ReactiveOAuth2UserService 提供 OidcReactiveOAuth2UserService.setOauth2UserService()

无论您自定义 OidcReactiveOAuth2UserService 还是为 OpenID Connect 1.0 提供者提供您自己的 ReactiveOAuth2UserService 实现,您都需要按照以下示例进行配置

  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			...
			.oauth2Login(withDefaults());

		return http.build();
	}

	@Bean
	public ReactiveOAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
		...
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            oauth2Login { }
        }

        return http.build()
    }

    @Bean
    fun oidcUserService(): ReactiveOAuth2UserService<OidcUserRequest, OidcUser> {
        // ...
    }
}

ID 令牌签名验证

OpenID Connect 1.0 身份验证引入了 ID 令牌,它是一个安全令牌,包含关于授权服务器在客户端使用时对最终用户的身份验证的声明。

ID 令牌表示为 JSON Web 令牌 (JWT),并且必须使用 JSON Web 签名 (JWS) 签名。

ReactiveOidcIdTokenDecoderFactory 提供一个 ReactiveJwtDecoder,用于 OidcIdToken 签名验证。默认算法为 RS256,但在客户端注册期间分配时可能不同。对于这些情况,可以配置一个解析器以返回为特定客户端分配的预期 JWS 算法。

JWS 算法解析器是一个 Function,它接受一个 ClientRegistration 并返回客户端的预期 JwsAlgorithm,例如 SignatureAlgorithm.RS256MacAlgorithm.HS256

以下代码展示了如何配置 OidcIdTokenDecoderFactory @Bean,使其默认情况下对所有 ClientRegistration 使用 MacAlgorithm.HS256

  • Java

  • Kotlin

@Bean
public ReactiveJwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
	ReactiveOidcIdTokenDecoderFactory idTokenDecoderFactory = new ReactiveOidcIdTokenDecoderFactory();
	idTokenDecoderFactory.setJwsAlgorithmResolver(clientRegistration -> MacAlgorithm.HS256);
	return idTokenDecoderFactory;
}
@Bean
fun idTokenDecoderFactory(): ReactiveJwtDecoderFactory<ClientRegistration> {
    val idTokenDecoderFactory = ReactiveOidcIdTokenDecoderFactory()
    idTokenDecoderFactory.setJwsAlgorithmResolver { MacAlgorithm.HS256 }
    return idTokenDecoderFactory
}
对于基于 MAC 的算法,例如 HS256HS384HS512,与 client-id 相对应的 client-secret 用作签名验证的对称密钥。
如果为 OpenID Connect 1.0 身份验证配置了多个 ClientRegistration,则 JWS 算法解析器可能会评估提供的 ClientRegistration 以确定要返回的算法。

然后,您可以继续配置 注销