Skip to content

Commit 8e66474

Browse files
committed
feat: ssl bypass
1 parent fa8ca18 commit 8e66474

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<url>https://github.com/clean-arch-enablers-project/cae-utils-http-client/blob/main/README.md</url>
99
<groupId>com.clean-arch-enablers</groupId>
1010
<artifactId>cae-http-client</artifactId>
11-
<version>2.0.0</version>
11+
<version>2.1.0</version>
1212
<packaging>jar</packaging>
1313
<licenses>
1414
<license>

src/main/java/com/cae/http_client/HttpRequestBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public interface HttpRequestBuilder extends HttpRequestBuilderForHandlers{
77
HttpRequestBuilder pathVariableOf(String pathVariablePlaceholder, String pathVariableValue);
88
HttpRequestBuilder queryParameterOf(String queryParameterName, String queryParameterValue);
99
HttpRequestBuilder proxyAddress(String host, Integer port);
10+
HttpRequestBuilder bypassSsl();
1011
HttpRequestModel buildRequestModel();
1112

1213
}

src/main/java/com/cae/http_client/implementations/AbstractHttpRequestModel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public abstract class AbstractHttpRequestModel implements HttpRequestModel {
1717
protected BodyPublisher body;
1818
protected HttpRequestMethod method;
1919
protected ProxyAddressModel proxyAddress;
20+
protected Boolean bypassSsl = false;
2021
protected HttpResponseHandler genericResponseHandler;
2122
protected final Map<Integer, HttpResponseHandler> responseHandlersByStatusCode = new HashMap<>();
2223
protected final Map<Class<? extends Exception>, ExceptionHandler> exceptionHandlersByExceptionType = new HashMap<>();

src/main/java/com/cae/http_client/implementations/FinalHttpRequestExecutor.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,27 @@ public HttpResponse<String> execute(HttpRequest finalRequest){
3737

3838
private HttpClient createClient() {
3939
var client = HttpClient.newBuilder();
40-
Optional.ofNullable(this.httpRequestModel.proxyAddress).ifPresent(proxyAddress -> client.proxy(ProxySelector.of(new InetSocketAddress(proxyAddress.getHost(), proxyAddress.getPort()))));
40+
this.handleProxySettings(client);
41+
this.handleSslByPass(client);
4142
return client.build();
4243
}
44+
45+
private void handleProxySettings(HttpClient.Builder client) {
46+
Optional.ofNullable(this.httpRequestModel.proxyAddress).ifPresent(
47+
proxyAddress -> client.proxy(
48+
ProxySelector.of(
49+
new InetSocketAddress(
50+
proxyAddress.getHost(),
51+
proxyAddress.getPort()
52+
)
53+
)
54+
)
55+
);
56+
}
57+
58+
private void handleSslByPass(HttpClient.Builder client) {
59+
if (this.httpRequestModel.bypassSsl)
60+
client.sslContext(SSLBypassSettings.getContext());
61+
}
62+
4363
}

src/main/java/com/cae/http_client/implementations/HttpRequestBuilderImplementation.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public HttpRequestBuilder proxyAddress(String host, Integer port) {
4343
return this;
4444
}
4545

46+
@Override
47+
public HttpRequestBuilder bypassSsl() {
48+
this.httpRequest.bypassSsl = true;
49+
return this;
50+
}
51+
4652
@Override
4753
public HttpRequestModel buildRequestModel() {
4854
return this.httpRequest;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.cae.http_client.implementations;
2+
3+
import lombok.AccessLevel;
4+
import lombok.NoArgsConstructor;
5+
6+
import javax.net.ssl.SSLContext;
7+
import javax.net.ssl.TrustManager;
8+
import javax.net.ssl.X509TrustManager;
9+
import java.security.KeyManagementException;
10+
import java.security.NoSuchAlgorithmException;
11+
import java.security.SecureRandom;
12+
import java.security.cert.X509Certificate;
13+
14+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
15+
public class SSLBypassSettings {
16+
17+
public static SSLContext getContext(){
18+
try{
19+
var allTrustingManager = new TrustManager[] {new CustomTrustManager()};
20+
var sslContext = SSLContext.getInstance("TLS");
21+
sslContext.init(null, allTrustingManager, new SecureRandom());
22+
return sslContext;
23+
} catch (NoSuchAlgorithmException noSuchAlgorithmException){
24+
throw new SSLBypassSettingsException(
25+
"Couldn't set the bypass for SSL. Problem while trying to get the SSL Context instance for TLS. More details:"
26+
+ noSuchAlgorithmException
27+
);
28+
} catch (KeyManagementException keyManagementException){
29+
throw new SSLBypassSettingsException(
30+
"Couldn't set the bypass for SSL. Problem while trying init the SSL Context. More details:"
31+
+ keyManagementException
32+
);
33+
}
34+
}
35+
36+
public static class CustomTrustManager implements X509TrustManager{
37+
38+
@Override
39+
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
40+
41+
@Override
42+
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
43+
44+
@Override
45+
public X509Certificate[] getAcceptedIssuers() {
46+
return new X509Certificate[0];
47+
}
48+
}
49+
50+
public static class SSLBypassSettingsException extends RuntimeException{
51+
52+
public SSLBypassSettingsException(String message){
53+
super(message);
54+
}
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)