授权授权支持

授权码

请参考OAuth 2.0授权框架以获取有关授权码授权的更多详细信息。

获取授权

请参考授权请求/响应协议流程以了解授权码授权。

发起授权请求

OAuth2AuthorizationRequestRedirectWebFilter 使用 ServerOAuth2AuthorizationRequestResolver 来解析 OAuth2AuthorizationRequest 并通过将最终用户的用户代理重定向到授权服务器的授权端点来启动授权码授权流程。

ServerOAuth2AuthorizationRequestResolver 的主要作用是从提供的 Web 请求中解析 OAuth2AuthorizationRequest。默认实现 DefaultServerOAuth2AuthorizationRequestResolver 匹配 (默认) 路径 /oauth2/authorization/{registrationId},提取 registrationId 并使用它为关联的 ClientRegistration 构建 OAuth2AuthorizationRequest

给定OAuth 2.0客户端注册的以下Spring Boot属性

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
            authorization-grant-type: authorization_code
            redirect-uri: "{baseUrl}/authorized/okta"
            scope: read, write
        provider:
          okta:
            authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize
            token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token

具有基本路径 /oauth2/authorization/okta 的请求将由 OAuth2AuthorizationRequestRedirectWebFilter 发起授权请求重定向,并最终启动授权码授权流程。

AuthorizationCodeReactiveOAuth2AuthorizedClientProvider 是授权码授权的 ReactiveOAuth2AuthorizedClientProvider 的一个实现,它也由 OAuth2AuthorizationRequestRedirectWebFilter 发起授权请求重定向。

如果OAuth 2.0客户端是公共客户端,则配置OAuth 2.0客户端注册如下

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-authentication-method: none
            authorization-grant-type: authorization_code
            redirect-uri: "{baseUrl}/authorized/okta"
            ...

公共客户端使用代码交换证明密钥 (PKCE) 受支持。如果客户端在不受信任的环境(例如,原生应用程序或基于 Web 浏览器的应用程序)中运行,因此无法维护其凭据的机密性,则在以下条件为真时将自动使用 PKCE

  1. client-secret 被省略(或为空)

  2. client-authentication-method 设置为“none”(ClientAuthenticationMethod.NONE

如果OAuth 2.0提供程序支持机密客户端的PKCE,您可以(可选)使用DefaultServerOAuth2AuthorizationRequestResolver.setAuthorizationRequestCustomizer(OAuth2AuthorizationRequestCustomizers.withPkce())进行配置。

DefaultServerOAuth2AuthorizationRequestResolver 还支持使用 UriComponentsBuilderredirect-uri 使用 URI 模板变量。

以下配置使用所有受支持的 URI 模板变量

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            ...
            redirect-uri: "{baseScheme}://{baseHost}{basePort}{basePath}/authorized/{registrationId}"
            ...
{baseUrl} 解析为 {baseScheme}://{baseHost}{basePort}{basePath}

使用 URI 模板变量配置 redirect-uri 在 OAuth 2.0 客户端运行在代理服务器之后时特别有用。这确保在扩展 redirect-uri 时使用 X-Forwarded-* 标头。

自定义授权请求

ServerOAuth2AuthorizationRequestResolver 的主要用例之一是能够使用 OAuth 2.0 授权框架中定义的标准参数以外的其他参数自定义授权请求。

例如,OpenID Connect 为授权码流程定义了额外的 OAuth 2.0 请求参数,这些参数扩展了OAuth 2.0 授权框架中定义的标准参数。其中一个扩展参数是 prompt 参数。

prompt 参数是可选的。空格分隔、区分大小写的 ASCII 字符串值列表,指定授权服务器是否提示最终用户重新进行身份验证和授权。定义的值为:noneloginconsentselect_account

以下示例显示如何使用 Consumer<OAuth2AuthorizationRequest.Builder> 配置 DefaultServerOAuth2AuthorizationRequestResolver,该 Consumer 通过包含请求参数 prompt=consent 来自定义 oauth2Login() 的授权请求。

  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginSecurityConfig {

	@Autowired
	private ReactiveClientRegistrationRepository clientRegistrationRepository;

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			.authorizeExchange(authorize -> authorize
				.anyExchange().authenticated()
			)
			.oauth2Login(oauth2 -> oauth2
				.authorizationRequestResolver(
					authorizationRequestResolver(this.clientRegistrationRepository)
				)
			);
		return http.build();
	}

	private ServerOAuth2AuthorizationRequestResolver authorizationRequestResolver(
			ReactiveClientRegistrationRepository clientRegistrationRepository) {

		DefaultServerOAuth2AuthorizationRequestResolver authorizationRequestResolver =
				new DefaultServerOAuth2AuthorizationRequestResolver(
						clientRegistrationRepository);
		authorizationRequestResolver.setAuthorizationRequestCustomizer(
				authorizationRequestCustomizer());

		return  authorizationRequestResolver;
	}

	private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
		return customizer -> customizer
					.additionalParameters(params -> params.put("prompt", "consent"));
	}
}
@Configuration
@EnableWebFluxSecurity
class SecurityConfig {

