HTTP 服務客戶端整合

Spring Security 的 OAuth 支援可以與 RestClientWebClient HTTP 服務客戶端 整合。

配置

在完成 RestClientWebClient 特定配置後,使用 HTTP 服務客戶端整合 只需要在需要 OAuth 的方法或其宣告的 HTTP 介面上新增 @ClientRegistrationId

由於 @ClientRegistrationId 的存在決定了 OAuth 令牌是否以及如何解析,因此將 Spring Security 的 OAuth 支援新增到任何配置都是安全的。

RestClient 配置

Spring Security 的 OAuth 支援可以與由 RestClient 支援的 HTTP 服務客戶端 整合。第一步是 建立一個 OAuthAuthorizedClientManager Bean

接下來,您必須配置 HttpServiceProxyFactoryRestClient 以感知 @ClientRegistrationId。為簡化此配置,請使用 OAuth2RestClientHttpServiceGroupConfigurer

  • Java

  • Kotlin

@Bean
OAuth2RestClientHttpServiceGroupConfigurer securityConfigurer(
		OAuth2AuthorizedClientManager manager) {
	return OAuth2RestClientHttpServiceGroupConfigurer.from(manager);
}
@Bean
fun securityConfigurer(manager: OAuth2AuthorizedClientManager): OAuth2RestClientHttpServiceGroupConfigurer {
    return OAuth2RestClientHttpServiceGroupConfigurer.from(manager)
}

此配置

WebClient 配置

Spring Security 的 OAuth 支援可以與由 WebClient 支援的 HTTP 服務客戶端 整合。第一步是 建立一個 ReactiveOAuthAuthorizedClientManager Bean

接下來,您必須配置 HttpServiceProxyFactoryWebRestClient 以感知 @ClientRegistrationId。為簡化此配置,請使用 OAuth2WebClientHttpServiceGroupConfigurer

  • Java

  • Kotlin

@Bean
OAuth2WebClientHttpServiceGroupConfigurer securityConfigurer(
		ReactiveOAuth2AuthorizedClientManager manager) {
	return OAuth2WebClientHttpServiceGroupConfigurer.from(manager);
}
@Bean
fun securityConfigurer(
    manager: ReactiveOAuth2AuthorizedClientManager?
): OAuth2WebClientHttpServiceGroupConfigurer {
    return OAuth2WebClientHttpServiceGroupConfigurer.from(manager)
}

此配置

@ClientRegistrationId

您可以在 HTTP 服務上新增 ClientRegistrationId 以指定要使用的 ClientRegistration

  • Java

  • Kotlin

	@GetExchange("/user")
	@ClientRegistrationId("github")
	User getAuthenticatedUser();
    @GetExchange("/user")
    @ClientRegistrationId("github")
    fun getAuthenticatedUser() : User

型別級別宣告

@ClientRegistrationId 也可以在型別級別新增,以避免在每個方法上重複宣告。

  • Java

  • Kotlin

@HttpExchange
@ClientRegistrationId("github")
public interface UserService {

	@GetExchange("/user")
	User getAuthenticatedUser();

	@GetExchange("/users/{username}/hovercard")
	Hovercard getHovercard(@PathVariable String username);

}
@HttpExchange
@ClientRegistrationId("github")
interface UserService {
    @GetExchange("/user")
    fun getAuthenticatedUser(): User

    @GetExchange("/users/{username}/hovercard")
    fun getHovercard(@PathVariable username: String): Hovercard
}

ClientRegistrationIdProcessor

然後,id 由以下元件處理:

© . This site is unofficial and not affiliated with VMware.