Jerey Client 에서 HTTPS 사용하기
RESTful 구성시에 https 정보를 내려받는것을 진행해야 하는 경우가 있다.
이럴때 기존 Jersey Client 를 사용하였을 경우에는 정상 동작하지 않는데
이 이유는 TLS(SSL) 구간이 있기 때문이다. 이부분 처리를 위해 아래와 같은 코드를 작성하여 처리한다면 내용을 가져올 수 있게 된다.
=============================== Code ================================
// 이 옵션을 활성화 하여야 처리가 가능
System.setProperty("jsse.enableSNIExtension", "false");
ClientConfig config = new DefaultClientConfig();
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
// slf4j 사용
logger.debug("hostname={}", hostname);
return true;
}
};
try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hv, sc));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
Client client = Client.create(config);
WebResource webResource = client.resource("https://test.com/getData");
webResource.method("POST");
Form form = new Form();
form.add("param1", "value1");
form.add("param2", "value2");
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_VALUE).post(ClientResponse.class, form);
String res = response.getEntity(String.class);
// 받아온 res 로 원하는 작업 진행
이럴때 기존 Jersey Client 를 사용하였을 경우에는 정상 동작하지 않는데
이 이유는 TLS(SSL) 구간이 있기 때문이다. 이부분 처리를 위해 아래와 같은 코드를 작성하여 처리한다면 내용을 가져올 수 있게 된다.
=============================== Code ================================
// 이 옵션을 활성화 하여야 처리가 가능
System.setProperty("jsse.enableSNIExtension", "false");
ClientConfig config = new DefaultClientConfig();
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
// slf4j 사용
logger.debug("hostname={}", hostname);
return true;
}
};
try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hv, sc));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
Client client = Client.create(config);
WebResource webResource = client.resource("https://test.com/getData");
webResource.method("POST");
Form form = new Form();
form.add("param1", "value1");
form.add("param2", "value2");
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_VALUE).post(ClientResponse.class, form);
String res = response.getEntity(String.class);
// 받아온 res 로 원하는 작업 진행
댓글
댓글 쓰기