首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot OAuth资源服务器代理配置

Spring Boot OAuth资源服务器代理配置
EN

Stack Overflow用户
提问于 2019-11-28 14:24:48
回答 1查看 777关注 0票数 1

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

代码语言:javascript
复制
@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和proxy的组合吗?

EN

回答 1

Stack Overflow用户

发布于 2019-11-28 16:51:41

我自己发现,提供一个带有正确配置的webclient的NimbusReactiveJwtDecoder bean可以解决这个问题。

代码语言:javascript
复制
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();
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59082870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档