    @Autowired
    private lateinit var customClientRegistrationRepository: ReactiveClientRegistrationRepository

    @Bean
    fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            authorizeExchange {
                authorize(anyExchange, authenticated)
            }
            oauth2Login {
                authorizationRequestResolver = authorizationRequestResolver(customClientRegistrationRepository)
            }
        }

        return http.build()
    }

    private fun authorizationRequestResolver(
            clientRegistrationRepository: ReactiveClientRegistrationRepository): ServerOAuth2AuthorizationRequestResolver {
        val authorizationRequestResolver = DefaultServerOAuth2AuthorizationRequestResolver(
                clientRegistrationRepository)
        authorizationRequestResolver.setAuthorizationRequestCustomizer(
                authorizationRequestCustomizer())
        return authorizationRequestResolver
    }

    private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
        return Consumer { customizer ->
            customizer
                .additionalParameters { params -> params["prompt"] = "consent" }
        }
    }
}

对于简单的用例,如果特定提供商的附加请求参数始终相同,则可以直接将其添加到authorization-uri属性中。

例如,如果对于提供商okta,请求参数prompt的值始终为consent,则只需按如下方式配置:

spring:
  security:
    oauth2:
      client:
        provider:
          okta:
            authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize?prompt=consent

上述示例展示了在标准参数之上添加自定义参数的常见用例。或者,如果您的需求更高级,您可以通过简单地覆盖OAuth2AuthorizationRequest.authorizationRequestUri属性来完全控制授权请求 URI 的构建。

OAuth2AuthorizationRequest.Builder.build()构造OAuth2AuthorizationRequest.authorizationRequestUri,它表示包含所有查询参数的授权请求 URI,使用application/x-www-form-urlencoded格式。

以下示例显示了前面示例中authorizationRequestCustomizer()的一个变体,它覆盖了OAuth2AuthorizationRequest.authorizationRequestUri属性。

  • Java

  • Kotlin

private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
	return customizer -> customizer
			.authorizationRequestUri(uriBuilder -> uriBuilder
					.queryParam("prompt", "consent").build());
}
private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
    return Consumer { customizer: OAuth2AuthorizationRequest.Builder ->
        customizer
                .authorizationRequestUri { uriBuilder: UriBuilder ->
                    uriBuilder
                            .queryParam("prompt", "consent").build()
                }
    }
}

存储授权请求

ServerAuthorizationRequestRepository负责从启动授权请求到收到授权响应(回调)期间的OAuth2AuthorizationRequest的持久化。

OAuth2AuthorizationRequest用于关联和验证授权响应。

ServerAuthorizationRequestRepository的默认实现是WebSessionOAuth2ServerAuthorizationRequestRepository,它将OAuth2AuthorizationRequest存储在WebSession中。

如果您有ServerAuthorizationRequestRepository的自定义实现,您可以按以下示例所示进行配置:

ServerAuthorizationRequestRepository 配置
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2ClientSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			.oauth2Client(oauth2 -> oauth2
				.authorizationRequestRepository(this.authorizationRequestRepository())
				...
			);
		return http.build();
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2ClientSecurityConfig {

    @Bean
    fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            oauth2Client {
                authorizationRequestRepository = authorizationRequestRepository()
            }
        }

        return http.build()
    }
}

请求访问令牌

请参考访问令牌请求/响应协议流程(针对授权码授权)。

授权码授权的ReactiveOAuth2AccessTokenResponseClient的默认实现是WebClientReactiveAuthorizationCodeTokenResponseClient,它使用WebClient在授权服务器的令牌端点交换授权码以获取访问令牌。

WebClientReactiveAuthorizationCodeTokenResponseClient非常灵活,因为它允许您自定义令牌请求的预处理和/或令牌响应的后处理。

自定义访问令牌请求

如果您需要自定义令牌请求的预处理,您可以为WebClientReactiveAuthorizationCodeTokenResponseClient.setParametersConverter()提供一个自定义的Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>>。默认实现构建一个MultiValueMap<String, String>,其中只包含标准OAuth 2.0 访问令牌请求grant_type参数,用于构造请求。WebClientReactiveAuthorizationCodeTokenResponseClient会直接将授权码授权所需的其它参数添加到请求体中。但是,提供自定义的Converter允许您扩展标准令牌请求并添加自定义参数。

如果您只想添加附加参数,您可以改为为WebClientReactiveAuthorizationCodeTokenResponseClient.addParametersConverter()提供一个自定义的Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>>,它将构建一个聚合Converter
自定义Converter必须返回目标 OAuth 2.0 提供商能够理解的有效的 OAuth 2.0 访问令牌请求参数。

自定义访问令牌响应

另一方面,如果您需要自定义令牌响应的后处理,则需要为WebClientReactiveAuthorizationCodeTokenResponseClient.setBodyExtractor()提供一个自定义配置的BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage>,用于将 OAuth 2.0 访问令牌响应转换为OAuth2AccessTokenResponseOAuth2BodyExtractors.oauth2AccessTokenResponse()提供的默认实现会解析响应并相应地处理错误。

自定义WebClient

或者,如果您的需求更高级,您可以通过简单地为WebClientReactiveAuthorizationCodeTokenResponseClient.setWebClient()提供一个自定义配置的WebClient来完全控制请求/响应。

无论您自定义WebClientReactiveAuthorizationCodeTokenResponseClient还是提供您自己的ReactiveOAuth2AccessTokenResponseClient实现,您都需要按以下示例所示进行配置:

访问令牌响应配置
  • Java

  • Kotlin

@Configuration
@EnableWebFluxSecurity
public class OAuth2ClientSecurityConfig {

	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		http
			.oauth2Client(oauth2 -> oauth2
				.authenticationManager(this.authorizationCodeAuthenticationManager())
				...
			);
		return http.build();
	}

	private ReactiveAuthenticationManager authorizationCodeAuthenticationManager() {
		WebClientReactiveAuthorizationCodeTokenResponseClient accessTokenResponseClient =
				new WebClientReactiveAuthorizationCodeTokenResponseClient();
		...

		return new OAuth2AuthorizationCodeReactiveAuthenticationManager(accessTokenResponseClient);
	}
}
@Configuration
@EnableWebFluxSecurity
class OAuth2ClientSecurityConfig {

    @Bean
    fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http {
            oauth2Client {
                authenticationManager = authorizationCodeAuthenticationManager()
            }
        }

        return http.build()
    }

    private fun authorizationCodeAuthenticationManager(): ReactiveAuthenticationManager {
        val accessTokenResponseClient = WebClientReactiveAuthorizationCodeTokenResponseClient()
        ...

        return OAuth2AuthorizationCodeReactiveAuthenticationManager(accessTokenResponseClient)
    }
}

刷新令牌

请参考 OAuth 2.0 授权框架了解有关刷新令牌的更多详细信息。

刷新访问令牌

请参考访问令牌请求/响应协议流程(针对刷新令牌授权)。

刷新令牌授权的ReactiveOAuth2AccessTokenResponseClient的默认实现是WebClientReactiveRefreshTokenTokenResponseClient,它在授权服务器的令牌端点刷新访问令牌时使用WebClient

WebClientReactiveRefreshTokenTokenResponseClient非常灵活,因为它允许您自定义令牌请求的预处理和/或令牌响应的后处理。

自定义访问令牌请求

如果您需要自定义令牌请求的预处理,您可以为WebClientReactiveRefreshTokenTokenResponseClient.setParametersConverter()提供一个自定义的Converter<OAuth2RefreshTokenGrantRequest, MultiValueMap<String, String>>。默认实现构建一个MultiValueMap<String, String>,其中只包含标准OAuth 2.0 访问令牌请求grant_type参数,用于构造请求。WebClientReactiveRefreshTokenTokenResponseClient会直接将刷新令牌授权所需的其它参数添加到请求体中。但是,提供自定义的Converter允许您扩展标准令牌请求并添加自定义参数。

如果您只想添加附加参数,您可以改为为WebClientReactiveRefreshTokenTokenResponseClient.addParametersConverter()提供一个自定义的Converter<OAuth2RefreshTokenGrantRequest, MultiValueMap<String, String>>,它将构建一个聚合Converter
自定义Converter必须返回目标 OAuth 2.0 提供商能够理解的有效的 OAuth 2.0 访问令牌请求参数。

自定义访问令牌响应

另一方面,如果您需要自定义令牌响应的后处理,则需要为WebClientReactiveRefreshTokenTokenResponseClient.setBodyExtractor()提供一个自定义配置的BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage>,用于将 OAuth 2.0 访问令牌响应转换为OAuth2AccessTokenResponseOAuth2BodyExtractors.oauth2AccessTokenResponse()提供的默认实现会解析响应并相应地处理错误。

自定义WebClient

或者,如果您的需求更高级,您可以通过简单地为WebClientReactiveRefreshTokenTokenResponseClient.setWebClient()提供一个自定义配置的WebClient来完全控制请求/响应。

无论您自定义WebClientReactiveRefreshTokenTokenResponseClient还是提供您自己的ReactiveOAuth2AccessTokenResponseClient实现,您都需要按以下示例所示进行配置:

访问令牌响应配置
  • Java

  • Kotlin

// Customize
ReactiveOAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenTokenResponseClient = ...

ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
		ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
				.authorizationCode()
				.refreshToken(configurer -> configurer.accessTokenResponseClient(refreshTokenTokenResponseClient))
				.build();

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
// Customize
val refreshTokenTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> = ...

