gpt4 book ai didi

spring - 我应该如何使用 Spring WebClient 代表另一个用户以非交互方式访问受 OAuth 保护的资源?

转载 作者:行者123 更新时间:2023-12-04 14:49:28 25 4
gpt4 key购买 nike

我有一个 Spring(不是 Boot)应用程序,它必须代表我们的用户以非交互方式(在计划任务中)访问一些第 3 方资源。这些资源使用 OAuth 2.0 进行授权。我们已经有了一个工作流程,可以让我们获得所需的 token ,并且正在使用 Spring Social 或我们自己的实现来访问资源,这两者都不是最佳的(Spring Social 似乎没有得到维护,我们宁愿使用库也不愿维护我们的 OAuth“框架”)。

我正在尝试使用 Spring Security 5.1 中的 WebClient,但我不确定我是否正确使用了它。

WebClient 是这样创建的:

final ClientRegistration 3rdParty = 3rdParty();

final ReactiveClientRegistrationRepository clientRegistrationRepository =
new InMemoryReactiveClientRegistrationRepository(3rdParty);

final ReactiveOAuth2AuthorizedClientService authorizedClientService =
new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrationRepository);

final ServerOAuth2AuthorizedClientRepository authorizedClientRepository =
new AuthenticatedPrincipalServerOAuth2AuthorizedClientRepository(authorizedClientService);

final ServerOAuth2AuthorizedClientExchangeFilterFunction autorizedClientExchangeFilterFunction =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrationRepository, authorizedClientRepository);

return WebClient.builder()
.filter(autorizedClientExchangeFilterFunction)
.build();

并且以这种方式访问​​资源是可行的:

final OAuth2AuthorizedClient oAuth2AuthorizedClient = ... // (OAuth2AuthorizedClient with OAuth2AccessToken)

final Mono<SomeResource> someResourceMono = webClient().get()
.uri(3rdpartyUrl)
.attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient(oAuth2AuthorizedClient))
.retrieve()
.bodyToMono(SomeResource.class);

问题是我看不到 ReactiveClientRegistrationRepositoryServerOAuth2AuthorizedClientRepository 是如何在这种方法中使用的。如果我必须创建一个完全填充的 OAuth2AuthorizedClient 来访问资源,为什么需要这些存储库?

我预计,我必须传递 clientRegistrationId,一些“principalName”,实现我们的 ReactiveOAuth2AuthorizedClientService 通过“principalName”加载 OAuth2AuthorizedClient 并让 ServerOAuth2AuthorizedClientRepository 完成其工作,但我看到将委托(delegate)人传递给 WebClient 的唯一方法是使用 ServerOAuth2AuthorizedClientExchangeFilterFunction#oauth2AuthorizedClient,这需要完整的 OAuth2AuthorizedClient。我哪里做错了?

最佳答案

除了通过 oauth2AuthorizedClient() 提供 OAuth2AuthorizedClient,您还可以通过 clientRegistrationId() 提供 clientRegistrationId > 和 ServerWebExchange 通过 serverWebExchange()。后两个选项的组合将从 ServerWebExchange 解析 Principal 并使用 ReactiveClientRegistrationRepositoryServerOAuth2AuthorizedClientRepository 来解析OAuth2AuthorizedClient。我知道你的用例有点不同,因为你在请求上下文之外运行 - 这只是一个 FYI。

...The problem is I don't see how the ReactiveClientRegistrationRepository and ServerOAuth2AuthorizedClientRepository are used in this approach

你还需要提供ReactiveClientRegistrationRepositoryServerOAuth2AuthorizedClientRepository作为ServerOAuth2AuthorizedClientExchangeFilterFunction支持刷新(authorization_code客户端)和更新(client_credentials 客户端)过期的访问 token 。

对于您的特定用例,请查看 UnAuthenticatedServerOAuth2AuthorizedClientRepository,因为此实现支持在请求上下文之外运行的 WebClient,例如后台线程。这是一个sample供大家引用。

关于spring - 我应该如何使用 Spring WebClient 代表另一个用户以非交互方式访问受 OAuth 保护的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55340549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com