【问题标题】:Spring Boot OAuth Ressource Server Proxy ConfigurationSpring Boot OAuth 资源服务器代理配置
【发布时间】:2020-03-23 18:10:45
【问题描述】:

我目前正在努力将代理与 Spring-Webflux 结合使用。在其他服务中,我总是遵循这种方法,效果很好(代理配置是从标准环境变量中检索的):

@Bean
public RestTemplate restTemplate() {
    final RestTemplate restTemplate = new RestTemplate();
    final CloseableHttpClient client = HttpClientBuilder.create().useSystemProperties().build();
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(client));
    return restTemplate;
  }

但现在我正在尝试使用 Spring Oauth-Resource-Server 包设置 OAuth-Ressource-Server。这个包使用 Spring-Webflux for HTTP(S)。该服务现在尝试从给定的 uri(需要代理)获取 jwk-set,但由于连接被拒绝错误而失败。有没有人得到 Spring-Webflux/OAuth-Ressource 和代理工作的组合?

【问题讨论】:

    标签: java spring spring-boot spring-security-oauth2 spring-reactive


    【解决方案1】:

    我自己发现,为 NimbusReactiveJwtDecoder bean 提供正确配置的 Web 客户端可以解决问题。

    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.client.reactive.ReactorClientHttpConnector;
    import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder;
    import org.springframework.stereotype.Component;
    import org.springframework.web.reactive.function.client.WebClient;
    import reactor.netty.http.client.HttpClient;
    import reactor.netty.tcp.ProxyProvider;
    
    @Data
    @Component
    @Configuration
    @ConfigurationProperties(value = "proxy")
    public class ProxyConfig {
    
      private String host;
      private int port;
      private String username;
      private String password;
    
      @Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
      private String jwkSetUri;
    
      @Bean
      public WebClient webClient(ReactorClientHttpConnector reactorClientHttpConnector) {
        return WebClient.builder().clientConnector(reactorClientHttpConnector).build();
      }
    
      @Bean
      public HttpClient httpClient() {
        return HttpClient.create()
            .tcpConfiguration(tcpClient ->
                tcpClient.proxy(
                    proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host(host)
                        .port(port).username(username)
                        .password(s -> password)));
      }
    
      @Bean
      ReactorClientHttpConnector reactorClientHttpConnector(HttpClient httpClient) {
        return new ReactorClientHttpConnector(httpClient);
      }
    
      @Bean
      public NimbusReactiveJwtDecoder nimbusReactiveJwtDecoder(WebClient webClient) {
        return NimbusReactiveJwtDecoder
            .withJwkSetUri(jwkSetUri)
            .webClient(webClient).build();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 2016-01-08
      • 2018-07-26
      • 2017-02-13
      • 2019-04-29
      • 1970-01-01
      • 2016-01-18
      • 2017-09-13
      • 2014-12-20
      相关资源
      最近更新 更多