val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
        .authorizationCode()
        .refreshToken { it.accessTokenResponseClient(refreshTokenTokenResponseClient) }
        .build()

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
ReactiveOAuth2AuthorizedClientProviderBuilder.builder().refreshToken()配置一个RefreshTokenReactiveOAuth2AuthorizedClientProvider,它是刷新令牌授权的ReactiveOAuth2AuthorizedClientProvider的实现。

OAuth2RefreshToken可以选择在authorization_codepassword授权类型的访问令牌响应中返回。如果OAuth2AuthorizedClient.getRefreshToken()可用且OAuth2AuthorizedClient.getAccessToken()已过期,则RefreshTokenReactiveOAuth2AuthorizedClientProvider将自动刷新它。

客户端凭据

请参考 OAuth 2.0 授权框架了解有关客户端凭据授权的更多详细信息。

请求访问令牌

请参考访问令牌请求/响应协议流程(针对客户端凭据授权)。

客户端凭据授权的ReactiveOAuth2AccessTokenResponseClient的默认实现是WebClientReactiveClientCredentialsTokenResponseClient,它在请求授权服务器令牌端点的访问令牌时使用WebClient

WebClientReactiveClientCredentialsTokenResponseClient非常灵活,因为它允许您自定义令牌请求的预处理和/或令牌响应的后处理。

自定义访问令牌请求

如果您需要自定义令牌请求的预处理,您可以为WebClientReactiveClientCredentialsTokenResponseClient.setParametersConverter()提供一个自定义的Converter<OAuth2ClientCredentialsGrantRequest, MultiValueMap<String, String>>。默认实现构建一个MultiValueMap<String, String>,其中只包含标准OAuth 2.0 访问令牌请求grant_type参数,用于构造请求。WebClientReactiveClientCredentialsTokenResponseClient会直接将客户端凭据授权所需的其它参数添加到请求体中。但是,提供自定义的Converter允许您扩展标准令牌请求并添加自定义参数。

如果您只想添加附加参数,您可以改为为WebClientReactiveClientCredentialsTokenResponseClient.addParametersConverter()提供一个自定义的Converter<OAuth2ClientCredentialsGrantRequest, MultiValueMap<String, String>>,它将构建一个聚合Converter
自定义Converter必须返回目标 OAuth 2.0 提供商能够理解的有效的 OAuth 2.0 访问令牌请求参数。

自定义访问令牌响应

另一方面,如果您需要自定义令牌响应的后处理,则需要为WebClientReactiveClientCredentialsTokenResponseClient.setBodyExtractor()提供一个自定义配置的BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage>,用于将 OAuth 2.0 访问令牌响应转换为OAuth2AccessTokenResponseOAuth2BodyExtractors.oauth2AccessTokenResponse()提供的默认实现会解析响应并相应地处理错误。

自定义WebClient

或者,如果您的需求更高级,您可以通过简单地为WebClientReactiveClientCredentialsTokenResponseClient.setWebClient()提供一个自定义配置的WebClient来完全控制请求/响应。

无论您自定义WebClientReactiveClientCredentialsTokenResponseClient还是提供您自己的ReactiveOAuth2AccessTokenResponseClient实现,您都需要按以下示例所示进行配置:

  • Java

  • Kotlin

// Customize
ReactiveOAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient = ...

ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
		ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
				.clientCredentials(configurer -> configurer.accessTokenResponseClient(clientCredentialsTokenResponseClient))
				.build();

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
// Customize
val clientCredentialsTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> = ...

val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
        .clientCredentials { it.accessTokenResponseClient(clientCredentialsTokenResponseClient) }
        .build()

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
ReactiveOAuth2AuthorizedClientProviderBuilder.builder().clientCredentials()配置一个ClientCredentialsReactiveOAuth2AuthorizedClientProvider,它是客户端凭据授权的ReactiveOAuth2AuthorizedClientProvider的实现。

使用访问令牌

给定OAuth 2.0客户端注册的以下Spring Boot属性

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
            authorization-grant-type: client_credentials
            scope: read, write
        provider:
          okta:
            token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token

…以及ReactiveOAuth2AuthorizedClientManager @Bean

  • Java

  • Kotlin

@Bean
public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
		ReactiveClientRegistrationRepository clientRegistrationRepository,
		ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {

	ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
			ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
					.clientCredentials()
					.build();

	DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
			new DefaultReactiveOAuth2AuthorizedClientManager(
					clientRegistrationRepository, authorizedClientRepository);
	authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

	return authorizedClientManager;
}
@Bean
fun authorizedClientManager(
        clientRegistrationRepository: ReactiveClientRegistrationRepository,
        authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
    val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
            .clientCredentials()
            .build()
    val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
            clientRegistrationRepository, authorizedClientRepository)
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
    return authorizedClientManager
}

您可以按如下方式获取OAuth2AccessToken

  • Java

  • Kotlin

@Controller
public class OAuth2ClientController {

	@Autowired
	private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;

