我目前正在努力将代理与 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。该包使用 HTTP(S) 的 Spring-Webflux。该服务现在尝试从给定的 uri(需要代理)获取 jwk-set,但由于连接拒绝错误而失败。有人得到了 Spring-Webflux/OAuth-Ressource 和代理的组合吗?
最佳答案
我自己发现,提供一个带有正确配置的 webclient 的 NimbusReactiveJwtDecoder bean 可以解决这个问题。
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();
}
}
关于java - Spring Boot OAuth 资源服务器代理配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59082870/