我需要使用由OAuth2保护的API.为此我使用OAuth2RestTemplate.
但我得到以下错误:
java.net.ConnectException: Connection timed out: connect
由于代理问题,这种情况正在发生.我知道如何在RestTemplate中设置代理:
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("Proxy host", 8080));
clientHttpRequestFactory.setProxy(代理);
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
我尝试为OAuth2RestTemplate设置的方式相同:
@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;
}
但它不起作用并给出“连接超时”例外.这是因为第一行OAuth2RestTemplate客户端=新OAuth2RestTemplate(resource(),oauth2ClientContext);它试图获取Access令牌,这意味着它还需要代理设置.如果我添加以下行,那么它的工作原理:
System.setProperty("https.proxyHost", "urproxy.com");
System.setProperty("https.proxyPort", "8080");
但我不能使用System.setProperties(“”,“”)选项,因为我们没有权限在tomcat服务器上设置.
我研究过但在创建此对象时找不到任何在OAuth2RestTemplate中设置代理的方法.
任何帮助,将不胜感激.谢谢