	@GetMapping("/")
	public Mono<String> index(Authentication authentication, ServerWebExchange exchange) {
		OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
				.principal(authentication)
				.attribute(ServerWebExchange.class.getName(), exchange)
				.build();

		return this.authorizedClientManager.authorize(authorizeRequest)
				.map(OAuth2AuthorizedClient::getAccessToken)
				...
				.thenReturn("index");
	}
}
class OAuth2ClientController {

    @Autowired
    private lateinit var authorizedClientManager: ReactiveOAuth2AuthorizedClientManager

    @GetMapping("/")
    fun index(authentication: Authentication, exchange: ServerWebExchange): Mono<String> {
        val authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
                .principal(authentication)
                .attribute(ServerWebExchange::class.java.name, exchange)
                .build()

        return authorizedClientManager.authorize(authorizeRequest)
                .map { it.accessToken }
                ...
                .thenReturn("index")
    }
}
ServerWebExchange是一个可选属性。如果未提供,它将通过键ServerWebExchange.classReactor 的上下文中获取。

资源所有者密码凭据

请参考 OAuth 2.0 授权框架了解有关资源所有者密码凭据授权的更多详细信息。

请求访问令牌

请参考访问令牌请求/响应协议流程(针对资源所有者密码凭据授权)。

资源所有者密码凭据授权的ReactiveOAuth2AccessTokenResponseClient的默认实现是WebClientReactivePasswordTokenResponseClient,它在请求授权服务器令牌端点的访问令牌时使用WebClient

WebClientReactivePasswordTokenResponseClient 非常灵活,因为它允许您自定义令牌请求的预处理和/或令牌响应的后处理。

自定义访问令牌请求

如果您需要自定义令牌请求的预处理,您可以向 WebClientReactivePasswordTokenResponseClient.setParametersConverter() 提供自定义的 Converter<OAuth2PasswordGrantRequest, MultiValueMap<String, String>>。默认实现构建一个仅包含标准OAuth 2.0 访问令牌请求grant_type 参数的 MultiValueMap<String, String>,该参数用于构建请求。WebClientReactivePasswordTokenResponseClient 会直接将资源所有者密码凭据授权所需的其它参数添加到请求正文。但是,提供自定义的 Converter 可以扩展标准令牌请求并添加自定义参数。

如果您只想添加附加参数,则可以向 WebClientReactivePasswordTokenResponseClient.addParametersConverter() 提供自定义的 Converter<OAuth2PasswordGrantRequest, MultiValueMap<String, String>>,它将构建一个聚合 Converter
自定义Converter必须返回目标 OAuth 2.0 提供商能够理解的有效的 OAuth 2.0 访问令牌请求参数。

自定义访问令牌响应

另一方面,如果您需要自定义令牌响应的后处理,则需要向 WebClientReactivePasswordTokenResponseClient.setBodyExtractor() 提供一个自定义配置的 BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage>,用于将 OAuth 2.0 访问令牌响应转换为 OAuth2AccessTokenResponseOAuth2BodyExtractors.oauth2AccessTokenResponse() 提供的默认实现会解析响应并相应地处理错误。

自定义WebClient

或者,如果您的需求更高级,您可以通过简单地向 WebClientReactivePasswordTokenResponseClient.setWebClient() 提供自定义配置的 WebClient 来完全控制请求/响应。

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

  • Java

  • Kotlin

// Customize
ReactiveOAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> passwordTokenResponseClient = ...

ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
		ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
				.password(configurer -> configurer.accessTokenResponseClient(passwordTokenResponseClient))
				.refreshToken()
				.build();

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
val passwordTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> = ...

val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
        .password { it.accessTokenResponseClient(passwordTokenResponseClient) }
        .refreshToken()
        .build()

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
ReactiveOAuth2AuthorizedClientProviderBuilder.builder().password() 配置一个 PasswordReactiveOAuth2AuthorizedClientProvider,它是资源所有者密码凭据授权的 ReactiveOAuth2AuthorizedClientProvider 的一个实现。

使用访问令牌

给定OAuth 2.0客户端注册的以下Spring Boot属性

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
            authorization-grant-type: password
            scope: read, write
        provider:
          okta:
            token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token

…以及ReactiveOAuth2AuthorizedClientManager @Bean

  • Java

  • Kotlin

@Bean
public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
		ReactiveClientRegistrationRepository clientRegistrationRepository,
		ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {

	ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
			ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
					.password()
					.refreshToken()
					.build();

	DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
			new DefaultReactiveOAuth2AuthorizedClientManager(
					clientRegistrationRepository, authorizedClientRepository);
	authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

	// Assuming the `username` and `password` are supplied as `ServerHttpRequest` parameters,
	// map the `ServerHttpRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
	authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());

	return authorizedClientManager;
}

