Skip to main content

Enabling HTTPS for Your Spring Boot Application

 Enabling HTTPS for Your Spring Boot Application


-By: Kailash Nirmal









Contents:

  • Introduction
  • Prerequisites
  • Step 1: Generate a Self-Signed SSL Certificate
  • Step 2: Configure Your Application
  • Step 3: Redirect HTTP to HTTPS
  • Step 4: Test the Configuration
  • Step 5 : Verify HTTPS configuration






Introduction


This guide outlines the steps to configure HTTPS for your Spring Boot application. Enabling HTTPS ensures secure communication between clients and servers, protecting sensitive data and enhancing overall security.

Prerequisites


Before you begin, ensure you have the following:

Java Development Kit (JDK) 8 or higher installed.
Maven or Gradle for managing your Spring Boot project.
An existing Spring Boot application.

You can check your Java version by running:

java -version

Step 1: Generate a Self-Signed SSL Certificate


  1. Open your terminal or command prompt.
  2. Run the following command to create a keystore and generate a self-signed certificate
keytool -genkeypair -alias yourAlias -keyalg RSA -keystore keystore.p12 -storetype PKCS12 -storepass yourPassword -validity 365

  1. Fill in the prompted details (name, organization, etc.) as required.
  2. The keystore.p12 file will be created in the current directory.

Step 2: Configure Your Application

  1. Move the keystore.p12 file to the src/main/resources directory of your project.
  2. Update your application.properties file to include the following SSL configuration:
server.port=8443 server.ssl.key-store=classpath:keystore.p12 server.ssl.key-store-password=yourPassword server.ssl.key-store-type=PKCS12 server.ssl.key-alias=yourAlias


Step 3: Redirect HTTP to HTTPS


To ensure that all HTTP requests are redirected to HTTPS, modify your security configuration class as needed.

For example :

http

.requiresChannel()

.anyRequest().requiresSecure()

.and()

.csrf().and() // Enable CSRF protection


Step 4: Test the Configuration


  1. Run your Spring Boot application.
  2. Access the application via the following URLs:

Step 5: Verify HTTPS Configuration

Using the Browser Developer Tools

  1. Open Your Browser: Launch your preferred web browser (e.g., Chrome, Firefox).
  2. Access Your Application: Navigate to your application using the HTTPS URL, for example: https://localhost:8443/login.
  3. Open Developer Tools:
  • Chrome: Press F12 or right-click on the page and select "Inspect."
  • Firefox: Press F12 or right-click and select "Inspect Element."
  1. Check the Security Tab:
  • In the Developer Tools, go to the "Security" tab.
  • Look for a message indicating that the connection is secure. You should see a note stating "Connection is secure" or similar.
  • If you see any warnings or errors, check the details for more information.

2. Checking the Certificate

  1. View Certificate Details:
  • In the same "Security" tab, you can click on "View certificate" or similar options to see details about the SSL certificate.
  • Ensure that the certificate is issued to your domain (or localhost in this case) and is valid.
  1. Certificate Validity:
  • Check the expiration date to ensure the certificate is still valid.
  • If you are using a self-signed certificate, it may show a warning indicating that it is not trusted. This is expected when using a self-signed certificate during development.

Verification


After following these steps, your Spring Boot application should now be accessible via HTTPS, ensuring secure communication.

I hope this guide was helpful. Let me know in-case any doubts or issues. 

Thanks,
Kailash Nirmal
JavaCharter

      Comments

      Popular posts from this blog

      How to Create Cross-References in Microsoft Word

        How to Create Cross-References in Microsoft Word When writing a document, you might want to refer to other sections or items within the same document. This is where cross-references come in handy! Cross-references allow you to link to other parts of your document, making it easy for readers to find related information. Here’s a simple guide on how to do it in Microsoft Word. What is a Cross-Reference? A cross-reference is a way to point your readers to additional information somewhere else in your document. Think of it as a signpost that directs someone to another part of the text. This is especially useful in longer documents, where readers might need to find related information quickly. For example, imagine you are writing a report about different species of birds. In one section, you describe the characteristics of sparrows. Later, you mention sparrows again in a different section discussing their habitats. Instead of repeating all the information about sparrows, you can creat...