Some companies may have to go through a proxy
that requires authentication when connecting to the Internet.
I would like to explain how to set proxy
when calling an API published on the Internet using the API client Feign
in such an environment.
Feign
delegates the process of issuing an HTTP request to another library.
In other words, you only have to support proxy
of the library to be used.
This time, I changed Feign
's Sample to get contributor of specified repository of GitHub explained on GitHub page to support proxy
. to watch.
For the explanation of Feign
, please refer to" Feign that implements API client only with interface is very convenient! ".
GitHubDemo.java
package com.example.feign.demo.github;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import java.util.concurrent.TimeUnit;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
/**
* modify Feign example(https://github.com/OpenFeign/feign) to corresponded proxy
*/
public class GitHubDemo {
public static void main(String[] args) {
// create instance of okhttp3.OkHttpClient corresponded proxy
OkHttpClient client = createOkHttpClientCorrespondedProxy("127.0.0.1",
8080, "userId", "pass");
// feign use proxy with authentication
GitHub github = Feign.builder()
// set instance of feign.Client.OkHttpClient
.client(new feign.okhttp.OkHttpClient(client))
// use Jackson
.decoder(new JacksonDecoder())
.target(GitHub.class, "https://api.github.com");
// call api [GET /repos/{owner}/{repo}/contributors]
List<Contributor> contributors = github.contributors("OpenFeign",
"feign");
for (Contributor contributor : contributors) {
System.out.println(
contributor.login + " (" + contributor.contributions + ")");
}
}
/**
* create instance of okhttp3.OkHttpClient corresponded proxy
* @param proxyHost proxy's host url or ipAddress
* @param proxyPort proxy's port number
* @param userId userId of proxy's authentication
* @param pass pass of proxy's authentication
* @return instance of okhttp3.OkHttpClient
*/
private static OkHttpClient createOkHttpClientCorrespondedProxy(
String proxyHost, int proxyPort, String userId, String pass) {
// okhttp3.Authenticator : correct
// java.net.Authenticator : incorrect
Authenticator proxyAuthenticator = new Authenticator() {
@Override
public Request authenticate(Route route,
Response response) throws IOException {
String credential = Credentials.basic(userId, pass);
return response.request().newBuilder()
.header("Proxy-Authorization", credential).build();
}
};
// okhttp3.OkHttpClient : correct
// feign.Client.OkHttpClient : incorrect
return new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.proxy(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort)))
.proxyAuthenticator(proxyAuthenticator).build();
}
}
The points to note are shown below.
feign.okhttp.OkHttpClient
, use a constructor that takes ʻokhttp3.OkHttpClient` as an argument., configure it to correspond to
proxy`.is correct,
java.net.Authenticator` is incorrectis correct,
feign.Client.OkHttpClient` is incorrect (creates a proxy-enabled okhttp instance)(reference) https://stackoverflow.com/questions/35554380/okhttpclient-proxy-authentication-how-to
Recommended Posts