private Function<OAuth2AuthorizeRequest, Mono<Map<String, Object>>> contextAttributesMapper() {
	return authorizeRequest -> {
		Map<String, Object> contextAttributes = Collections.emptyMap();
		ServerWebExchange exchange = authorizeRequest.getAttribute(ServerWebExchange.class.getName());
		ServerHttpRequest request = exchange.getRequest();
		String username = request.getQueryParams().getFirst(OAuth2ParameterNames.USERNAME);
		String password = request.getQueryParams().getFirst(OAuth2ParameterNames.PASSWORD);
		if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
			contextAttributes = new HashMap<>();

			// `PasswordReactiveOAuth2AuthorizedClientProvider` requires both attributes
			contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
			contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
		}
		return Mono.just(contextAttributes);
	};
}
@Bean
fun authorizedClientManager(
        clientRegistrationRepository: ReactiveClientRegistrationRepository,
        authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
    val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
            .password()
            .refreshToken()
            .build()
    val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
            clientRegistrationRepository, authorizedClientRepository)
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)

    // Assuming the `username` and `password` are supplied as `ServerHttpRequest` parameters,
    // map the `ServerHttpRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
    authorizedClientManager.setContextAttributesMapper(contextAttributesMapper())
    return authorizedClientManager
}

private fun contextAttributesMapper(): Function<OAuth2AuthorizeRequest, Mono<MutableMap<String, Any>>> {
    return Function { authorizeRequest ->
        var contextAttributes: MutableMap<String, Any> = mutableMapOf()
        val exchange: ServerWebExchange = authorizeRequest.getAttribute(ServerWebExchange::class.java.name)!!
        val request: ServerHttpRequest = exchange.request
        val username: String? = request.queryParams.getFirst(OAuth2ParameterNames.USERNAME)
        val password: String? = request.queryParams.getFirst(OAuth2ParameterNames.PASSWORD)
        if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
            contextAttributes = hashMapOf()

            // `PasswordReactiveOAuth2AuthorizedClientProvider` requires both attributes
            contextAttributes[OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME] = username!!
            contextAttributes[OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME] = password!!
        }
        Mono.just(contextAttributes)
    }
}

您可以按如下方式获取OAuth2AccessToken

  • Java

  • Kotlin

@Controller
public class OAuth2ClientController {

	@Autowired
	private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;

	@GetMapping("/")
	public Mono<String> index(Authentication authentication, ServerWebExchange exchange) {
		OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
				.principal(authentication)
				.attribute(ServerWebExchange.class.getName(), exchange)
				.build();

		return this.authorizedClientManager.authorize(authorizeRequest)
				.map(OAuth2AuthorizedClient::getAccessToken)
				...
				.thenReturn("index");
	}
}
@Controller
class OAuth2ClientController {
    @Autowired
    private lateinit var authorizedClientManager: ReactiveOAuth2AuthorizedClientManager

    @GetMapping("/")
    fun index(authentication: Authentication, exchange: ServerWebExchange): Mono<String> {
        val authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
                .principal(authentication)
                .attribute(ServerWebExchange::class.java.name, exchange)
                .build()

        return authorizedClientManager.authorize(authorizeRequest)
                .map { it.accessToken }
                ...
                .thenReturn("index")
    }
}
ServerWebExchange是一个可选属性。如果未提供,它将通过键ServerWebExchange.classReactor 的上下文中获取。

JWT Bearer

有关JWT Bearer 授权的更多详细信息,请参阅 OAuth 2.0 客户端身份验证和授权授权的 JSON Web 令牌 (JWT) 配置文件。

请求访问令牌

请参阅访问令牌请求/响应协议流程,了解 JWT Bearer 授权。

JWT Bearer 授权的 ReactiveOAuth2AccessTokenResponseClient 的默认实现是 WebClientReactiveJwtBearerTokenResponseClient,它在向授权服务器的令牌端点请求访问令牌时使用 WebClient

WebClientReactiveJwtBearerTokenResponseClient 非常灵活,因为它允许您自定义令牌请求的预处理和/或令牌响应的后处理。

自定义访问令牌请求

如果您需要自定义令牌请求的预处理,您可以向 WebClientReactiveJwtBearerTokenResponseClient.setParametersConverter() 提供自定义的 Converter<JwtBearerGrantRequest, MultiValueMap<String, String>>。默认实现构建一个仅包含标准OAuth 2.0 访问令牌请求grant_type 参数的 MultiValueMap<String, String>,该参数用于构建请求。WebClientReactiveJwtBearerTokenResponseClient 会直接将 JWT Bearer 授权所需的其它参数添加到请求正文。但是,提供自定义的 Converter 可以扩展标准令牌请求并添加自定义参数。

如果您只想添加附加参数,则可以向 WebClientReactiveJwtBearerTokenResponseClient.addParametersConverter() 提供自定义的 Converter<JwtBearerGrantRequest, MultiValueMap<String, String>>,它将构建一个聚合 Converter
自定义Converter必须返回目标 OAuth 2.0 提供商能够理解的有效的 OAuth 2.0 访问令牌请求参数。

自定义访问令牌响应

