Spring Boot SSL Configuration

 In this topic, We will discuss on Spring boot SSL configuration and also discuss on Keytool.

What is SSL?

SSL (Secure Sockets Layer) is the standard security technology for establishing an encrypted link between a web server and a browser. This link ensures that all data passed between the web server and browsers remain private and integral.

SSL or TLS (Transport Layer Security) certificates are data files that bind a cryptographic key to the details of an organization. When SSL/TLS certificate is installed on a web server, it enables a secure connection between the web server and the browser that connects to it.

In this article, we are going use spring boot 2.0.x version. In the previous article, we have already explained Spring boot hello world example.

Spring Boot SSL Configuration:

Tools used:

  1. JDK 1.8
  2. Spring Boot 2.0.4
  3. Maven
  4. Keytool
  5. Favorite IDE

Project Structure:

Spring Boot SSL Configuration Structure
Spring Boot SSL Configuration Structure

To enable SSL (https) for your application on the embedded Tomcat. Follow the below steps as mentioned.

  • Get the SSL certification: generate a self-signed certificate or get one from a Certificate Authority
  • Enable HTTPS in Spring Boot
  • Redirect HTTP to HTTPS

Get an SSL certification:

We have two option to get an SSL certificate for an application.

  1. Generate it by yourself 
  2. Get it from any certification authority(CA).

You can also get it from any certification authority. I have heard good things about SSLStore to buy your certificate for a reasonable price with excellent support.

In this application, We are going to explain how to create self-signed certificate generated by java keytoolcommand. Execute the below command to get the certificate.

Spring Boot SSL Configuration
Spring Boot SSL Configuration

 

This will generate the technologic.jks file containing our self-signed certificates in the directory from where the Keytool command has been executed with certificate alias techielogic.

Configure the SSL certificates Spring boot  :

Create Spring boot hello world application as mentioned in this article. Next step is to enable the HTTPS in spring boot application.

By default embedded tomcat is enabled with HTTP with 8080 port Spring Boot Application. Let’s configure HTTPS in the default application.properties file under src/main/resources of your Spring Boot application.

That’s it. We configured SSL certification to the application. Now execute below commands to build the application and run the application.

Spring Boot SSL Configuration
Spring Boot SSL Configuration

As in the above image, Application is running in the https with 8443 port. To test this application run the below URL.

  • Redirect HTTP to HTTPS

This is an optional step in case you want to redirect your HTTP traffic to HTTPS so that the full site becomes securedTo achieve this we’ll need to add a second Tomcat connector, but currently, it is not possible to configure two connectors in the application. So that any request in 8080 through HTTP, it would be automatically redirected to 8443 and https.

Note: Below code is applicable for above Spring boot 2.0.x version. For below 2.0.x this code won’t work.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons