gpt4 book ai didi

spring - 如何在 spring oauth2 OAuth2AccessToken 请求或如何覆盖 OAuth2AccessTokenSupport restTemplate 变量上设置代理?

转载 作者:行者123 更新时间:2023-12-04 19:41:58 26 4
gpt4 key购买 nike

我尝试通过以下方式设置网络代理,但没有一种方法有效

1:设置jvm变量,如 -Dhttp.proxyHost= -Dhttp.proxyPort= ......
2:创建Bean。

@Bean
public RestTemplate restTemplate() {
final String proxyHost = "######"; // host
final int proxyPort = ####; // port
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setProxy(new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
return new RestTemplate(factory);
}

但是这个配置被 OAuth2AccessTokenSupport.restTemplate 覆盖。

所以下面的方法总是返回新创建的休息模板对象。

org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport
protected RestOperations getRestTemplate() {
if (restTemplate == null) {
synchronized (this) {
if (restTemplate == null) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(getResponseErrorHandler());
restTemplate.setRequestFactory(requestFactory);
restTemplate.setInterceptors(interceptors);
this.restTemplate = restTemplate;
}
}
}
if (messageConverters == null) {
setMessageConverters(new RestTemplate().getMessageConverters());
}
return restTemplate;
}

请帮助我在 OAuth 客户端应用程序的其余模板上覆盖或设置代理。

最佳答案

另一种方法是将自定义 AccessTokenProvider 设置为您的 OAuth2RestTemplate。在下面的代码示例中,绕过了 SSL 验证:

@Configuration
public class ConfigLocal {

@Value("${https.proxyHost}")
private String proxyHost;

@Value("${https.proxyPort}")
private Integer proxyPort;

@Value("${https.proxyUser}")
private String proxyUser;

@Value("${https.proxyPassword}")
private String proxyPassword;

@Bean
public OAuth2RestTemplate oauth2RestTemplate(ClientCredentialsResourceDetails clientCredentialsResourceDetails)
throws KeyManagementException, KeyStoreException, NoSuchAlgorithmException {
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(clientCredentialsResourceDetails);

// Instanciate a new http client with proxy configuration, and bypass SSL Certificate verification
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPassword));

HttpClientBuilder httpClientBuilder =
HttpClients.custom()
.setProxy(new HttpHost(proxyHost, proxyPort))
.setDefaultCredentialsProvider(credentialsProvider)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true)
.build());

// requestFactory
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
ClientCredentialsAccessTokenProvider clientCredentialsAccessTokenProvider = new ClientCredentialsAccessTokenProvider();
clientCredentialsAccessTokenProvider.setRequestFactory(requestFactory);

// accessTokenProvider
AccessTokenProvider accessTokenProvider = new AccessTokenProviderChain(Arrays.<AccessTokenProvider> asList(
new AuthorizationCodeAccessTokenProvider(), new ImplicitAccessTokenProvider(),
new ResourceOwnerPasswordAccessTokenProvider(), clientCredentialsAccessTokenProvider));

restTemplate.setAccessTokenProvider(accessTokenProvider);

return restTemplate;
}
}

关于spring - 如何在 spring oauth2 OAuth2AccessToken 请求或如何覆盖 OAuth2AccessTokenSupport restTemplate 变量上设置代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com