另一方面,如果您需要自定义令牌响应的后处理,则需要向 WebClientReactiveJwtBearerTokenResponseClient.setBodyExtractor() 提供一个自定义配置的 BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage>,用于将 OAuth 2.0 访问令牌响应转换为 OAuth2AccessTokenResponseOAuth2BodyExtractors.oauth2AccessTokenResponse() 提供的默认实现会解析响应并相应地处理错误。

自定义WebClient

或者,如果您的需求更高级,您可以通过简单地向 WebClientReactiveJwtBearerTokenResponseClient.setWebClient() 提供自定义配置的 WebClient 来完全控制请求/响应。

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

  • Java

  • Kotlin

// Customize
ReactiveOAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerTokenResponseClient = ...

JwtBearerReactiveOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider = new JwtBearerReactiveOAuth2AuthorizedClientProvider();
jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient);

ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
		ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
				.provider(jwtBearerAuthorizedClientProvider)
				.build();

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
// Customize
val jwtBearerTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<JwtBearerGrantRequest> = ...

val jwtBearerAuthorizedClientProvider = JwtBearerReactiveOAuth2AuthorizedClientProvider()
jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient)

val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
        .provider(jwtBearerAuthorizedClientProvider)
        .build()

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)

使用访问令牌

给定OAuth 2.0客户端注册的以下Spring Boot属性

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
            authorization-grant-type: urn:ietf:params:oauth:grant-type:jwt-bearer
            scope: read
        provider:
          okta:
            token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token

…以及 OAuth2AuthorizedClientManager@Bean

  • Java

  • Kotlin

@Bean
public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
		ReactiveClientRegistrationRepository clientRegistrationRepository,
		ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {

	JwtBearerReactiveOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider =
			new JwtBearerReactiveOAuth2AuthorizedClientProvider();

	ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
			ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
					.provider(jwtBearerAuthorizedClientProvider)
					.build();

	DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
			new DefaultReactiveOAuth2AuthorizedClientManager(
					clientRegistrationRepository, authorizedClientRepository);
	authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

	return authorizedClientManager;
}
@Bean
fun authorizedClientManager(
        clientRegistrationRepository: ReactiveClientRegistrationRepository,
        authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
    val jwtBearerAuthorizedClientProvider = JwtBearerReactiveOAuth2AuthorizedClientProvider()
    val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
            .provider(jwtBearerAuthorizedClientProvider)
            .build()
    val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
            clientRegistrationRepository, authorizedClientRepository)
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
    return authorizedClientManager
}

您可以按如下方式获取OAuth2AccessToken

  • Java

  • Kotlin

@RestController
public class OAuth2ResourceServerController {

	@Autowired
	private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;

	@GetMapping("/resource")
	public Mono<String> resource(JwtAuthenticationToken jwtAuthentication, ServerWebExchange exchange) {
		OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
				.principal(jwtAuthentication)
				.build();

		return this.authorizedClientManager.authorize(authorizeRequest)
				.map(OAuth2AuthorizedClient::getAccessToken)
				...
	}
}
class OAuth2ResourceServerController {

    @Autowired
    private lateinit var authorizedClientManager: ReactiveOAuth2AuthorizedClientManager

    @GetMapping("/resource")
    fun resource(jwtAuthentication: JwtAuthenticationToken, exchange: ServerWebExchange): Mono<String> {
        val authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
                .principal(jwtAuthentication)
                .build()
        return authorizedClientManager.authorize(authorizeRequest)
                .map { it.accessToken }
                ...
    }
}
JwtBearerReactiveOAuth2AuthorizedClientProvider 默认情况下通过 OAuth2AuthorizationContext.getPrincipal().getPrincipal() 解析 Jwt 断言,因此在前面的示例中使用了 JwtAuthenticationToken
如果您需要从不同的来源解析 Jwt 断言,您可以向 JwtBearerReactiveOAuth2AuthorizedClientProvider.setJwtAssertionResolver() 提供自定义的 Function<OAuth2AuthorizationContext, Mono<Jwt>>

令牌交换

有关令牌交换授权的更多详细信息,请参阅 OAuth 2.0 令牌交换。

请求访问令牌

请参阅令牌交换请求和响应协议流程,了解令牌交换授权。

令牌交换授权的 ReactiveOAuth2AccessTokenResponseClient 的默认实现是 WebClientReactiveTokenExchangeTokenResponseClient,它在向授权服务器的令牌端点请求访问令牌时使用 WebClient

WebClientReactiveTokenExchangeTokenResponseClient 非常灵活,因为它允许您自定义令牌请求的预处理和/或令牌响应的后处理。

自定义访问令牌请求

如果您需要自定义令牌请求的预处理,您可以向 WebClientReactiveTokenExchangeTokenResponseClient.setParametersConverter() 提供自定义的 Converter<TokenExchangeGrantRequest, MultiValueMap<String, String>>。默认实现构建一个仅包含标准OAuth 2.0 访问令牌请求grant_type 参数的 MultiValueMap<String, String>,该参数用于构建请求。WebClientReactiveTokenExchangeTokenResponseClient 会直接将令牌交换授权所需的其它参数添加到请求正文。但是,提供自定义的 Converter 可以扩展标准令牌请求并添加自定义参数。

