Java Spring RestTemplate 和代理身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31273236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:52:00  来源:igfitidea点击:

Spring RestTemplate and Proxy Auth

javaspringspring-mvcproxy

提问by yogiginger

I'm trying to do REST calls with Spring. As I understand, the right way to go is using RestTemplate(?). Problem is, I'm behind a proxy.

我正在尝试使用 Spring 进行 REST 调用。据我了解,正确的做法是使用RestTemplate(?)。问题是,我支持代理。

This is my code right now:

这是我现在的代码:

SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress(host, 3128);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
factory.setProxy(proxy);

RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(factory);

It seems to work, but I need to authenticate at the proxy, but how is this done? The Proxytype as well as the SimpleClientHttpRequestFactorytype don't seem to handle credentials. Without credentials, I'm getting just 407...

它似乎有效,但我需要在代理上进行身份验证,但这是如何完成的?该Proxy类型以及该SimpleClientHttpRequestFactory类型似乎不处理凭据。没有凭据,我只会收到 407 ...

采纳答案by Matthew Fontana

After quite a few different options I settled on The below code due to the ability to set the proxy for the RestTemplate at creation so I could refactor it into a separate method. Just to note it also has an additional dependency so keep that in mind.

由于能够在创建时为 RestTemplate 设置代理,因此我选择了以下代码,因此我可以将其重构为一个单独的方法。请注意,它还有一个额外的依赖项,所以请记住这一点。

private RestTemplate createRestTemplate() throws Exception {
    final String username = "myusername";
    final String password = "myPass";
    final String proxyUrl = "proxy.nyc.bigtower.com";
    final int port = 8080;

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials( 
        new AuthScope(proxyUrl, port), 
        new UsernamePasswordCredentials(username, password)
    );

    HttpHost myProxy = new HttpHost(proxyUrl, port);
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();

    HttpClient httpClient = clientBuilder.build();
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    factory.setHttpClient(httpClient);

    return new RestTemplate(factory);
}

//Dependencies I used.

//我使用的依赖项。

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>

回答by Ewerton Costa

the code below worked for me.

下面的代码对我有用。

RestTemplate restTemplate = new RestTemplate();

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( new AuthScope("proxyHost", "proxyPort"), new UsernamePasswordCredentials("proxyUser", "proxyPass") );
HttpClientBuilder clientBuilder = HttpClientBuilder.create();

clientBuilder.useSystemProperties();
clientBuilder.setProxy(new HttpHost("proxyHost", "proxyPort"));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

CloseableHttpClient client = clientBuilder.build();

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(client);

restTemplate.setRequestFactory(factory);