Spring Security 5 在 Application Runner 中调用 OAuth2 Secure api 会导致 IllegalArgumentException

给定以下代码,是否可以在应用程序运行程序中调用客户端凭据安全 API?

@Bean
public ApplicationRunner test(
    WebClient.Builder builder,
    ClientRegistrationRepository clientRegistrationRepo, 
    OAuth2AuthorizedClientRepository authorizedClient) {
        return args -> {
            try {
                var oauth2 =
                    new ServletOAuth2AuthorizedClientExchangeFilterFunction(
                        clientRegistrationRepo,
                        authorizedClient);
                oauth2.setDefaultClientRegistrationId("test");
                var response = builder
                    .apply(oauth2.oauth2Configuration())
                    .build()
                    .get()
                    .uri("test")
                    .retrieve()
                    .bodyToMono(String.class)
                    .block();
                log.info("Response - {}", response);
            } catch (Exception e) {
                log.error("Failed to call test.", e);
            }
        };
    }

代码由于以下原因而失败:

java.lang.IllegalArgumentException: request cannot be null

全栈,

java.lang.IllegalArgumentException: request cannot be null
    at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository.loadAuthorizedClient(HttpSessionOAuth2AuthorizedClientRepository.java:47) ~[spring-security-oauth2-client-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.populateDefaultOAuth2AuthorizedClient(ServletOAuth2AuthorizedClientExchangeFilterFunction.java:364) ~[spring-security-oauth2-client-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.lambda$null$2(ServletOAuth2AuthorizedClientExchangeFilterFunction.java:209) ~[spring-security-oauth2-client-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.reactive.function.client.DefaultWebClient$DefaultRequestBodyUriSpec.attributes(DefaultWebClient.java:234) ~[spring-webflux-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.reactive.function.client.DefaultWebClient$DefaultRequestBodyUriSpec.attributes(DefaultWebClient.java:153) ~[spring-webflux-5.1.5.RELEASE.jar:5.1.5.RELEASE]

失败的方法看起来像这样,

public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(
    String clientRegistrationId,  Authentication principal, HttpServletRequest request){

    Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty");
    Assert.notNull(request, "request cannot be null");
    return (OAuth2AuthorizedClient)this
        .getAuthorizedClients(request)
        .get(clientRegistrationId);
}

这是有道理的,因为它不能使用它,它在应用程序启动时被调用。HttpServletRequest

除了制作我自己的no-op之外,还有什么解决方法吗?OAuth2AuthorizedClientRepository

编辑

这不是一个完全反应式的堆栈。它是一个Spring Web堆栈,其中使用了WebClient。

我很清楚哪些适用于完全反应式堆栈,并且需要并且由于这是在Servlet堆栈之上构建的应用程序中而不是反应式的应用程序中而不可用的。ServerOAuth2AuthorizedClientExchangeFilterFunctionReactiveClientRegistrationRepositoryReactiveOauth2AuthorizedClient


答案 1

由于我也偶然发现了这个问题,我将详细阐述一下Darren Forsythe的更新答案,以便其他人更容易找到:

OP提交的问题导致执行了能够OAuth2AuthorizedClientManager

在 HttpServletRequest 上下文之外运行,例如在调度/后台线程和/或服务层中

(来自官方文档)

所述实现中,将 ,传递给 以替换默认的。AuthorizedClientServiceOAuth2AuthorizedClientManagerServletOAuth2AuthorizedClientExchangeFilterFunction

在我的示例中,这看起来像这样:

@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
        ClientRegistrationRepository clientRegistrationRepository,
        OAuth2AuthorizedClientService clientService)
{

    OAuth2AuthorizedClientProvider authorizedClientProvider = 
        OAuth2AuthorizedClientProviderBuilder.builder()
            .clientCredentials()
            .build();

    AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager = 
        new AuthorizedClientServiceOAuth2AuthorizedClientManager(
            clientRegistrationRepository, clientService);
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

    return authorizedClientManager;
}

@Bean
WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager)
{
    ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 =
        new ServletOAuth2AuthorizedClientExchangeFilterFunction(
            authorizedClientManager);
    oauth2.setDefaultClientRegistrationId("keycloak");
    return WebClient.builder().apply(oauth2.oauth2Configuration()).build();
}

答案 2

我最终向Spring Security团队询问了这个问题,

https://github.com/spring-projects/spring-security/issues/6683

不幸的是,如果你在servlet堆栈上,并在后台线程中使用纯Spring Security 5 API调用OAuth2资源,那么就没有可用的资源。OAuth2AuthorizedClientRepository

实际上有两种选择,

  1. 实现一个完全无操作的版本,
 var oauth2 = new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrationRepo,
                        new OAuth2AuthorizedClientRepository() {
                            @Override
                            public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String s,
                                    Authentication authentication, HttpServletRequest httpServletRequest) {
                                return null;
                            }

                            @Override
                            public void saveAuthorizedClient(OAuth2AuthorizedClient oAuth2AuthorizedClient,
                                    Authentication authentication, HttpServletRequest httpServletRequest,
                                    HttpServletResponse httpServletResponse) {

                            }

                            @Override
                            public void removeAuthorizedClient(String s, Authentication authentication,
                                    HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

                            }
                        });
  1. 实现 的 Servlet 版本。UnAuthenticatedServerOAuth2AuthorizedClientRepository GitHub Source,它具有一些比纯no-op基本功能。UnAuthenticatedServerOAuth2AuthorizedClientRepository

提供有关 GitHub 问题的反馈可能有助于 Spring Security 团队评估接受 PR 并维护 Servlet 版本的UnAuthenticatedServerOAuth2AuthorizedClientRepository

我联系了Spring Security Team Spring Security Issue 6683,并在Spring Security 5.2中添加了Servlet版本,以便在非http线程上使用。ServerOAuth2AuthorizedClientExchangeFilterFunction


推荐