简体   繁体   English

Spring RestTemplate 和代理身份验证

[英]Spring RestTemplate and Proxy Auth

I'm trying to do REST calls with Spring.我正在尝试使用 Spring 进行 REST 调用。 As I understand, the right way to go is using RestTemplate (?).据我了解,正确的做法是使用RestTemplate (?)。 Problem is, I'm behind a proxy.问题是,我支持代理。

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 Proxy type as well as the SimpleClientHttpRequestFactory type don't seem to handle credentials. Proxy类型以及SimpleClientHttpRequestFactory类型似乎不处理凭据。 Without credentials, I'm getting just 407...没有凭据,我只会收到 407 ...

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.由于能够在创建时为 RestTemplate 设置代理,因此我选择了以下代码,因此我可以将其重构为一个单独的方法。 Just to note it also has an additional dependency so keep that in mind.请注意,它还有一个额外的依赖项,所以请记住这一点。

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>

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);

If your proxy require basic auth, you can simply set the HTTP header Proxy-Authorization to handle authentication:如果您的代理需要基本身份验证,您可以简单地设置 HTTP 标头Proxy-Authorization来处理身份验证:

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

final String username = "Aladdin";
final String password = "open sesame";

final RestTemplate restTemplate = new RestTemplateBuilder()
    .requestFactory(() -> factory)
    .defaultHeader(HttpHeaders.PROXY_AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString((username + ':' + password).getBytes(StandardCharsets.UTF_8)))
    .build();

You don't need to use an additional dependency for that.您不需要为此使用额外的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM