Skip to content

Commit

Permalink
Made java 8 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakky54 committed Dec 4, 2021
1 parent f2dd900 commit 6f8f310
Show file tree
Hide file tree
Showing 38 changed files with 72 additions and 530 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:

- uses: actions/checkout@v1

- name: Set up JDK 11
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '11'
java-version: '8'

- name: Cache dependencies
uses: actions/cache@v1
Expand All @@ -26,7 +26,7 @@ jobs:
${{ runner.os }}-maven-
- name: Build, test and quality scan
run: mvn clean install sonar:sonar -P'!standalone-client' -Pjacoco -Dsonar.organization=hakky54 -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=${{ secrets.SONAR_TOKEN }} --no-transfer-progress
run: mvn clean install -P'!standalone-client' --no-transfer-progress
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: github.actor == 'Hakky54'
Expand All @@ -40,11 +40,11 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Set up JDK 11
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '11'
java-version: '8'

- name: Cache dependencies
uses: actions/cache@v1
Expand All @@ -62,11 +62,11 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Set up JDK 11
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '11'
java-version: '8'

- name: Cache dependencies
uses: actions/cache@v1
Expand All @@ -84,11 +84,11 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Set up JDK 11
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '11'
java-version: '8'

- name: Cache dependencies
uses: actions/cache@v1
Expand Down
2 changes: 1 addition & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
image: adoptopenjdk/maven-openjdk11:latest
image: adoptopenjdk/maven-openjdk8:latest
tasks:
- init: mvn install
12 changes: 3 additions & 9 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[![SonarCloud](https://sonarcloud.io/images/project_badges/sonarcloud-white.svg)](https://sonarcloud.io/dashboard?id=nl.altindag%3Amutual-tls-ssl)

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Hakky54/mutual-tls)
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Hakky54/mutual-tls/tree/tutorial-for-java-8)

# Mastering two way TLS 🔐 [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=If%20you%20are%20interested%20in%20securing%20your%20web%20application,%20you%20might%20want%20to%20read%20the%20tutorial%20Mastering%20two-way%20tls&url=https://github.com/Hakky54/mutual-tls&via=hakky541&hashtags=encryption,security,https,ssl,tls,certificate,developer,java,scala,kotlin,sslcontextkickstart)

Expand Down Expand Up @@ -57,23 +57,17 @@ Also [GitHub - SSLContext Kickstart](https://github.com/Hakky54/sslcontext-kicks
# Tutorial
## Starting the server
**Minimum requirements:**
1. Java 11
1. Java 8
2. Maven 3.5.0
3. Eclipse, Intellij IDEA (or any other text editor like VIM)
4. A terminal

If you want to start instantly without installing any software, click the button below to open the project in an online development environment:

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Hakky54/mutual-tls)
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Hakky54/mutual-tls/tree/tutorial-for-java-8)

This project contains a maven wrapper, so you can run this project without installing maven. The documentation for this tutorial contains next to the default mvn command also the commands for the maven wrapper.

If you want to run this project with Java 8, you can get an older version with the git command below.
And it is recommended to follow the instruction for that specific version, which is available at this [page](https://github.com/Hakky54/mutual-tls-ssl/tree/java-8-compatible)
```bash
git checkout tags/java-8-compatible
```

The server depends on the other components of the project, so run `mvn install` in the root directory first.
```bash
mvn install
Expand Down
8 changes: 0 additions & 8 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.mizosoft.methanol</groupId>
<artifactId>methanol</artifactId>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-client-core</artifactId>
Expand All @@ -146,10 +142,6 @@
<groupId>io.ktor</groupId>
<artifactId>ktor-client-android</artifactId>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-client-java</artifactId>
</dependency>
<dependency>
<groupId>org.http4k</groupId>
<artifactId>http4k-core</artifactId>
Expand Down
55 changes: 16 additions & 39 deletions client/src/main/java/nl/altindag/client/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import akka.actor.ActorSystem;
import akka.http.javadsl.ConnectionContext;
import akka.http.javadsl.HttpsConnectionContext;
import com.github.mizosoft.methanol.Methanol;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.gson.GsonBuilder;
Expand All @@ -15,6 +14,7 @@
import com.twitter.finagle.http.Response;
import com.typesafe.config.ConfigFactory;
import feign.Feign;
import io.netty.handler.ssl.SslContext;
import io.vertx.core.Vertx;
import io.vertx.core.net.KeyCertOptions;
import io.vertx.core.net.TrustOptions;
Expand All @@ -41,7 +41,9 @@
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.impl.client.HttpClients;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.Dsl;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
Expand All @@ -58,7 +60,6 @@
import javax.net.ssl.SSLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;

import static java.util.Objects.nonNull;
import static nl.altindag.client.Constants.SERVER_URL;
Expand Down Expand Up @@ -128,18 +129,6 @@ public org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient apache5Htt
return client;
}

@Bean
public HttpClient jdkHttpClient(@Autowired(required = false) SSLFactory sslFactory) {
if (nonNull(sslFactory)) {
return HttpClient.newBuilder()
.sslParameters(sslFactory.getSslParameters())
.sslContext(sslFactory.getSslContext())
.build();
} else {
return HttpClient.newHttpClient();
}
}

@Bean
public RestTemplate restTemplate(org.apache.http.impl.client.CloseableHttpClient httpClient) {
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
Expand All @@ -150,7 +139,7 @@ public RestTemplate restTemplate(org.apache.http.impl.client.CloseableHttpClient
public OkHttpClient okHttpClient(@Autowired(required = false) SSLFactory sslFactory) {
if (nonNull(sslFactory)) {
return new OkHttpClient.Builder()
.sslSocketFactory(sslFactory.getSslSocketFactory(), sslFactory.getTrustManager().orElseThrow())
.sslSocketFactory(sslFactory.getSslSocketFactory(), sslFactory.getTrustManager().get())
.hostnameVerifier(sslFactory.getHostnameVerifier())
.build();
} else {
Expand All @@ -161,9 +150,9 @@ public OkHttpClient okHttpClient(@Autowired(required = false) SSLFactory sslFact
@Bean
@Scope("prototype")
public reactor.netty.http.client.HttpClient nettyHttpClient(@Autowired(required = false) SSLFactory sslFactory) throws SSLException {
var httpClient = reactor.netty.http.client.HttpClient.create();
reactor.netty.http.client.HttpClient httpClient = reactor.netty.http.client.HttpClient.create();
if (nonNull(sslFactory)) {
var sslContext = NettySslUtils.forClient(sslFactory).build();
SslContext sslContext = NettySslUtils.forClient(sslFactory).build();
httpClient = httpClient.secure(sslSpec -> sslSpec.sslContext(sslContext));
}
return httpClient;
Expand All @@ -173,7 +162,7 @@ public reactor.netty.http.client.HttpClient nettyHttpClient(@Autowired(required
@Scope("prototype")
public org.eclipse.jetty.client.HttpClient jettyHttpClient(@Autowired(required = false) SSLFactory sslFactory) {
if (nonNull(sslFactory)) {
var sslContextFactory = JettySslUtils.forClient(sslFactory);
SslContextFactory sslContextFactory = JettySslUtils.forClient(sslFactory);
return new org.eclipse.jetty.client.HttpClient(sslContextFactory);
} else {
return new org.eclipse.jetty.client.HttpClient();
Expand Down Expand Up @@ -209,7 +198,7 @@ public Client jerseyClient(@Autowired(required = false) SSLFactory sslFactory) {
@Bean
public com.sun.jersey.api.client.Client oldJerseyClient(@Autowired(required = false) SSLFactory sslFactory) {
if (nonNull(sslFactory)) {
var clientConfig = new DefaultClientConfig();
DefaultClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(sslFactory.getHostnameVerifier(), sslFactory.getSslContext()));
return com.sun.jersey.api.client.Client.create(clientConfig);
} else {
Expand All @@ -236,13 +225,13 @@ public javax.ws.rs.client.Client cxfJaxRsClient(@Autowired(required = false) SSL

@Bean
public org.apache.cxf.jaxrs.client.WebClient cxfWebClient(@Autowired(required = false) SSLFactory sslFactory) {
var factory = new JAXRSClientFactoryBean();
JAXRSClientFactoryBean factory = new JAXRSClientFactoryBean();
factory.setAddress(SERVER_URL);
if (nonNull(sslFactory)) {
// One can also get conduit from WebClient.getConfig(webClient).getHttpConduit() and change it directly
factory.setBus(new CXFBusFactory().createBus());
factory.getBus().setExtension((name, address, httpConduit) -> {
var tls = new TLSClientParameters();
TLSClientParameters tls = new TLSClientParameters();
tls.setSSLSocketFactory(sslFactory.getSslSocketFactory());
tls.setHostnameVerifier(sslFactory.getHostnameVerifier());
httpConduit.setTlsClientParameters(tls);
Expand Down Expand Up @@ -286,8 +275,8 @@ public Retrofit retrofit(OkHttpClient okHttpClient) {

@Bean
public Service<Request, Response> finagle(@Autowired(required = false) SSLFactory sslFactory) throws URISyntaxException {
var uri = new URI(SERVER_URL);
var client = Http.client();
URI uri = new URI(SERVER_URL);
Http.Client client = Http.client();
if (nonNull(sslFactory)) {
client = client
.withNoHttp2()
Expand All @@ -308,7 +297,7 @@ public ActorSystem actorSystem() {
@Bean
public akka.http.javadsl.Http akkaHttpClient(@Autowired(required = false) SSLFactory sslFactory,
ActorSystem actorSystem) {
var http = akka.http.javadsl.Http.get(actorSystem);
akka.http.javadsl.Http http = akka.http.javadsl.Http.get(actorSystem);
if (nonNull(sslFactory)) {
HttpsConnectionContext httpsContext = ConnectionContext.httpsClient(sslFactory.getSslContext());
http.setDefaultClientHttpsContext(httpsContext);
Expand All @@ -319,9 +308,9 @@ public akka.http.javadsl.Http akkaHttpClient(@Autowired(required = false) SSLFac
@Bean
public AsyncHttpClient asyncHttpClient(@Autowired(required = false) SSLFactory sslFactory) throws SSLException {
if (nonNull(sslFactory)) {
var sslContext = NettySslUtils.forClient(sslFactory).build();
SslContext sslContext = NettySslUtils.forClient(sslFactory).build();

var clientConfigBuilder = dispatch.Http.defaultClientBuilder()
DefaultAsyncHttpClientConfig.Builder clientConfigBuilder = dispatch.Http.defaultClientBuilder()
.setSslContext(sslContext);

return Dsl.asyncHttpClient(clientConfigBuilder);
Expand All @@ -340,21 +329,9 @@ public Feign.Builder feign(@Autowired(required = false) SSLFactory sslFactory) {
}
}

@Bean
public Methanol methanol(@Autowired(required = false) SSLFactory sslFactory) {
if (nonNull(sslFactory)) {
return Methanol.newBuilder()
.sslContext(sslFactory.getSslContext())
.sslParameters(sslFactory.getSslParameters())
.build();
} else {
return Methanol.create();
}
}

@Bean
public io.vertx.ext.web.client.WebClient vertxWebClient(@Autowired(required = false) SSLFactory sslFactory) {
var clientOptions = new WebClientOptions();
WebClientOptions clientOptions = new WebClientOptions();

if (nonNull(sslFactory)) {
clientOptions.setSsl(true);
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/java/nl/altindag/client/PropertyResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class PropertyResolver {

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
var propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
var yaml = new YamlPropertiesFactoryBean();
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource(CLIENT_PROPERTY_FILE));
propertySourcesPlaceholderConfigurer.setProperties(Objects.requireNonNull(yaml.getObject()));
return propertySourcesPlaceholderConfigurer;
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/java/nl/altindag/client/SSLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public SSLFactory sslFactory(
if (oneWayAuthenticationEnabled) {
sslFactory = SSLFactory.builder()
.withTrustMaterial(trustStorePath, trustStorePassword)
.withProtocols("TLSv1.3")
.withProtocols("TLSv1.2")
.build();
}

if (twoWayAuthenticationEnabled) {
sslFactory = SSLFactory.builder()
.withIdentityMaterial(keyStorePath, keyStorePassword)
.withTrustMaterial(trustStorePath, trustStorePassword)
.withProtocols("TLSv1.3")
.withProtocols("TLSv1.2")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import nl.altindag.client.ClientType;
import nl.altindag.client.model.ClientResponse;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
Expand All @@ -26,7 +27,7 @@ public Apache5HttpAsyncClientService(CloseableHttpAsyncClient httpClient) {

@Override
public ClientResponse executeRequest(String url) throws Exception {
var request = SimpleHttpRequests.get(url);
SimpleHttpRequest request = SimpleHttpRequests.get(url);
request.addHeader(HEADER_KEY_CLIENT_TYPE, getClientType().getValue());

Future<SimpleHttpResponse> responseFuture = httpClient.execute(request, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.ws.rs.client.Client;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import static nl.altindag.client.ClientType.APACHE_CXF_JAX_RS;
import static nl.altindag.client.Constants.HEADER_KEY_CLIENT_TYPE;
Expand All @@ -26,7 +27,7 @@ public ApacheCXFJaxRsClientService(@Qualifier("cxf") Client client) {

@Override
public ClientResponse executeRequest(String url) {
var response = client.target(url)
Response response = client.target(url)
.request(MediaType.TEXT_PLAIN_TYPE)
.header(HEADER_KEY_CLIENT_TYPE, getClientType().getValue())
.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import static nl.altindag.client.ClientType.APACHE_CXF_WEB_CLIENT;
import static nl.altindag.client.Constants.HEADER_KEY_CLIENT_TYPE;
Expand All @@ -24,7 +25,7 @@ public ApacheCXFWebClientService(WebClient client) {

@Override
public ClientResponse executeRequest(String url) throws Exception {
var response = client.to(url, false)
Response response = client.to(url, false)
.accept(MediaType.TEXT_PLAIN_TYPE)
.header(HEADER_KEY_CLIENT_TYPE, getClientType().getValue())
.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public ApacheHttpAsyncClientService(CloseableHttpAsyncClient httpClient) {

@Override
public ClientResponse executeRequest(String url) throws Exception {
var request = new HttpGet(url);
HttpGet request = new HttpGet(url);
request.addHeader(HEADER_KEY_CLIENT_TYPE, getClientType().getValue());

Future<HttpResponse> responseFuture = httpClient.execute(request, null);
HttpResponse response = responseFuture.get(TIMEOUT_AMOUNT_IN_SECONDS, TimeUnit.SECONDS);

var responseBody = EntityUtils.toString(response.getEntity());
String responseBody = EntityUtils.toString(response.getEntity());
int statusCode = response.getStatusLine().getStatusCode();
return new ClientResponse(responseBody, statusCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public ApacheHttpClientService(CloseableHttpClient httpClient) {

@Override
public ClientResponse executeRequest(String url) throws IOException {
var request = new HttpGet(url);
HttpGet request = new HttpGet(url);
request.addHeader(HEADER_KEY_CLIENT_TYPE, getClientType().getValue());
HttpResponse response = httpClient.execute(request);

var responseBody = EntityUtils.toString(response.getEntity());
String responseBody = EntityUtils.toString(response.getEntity());
int statusCode = response.getStatusLine().getStatusCode();
return new ClientResponse(responseBody, statusCode);
}
Expand Down
Loading

0 comments on commit 6f8f310

Please sign in to comment.