Open In App

Spring Boot – Enabling HTTPS

Last Updated : 26 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The security of your online apps is crucial in the modern digital era. The use of HTTPS (Hypertext Transfer Protocol Secure) to encrypt data sent between the server and the client is a crucial component of web application security. If you’re developing a Spring Boot Java application and wish to enable HTTPS with a self-signed certificate on your local macOS computer, you’re in the correct spot. We’ll walk you through the process of creating a self-signed certificate and enabling HTTPS in a Spring Boot Java application in this blog post.

Why Use HTTPS?

By encrypting data while it is being transmitted, HTTPS guarantees secure communication between your web application and its users. Sensitive information, including login credentials, credit card information, and personal data, is protected from prying eyes and dangerous actors thanks to its encryption. Building user trust and preserving the integrity and confidentiality of your application depend on having HTTPS enabled.

Prerequisites

Before we dive into the implementation, make sure you have the following prerequisites in place:

  1. A Spring Boot Java application.
  2. Basic knowledge of Spring Boot.
  3. JDK installed on your macOS machine.
  4. Keytool utility (usually included with the JDK).

Step By Step Implementation

Step 1: Generate a Self-Signed Certificate

Open a terminal window on your macOS machine and run the following command to generate a self-signed certificate using the keytool utility:

keytool -genkeypair -alias mycert -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650
  • -genkeypair: Generates a key pair (a public key and associated private key).
  • -alias mycert: Sets an alias for the certificate.
  • -keyalg RSA: Specifies the key algorithm (RSA).
  • -keysize 2048: Sets the key size to 2048 bits.
  • -storetype PKCS12: Sets the keystore type to PKCS12.
  • -keystore keystore.p12: Specifies the keystore file name (you can choose any name you prefer).
  • -validity 3650: Sets the certificate’s validity period to 10 years (adjust as needed).

You will be asked to submit details like your name, organization, and location during the process. You’ll have a keystore.p12 file with your self-signed certificate after responding to the questions.

Step 2: Configure HTTPS in application.properties

In your Spring Boot application’s src/main/resources/application.properties file, you need to specify the location of your keystore file, its password, and the server port:

server.port=8443
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=your-keystore-password
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=mycert

Replace your-keystore-password with the password you used during the certificate generation process.

Step 3: Create a Controller (Optional)

As described in the earlier edition of this blog post, you can develop a straightforward controller to test your Spring Boot application that supports HTTPS.

Step 4: Run Your Application

Now, you can run your Spring Boot application. If you’ve created the SecureController mentioned in Step 3, you can access it using the following URL:

https://localhost:8443/secure/hello

Conclusion

A crucial step in safeguarding your local macOS machine’s Spring Boot Java application is to enable HTTPS with a self-signed certificate. You may quickly create a self-signed certificate and set up HTTPS to safeguard user data while developing and testing your application by following these instructions. Keep in mind that self-signed certificates are ideal for testing and development but not for usage in production. Obtaining a genuine SSL certificate from a reputable certificate authority is crucial for production. Your users can feel secure using your application, even while it is still in development, if HTTPS is in place.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads