1

我目前正在努力将代理与 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 和代理工作的组合?

4

1 回答 1

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-11-28T08:51:41.130 回答