postforobject 设置代理_OAuth2RestTemplate中的代理配置
我需要使用由OAuth2保護(hù)的API.為此我使用OAuth2RestTemplate.
但我得到以下錯(cuò)誤:
java.net.ConnectException: Connection timed out: connect
由于代理問(wèn)題,這種情況正在發(fā)生.我知道如何在RestTemplate中設(shè)置代理:
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("Proxy host", 8080));
clientHttpRequestFactory.setProxy(代理);
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
我嘗試為OAuth2RestTemplate設(shè)置的方式相同:
@Bean
public OAuth2RestOperations restTemplate(OAuth2ClientContext oauth2ClientContext) {
OAuth2RestTemplate client = new OAuth2RestTemplate(resource(), oauth2ClientContext);
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
clientHttpRequestFactory.setProxy(proxy);
client.setRequestFactory(clientHttpRequestFactory);
return client;
}
但它不起作用并給出“連接超時(shí)”例外.這是因?yàn)榈谝恍蠴Auth2RestTemplate客戶(hù)端=新OAuth2RestTemplate(resource(),oauth2ClientContext);它試圖獲取Access令牌,這意味著它還需要代理設(shè)置.如果我添加以下行,那么它的工作原理:
System.setProperty("https.proxyHost", "urproxy.com");
System.setProperty("https.proxyPort", "8080");
但我不能使用System.setProperties(“”,“”)選項(xiàng),因?yàn)槲覀儧](méi)有權(quán)限在tomcat服務(wù)器上設(shè)置.
我研究過(guò)但在創(chuàng)建此對(duì)象時(shí)找不到任何在OAuth2RestTemplate中設(shè)置代理的方法.
任何幫助,將不勝感激.謝謝
總結(jié)
以上是生活随笔為你收集整理的postforobject 设置代理_OAuth2RestTemplate中的代理配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 快手评论通知怎么开的
- 下一篇: k8s 命令 重启_k8s 常用命令