如果您只想添加附加参数,则可以向 WebClientReactiveTokenExchangeTokenResponseClient.addParametersConverter() 提供自定义的 Converter<TokenExchangeGrantRequest, MultiValueMap<String, String>>,它将构建一个聚合 Converter
自定义Converter必须返回目标 OAuth 2.0 提供商能够理解的有效的 OAuth 2.0 访问令牌请求参数。

自定义访问令牌响应

另一方面,如果您需要自定义令牌响应的后处理,则需要向 WebClientReactiveTokenExchangeTokenResponseClient.setBodyExtractor() 提供一个自定义配置的 BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage>,用于将 OAuth 2.0 访问令牌响应转换为 OAuth2AccessTokenResponseOAuth2BodyExtractors.oauth2AccessTokenResponse() 提供的默认实现会解析响应并相应地处理错误。

自定义WebClient

或者,如果您的需求更高级,您可以通过简单地向 WebClientReactiveTokenExchangeTokenResponseClient.setWebClient() 提供自定义配置的 WebClient 来完全控制请求/响应。

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

  • Java

  • Kotlin

// Customize
ReactiveOAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeTokenResponseClient = ...

TokenExchangeReactiveOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider = new TokenExchangeReactiveOAuth2AuthorizedClientProvider();
tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeTokenResponseClient);

ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
		ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
				.provider(tokenExchangeAuthorizedClientProvider)
				.build();

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
// Customize
val tokenExchangeTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> = ...

val tokenExchangeAuthorizedClientProvider = TokenExchangeReactiveOAuth2AuthorizedClientProvider()
tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeTokenResponseClient)

val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
        .provider(tokenExchangeAuthorizedClientProvider)
        .build()

...

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)

使用访问令牌

给定OAuth 2.0客户端注册的以下Spring Boot属性

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
            authorization-grant-type: urn:ietf:params:oauth:grant-type:token-exchange
            scope: read
        provider:
          okta:
            token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token

…以及 OAuth2AuthorizedClientManager@Bean

  • Java

  • Kotlin

@Bean
public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
		ReactiveClientRegistrationRepository clientRegistrationRepository,
		ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {

	TokenExchangeReactiveOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider =
			new TokenExchangeReactiveOAuth2AuthorizedClientProvider();

	ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
			ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
					.provider(tokenExchangeAuthorizedClientProvider)
					.build();

	DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
			new DefaultReactiveOAuth2AuthorizedClientManager(
					clientRegistrationRepository, authorizedClientRepository);
	authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

	return authorizedClientManager;
}
@Bean
fun authorizedClientManager(
        clientRegistrationRepository: ReactiveClientRegistrationRepository,
        authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
    val tokenExchangeAuthorizedClientProvider = TokenExchangeReactiveOAuth2AuthorizedClientProvider()
    val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
            .provider(tokenExchangeAuthorizedClientProvider)
            .build()
    val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
            clientRegistrationRepository, authorizedClientRepository)
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
    return authorizedClientManager
}

您可以按如下方式获取OAuth2AccessToken

  • Java

  • Kotlin

@RestController
public class OAuth2ResourceServerController {

	@Autowired
	private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;

	@GetMapping("/resource")
	public Mono<String> resource(JwtAuthenticationToken jwtAuthentication) {
		OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
				.principal(jwtAuthentication)
				.build();

		return this.authorizedClientManager.authorize(authorizeRequest)
				.map(OAuth2AuthorizedClient::getAccessToken)
				...
	}
}
class OAuth2ResourceServerController {

    @Autowired
    private lateinit var authorizedClientManager: ReactiveOAuth2AuthorizedClientManager

    @GetMapping("/resource")
    fun resource(jwtAuthentication: JwtAuthenticationToken): Mono<String> {
        val authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
                .principal(jwtAuthentication)
                .build()
        return authorizedClientManager.authorize(authorizeRequest)
                .map { it.accessToken }
                ...
    }
}
TokenExchangeReactiveOAuth2AuthorizedClientProvider 默认情况下通过 OAuth2AuthorizationContext.getPrincipal().getPrincipal() 解析主题令牌(作为 OAuth2Token),因此在前面的示例中使用了 JwtAuthenticationToken。默认情况下不会解析参与者令牌。
如果您需要从不同的来源解析主题令牌,您可以向 TokenExchangeReactiveOAuth2AuthorizedClientProvider.setSubjectTokenResolver() 提供自定义的 Function<OAuth2AuthorizationContext, Mono<OAuth2Token>>
如果您需要解析参与者令牌,您可以向 TokenExchangeReactiveOAuth2AuthorizedClientProvider.setActorTokenResolver() 提供自定义的 Function<OAuth2AuthorizationContext, Mono<OAuth2Token>>