Spring Boot – Enabling HTTPS

By default, Spring Boot application uses HTTP 8080 port when the application starts up.

Started Application on Tomcat port_8080

You need to follow the steps given below to configure the HTTPS and the port 443 in Spring Boot application −

  • Obtain the SSL certificate – Create a self-signed certificate or get one from a Certificate Authority
  • Enable HTTPS and 443 port

1. Self-Signed Certificate

To create a self-signed certificate, Java Run Time environment comes bundled with certificate management utility key tool. This utility tool is used to create a Self-Signed certificate. It is shown in the code given here −

keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
Enter keystore password:
   Re-enter new password:
   What is your first and last name?
   [Unknown]:
   What is the name of your organizational unit?
   [Unknown]:
   What is the name of your organization?
   [Unknown]:
   What is the name of your City or Locality?
   [Unknown]:
   What is the name of your State or Province?
   [Unknown]:
   What is the two-letter country code for this unit?
   [Unknown]:
   Is CN = Unknown, OU=Unknown, O = Unknown, L = Unknown, ST = Unknown, C = Unknown correct?
   [no]: yes

This code will generate a PKCS12 keystore file named as keystore.p12 and the certificate alias name is tomcat.

2. Configure HTTPS

We need to provide the server port as 443, key-store file path, key-store-password, key-store-type and key alias name into the application.properties file. Observe the code given here −

server.port: 443
server.ssl.key-store: keystore.p12
server.ssl.key-store-password: springboot
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat

You can use the following code if you are using YAML properties use below application.yml −

server:
   port: 443
   ssl:
      key-store: keystore.p12
      key-store-password: springboot
      keyStoreType: PKCS12
      keyAlias: tomcat

You can create an executable JAR file, and run the spring boot application by using the following Maven or Gradle commands.

For Maven, you can use the following command −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

For Gradle, you can use the command

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.

Now, run the JAR file by using the following command −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 443 with https as shown −

Started Application on Tomcat Port 443

Related posts:

Truyền giá trị và tham chiếu trong java
Lập trình hướng đối tượng (OOPs) trong java
Java Program to Implement Queue using Linked List
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to Implement Knapsack Algorithm
Spring Data MongoDB Transactions
Java Program to Implement Queue using Two Stacks
Java Program to Implement Iterative Deepening
Java Switch Statement
Removing Elements from Java Collections
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Mockito and JUnit 5 – Using ExtendWith
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Một số nguyên tắc, định luật trong lập trình
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Implement Sparse Array
Java Program to Implement Hash Tables with Quadratic Probing
Spring REST API + OAuth2 + Angular
Working With Maps Using Streams
Logging in Spring Boot
Java Program to Represent Graph Using 2D Arrays
Guide to the Java Queue Interface
Converting Java Date to OffsetDateTime
Hướng dẫn Java Design Pattern – Template Method
Java – Write an InputStream to a File
Java Program to Implement Stack using Two Queues
Lập trình đa luồng với Callable và Future trong Java
Jackson Exceptions – Problems and Solutions
How to Read a Large File Efficiently with Java
Weak References in Java