Skip to content

Commit 4b9bb00

Browse files
committed
Allowed to disable ssl verification
1 parent 1ad6433 commit 4b9bb00

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/main/java/org/javawebstack/httpclient/HTTPClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class HTTPClient {
2222
private BeforeRequestInterceptor beforeInterceptor;
2323

2424
private boolean debug = false;
25+
private boolean sslVerification = true;
2526

2627
public HTTPClient(String baseUrl) {
2728
this.baseUrl = baseUrl;
@@ -37,6 +38,14 @@ public boolean isDebug(){
3738
return debug;
3839
}
3940

41+
public void setSSLVerification(boolean sslVerification){
42+
this.sslVerification = sslVerification;
43+
}
44+
45+
public boolean isSSLVerification(){
46+
return this.sslVerification;
47+
}
48+
4049
public HTTPClient graphMapper(GraphMapper mapper){
4150
this.graphMapper = mapper;
4251
return this;

src/main/java/org/javawebstack/httpclient/HTTPRequest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import org.javawebstack.httpclient.interceptor.ResponseTransformer;
55
import org.javawebstack.querystring.QueryString;
66

7+
import javax.net.ssl.*;
78
import java.io.ByteArrayOutputStream;
89
import java.io.IOException;
910
import java.io.InputStream;
1011
import java.io.OutputStream;
11-
import java.net.HttpURLConnection;
12-
import java.net.URL;
12+
import java.net.*;
1313
import java.nio.charset.StandardCharsets;
14+
import java.security.cert.X509Certificate;
1415
import java.util.Base64;
1516
import java.util.HashMap;
1617
import java.util.Map;
@@ -162,6 +163,24 @@ public HTTPRequest execute(){
162163
try{
163164
URL theUrl = new URL(client.getBaseUrl() + ((path.startsWith("/") || path.startsWith("http://") || path.startsWith("https://")) ? "" : "/") + path + (query.size() > 0 ? "?" + query.toString() : ""));
164165
conn = (HttpURLConnection) theUrl.openConnection();
166+
if(!client.isSSLVerification() && conn instanceof HttpsURLConnection){
167+
HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
168+
TrustManager[] trustAllCerts = new TrustManager[] {
169+
new X509TrustManager() {
170+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
171+
return null;
172+
}
173+
public void checkClientTrusted(X509Certificate[] certs, String authType) {
174+
}
175+
public void checkServerTrusted(X509Certificate[] certs, String authType) {
176+
}
177+
}
178+
};
179+
SSLContext sc = SSLContext.getInstance("SSL");
180+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
181+
httpsConn.setSSLSocketFactory(sc.getSocketFactory());
182+
httpsConn.setHostnameVerifier((hostname, session) -> true);
183+
}
165184
conn.setReadTimeout(client.getTimeout());
166185
conn.setConnectTimeout(5000);
167186
conn.setRequestMethod(method);

0 commit comments

Comments
 (0)