简体   繁体   English

Spring Reactive Oauth2 Webclient 不使用配置的代理

[英]Spring Reactive Oauth2 Webclient not using configured proxy

I have an Oauth2 authentication service that must use proxy to call the OAuth provider to get token after user authentication.我有一个 Oauth2 身份验证服务,必须使用代理调用 OAuth 提供程序以在用户身份验证后获取令牌。 The server used here is.netty while i have a reactive server for gateway reasons.此处使用的服务器是 .netty,而出于网关原因我有一个反应式服务器。

This is the configuration that i am using:这是我正在使用的配置:

@Configuration
public class GithubProxyConfig {

    private static final Logger LOGGER = Logger.getLogger(GithubProxyConfig.class);

    @Bean("githubClientRegistrationRepository")
    public ReactiveClientRegistrationRepository githubClientRegistrationRepository() {
        ClientRegistration registration = ClientRegistration
                .withRegistrationId("github")
                .clientId("ID")
                .clientSecret("SECRET")
                .redirectUri("https://oauth-service/api/login/oauth2/code/github")
                .authorizationUri("https://github.com/login/oauth/authorize")
                .tokenUri("https://github.com/login/oauth/access_token")
                .userInfoUri("https://api.github.com/user")
                .userNameAttributeName("login")
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .build();
        return new InMemoryReactiveClientRegistrationRepository(registration);
    }

    @Primary
    @Bean
    @DependsOn(value = {"githubClientRegistrationRepository"})
    public AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientServiceReactiveOAuth2AuthorizedClientManager(
            @Qualifier("githubClientRegistrationRepository") ReactiveClientRegistrationRepository clientRegistrations,
            WebClientBuilderFactory webClientBuilderFactory
    ) throws SSLException {
        WebClient webClient = webClientBuilderFactory
                .newBuilder(LOGGER, "Github Client")
                .clientConnector(sslConnectorFrom("60.32.59.68", 8080))
                .build();
        InMemoryReactiveOAuth2AuthorizedClientService authorizedClientService = new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrations);
        AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager =
                new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistrations, authorizedClientService);
        authorizedClientManager.setAuthorizedClientProvider(createAuthorizedClientProvider(webClient));
        ServerOAuth2AuthorizedClientExchangeFilterFunction oauth2FilterFunction = new ServerOAuth2AuthorizedClientExchangeFilterFunction(
                authorizedClientManager
        );
        oauth2FilterFunction.setDefaultClientRegistrationId("github");
        return authorizedClientManager;
    }

    private ReactiveOAuth2AuthorizedClientProvider createAuthorizedClientProvider(WebClient webClient) {
        WebClientReactiveClientCredentialsTokenResponseClient clientCredentialsTokenResponseClient
                = new WebClientReactiveClientCredentialsTokenResponseClient();
        clientCredentialsTokenResponseClient.setWebClient(webClient);

        return ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
                .clientCredentials(builder -> builder.accessTokenResponseClient(clientCredentialsTokenResponseClient))
                .build();
    }

}

When i start the flow, no proxy is used and even the WebClient is not used to get access token.当我启动流程时,没有使用代理,甚至没有使用 WebClient 来获取访问令牌。 And i get a timeout exception for that.我得到了一个超时异常。 The same issue was discussed in Github: https://github.com/spring-projects/spring-security/issues/8966 Github讨论了同样的问题: https://github.com/spring-projects/spring-security/issues/8966

Any help for the resolution of this issue to make the use of the proxy for this client.解决此问题以为此客户端使用代理的任何帮助。 Thank you谢谢

I had a problem in my gateway with reactive oauth2 not honoring standard Java proxy settings too, but in my case it was for JWT decoding.我在我的网关中遇到了问题,反应性 oauth2 也不遵守标准 Java 代理设置,但在我的情况下,它是针对 JWT 解码的。 In that case, I had to end up building a new decoder with an instantiated WebClient by using the WebClient builder with a new client connector that has a specific proxy override.在那种情况下,我不得不通过将 WebClient 构建器与具有特定代理覆盖的新客户端连接器一起使用,最终构建一个带有实例化 WebClient 的新解码器。

In your situation, you may want to try similar.在您的情况下,您可能想尝试类似的方法。 Again, not the exact same situation, but here is how I overrode the WebClient in a decoder example.同样,情况不完全相同,但这是我在解码器示例中覆盖 WebClient 的方式。

    ReactiveJwtDecoder customDecoder() {

        HttpClient httpClient =
                HttpClient.create()
                        .proxy(proxy -> proxy
                                .type(ProxyProvider.Proxy.HTTP)
                                .host(proxyHost)
                                .port(proxyPort));

        ReactorClientHttpConnector conn = new ReactorClientHttpConnector(httpClient);

        final NimbusReactiveJwtDecoder userTokenDecoder = NimbusReactiveJwtDecoder.withJwkSetUri(this.jwkSetUri)
                .webClient(WebClient.builder().clientConnector(conn).build()).build();
...
...
...

        return userTokenDecoder;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM