java—如何处理oauth webclient异常

vwoqyblh  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(1585)

我对spring还很陌生,在oauth对服务器进行身份验证时,我一直在尝试捕获未经授权的异常。我不明白为什么handleresponseerror()方法没有捕获异常。
我得到的线索是:

org.springframework.security.oauth2.client.ClientAuthorizationException: [invalid_client] Client authentication failed
    at org.springframework.security.oauth2.client.ClientCredentialsReactiveOAuth2AuthorizedClientProvider.lambda$authorize$0(ClientCredentialsReactiveOAuth2AuthorizedClientProvider.java:82)
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ Request to GET https://localhost:8181/catalog/NL/brands?language_code=nl [DefaultWebClient]
Stack trace:
        at org.springframework.security.oauth2.client.ClientCredentialsReactiveOAuth2AuthorizedClientProvider.lambda$authorize$0(ClientCredentialsReactiveOAuth2AuthorizedClientProvider.java:82)
        at reactor.core.publisher.Mono.lambda$onErrorMap$29(Mono.java:3272)
        at reactor.core.publisher.Mono.lambda$onErrorResume$31(Mono.java:3362)
        at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:88)
        at reactor.core.publisher.FluxHide$SuppressFuseableSubscriber.onError(FluxHide.java:132)
        at reactor.core.publisher.MonoFlatMap$FlatMapMain.secondError(MonoFlatMap.java:185)
        at reactor.core.publisher.MonoFlatMap$FlatMapInner.onError(MonoFlatMap.java:251)
        at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onError(FluxMapFuseable.java:134)
        at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onError(FluxMapFuseable.java:134)
        at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:135)
        at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:114)
        at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:67)
        at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:73)
        at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1782)
        at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:144)

要向服务器进行身份验证的代码:

@Bean
  public WebClient myClient() {
    InMemoryReactiveClientRegistrationRepository clientRegistryRepo = new InMemoryReactiveClientRegistrationRepository(getClientRegistration());
    InMemoryReactiveOAuth2AuthorizedClientService clientService = new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistryRepo);
    AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistryRepo, clientService);
    ServerOAuth2AuthorizedClientExchangeFilterFunction oauthFilter = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);

    oauthFilter.setDefaultClientRegistrationId(OAUTH_PROVIDER_NAME);

    return WebClient.builder()
        .clientConnector(new JettyClientHttpConnector(createHttpClient()))
        .exchangeStrategies(getMaxMessageInMemorySize(maxInMemorySize))
        .baseUrl(baseURL)
        .filter(oauthFilter)
        .filter(handleResponseError())
        .build();
  }

  private static ExchangeFilterFunction handleResponseError() {
    return ExchangeFilterFunction.ofResponseProcessor(
        response -> response.statusCode().isError() ?
            response.bodyToMono(String.class)
                .flatMap(errorBody -> Mono.error(new MyUnAuthorizedRequestException(response.statusCode().name(), errorBody, ""))) :
            Mono.just(response));
  }

我看了各种各样的例子:
如何在SpringWebFlux中的webclient示例化期间设置一次访问令牌?
https://www.baeldung.com/spring-webclient-oauth2
我使用@controlleradvice捕获所有其他异常。这是正确的处理方法吗?

svujldwt

svujldwt1#

我找到了解决办法:

.... omitted
 ServerOAuth2AuthorizedClientExchangeFilterFunction oauthFilter = new 
 ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
    oauthFilter.setDefaultClientRegistrationId(OAUTH_PROVIDER_NAME);

    return WebClient.builder()
        .clientConnector(new JettyClientHttpConnector(createHttpClient()))
        .exchangeStrategies(getMaxMessageInMemorySize(maxInMemorySize))
        .baseUrl(baseURL)
        .filter(oauthFilter)
        .build();
  }

  private ReactiveOAuth2AuthorizationFailureHandler getReactiveOAuth2AuthorizationFailureHandler() {
    final ReactiveOAuth2AuthorizationFailureHandler reactiveOAuth2AuthorizationFailureHandler = (authorizationException, principal, attributes) -> {
      if (authorizationException instanceof ClientAuthorizationException) {
        ClientAuthorizationException clientAuthorizationException = (ClientAuthorizationException)authorizationException;
        return Mono.error(new MyUnAuthorizedRequestException("401","Could not authorize client", clientAuthorizationException.getMessage()));
      } else {
        return Mono.empty();
      }
    };
    return reactiveOAuth2AuthorizationFailureHandler;
  }

相关问题