DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Using ZK With Spring Boot
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project

Trending

  • Three Measured Outcomes of Manually Scaling Fleet in Prod
  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud
  • A Software Factory on Kubernetes: Gemini Builds, jev Decides (Genkit + senro in Go)
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. How to Enable HTTPS on a Spring Boot Application

How to Enable HTTPS on a Spring Boot Application

In this article, see a tutorial on how to enable HTTPS on a Spring Boot application.

By 
EBINEZAR GNANASEKARAN
·
Mar. 09, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
83.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

HTTPS is a secure version of HTTP designed to provide Transport Layer Security (TLS) [the successor to Secure Sockets Layer (SSL)], the padlock icon in the address bar that establishes an encrypted connection between a web server and a browser. HTTPS encrypts every data packet to transmit in a secure way and protects sensitive data from an eavesdropper or hacker.

You can implement HTTPS by installing the SSL certificates on your web application. You can use either certificate issued by trusted Certificate Authorities (CA) or Self-Signed Certificate.

For development and learning purposes, you could use the Self-Signed Certificate. You would generate the Self Signed Certificate by using the Java Keytool.

Self-Signed Certificate

You can generate the certificates by using Keytool located under the JDK bin folder. For example, C:\Program Files\Java\jdk1.8.0_161\bin. There are two Self-Signed Certificates that are available, as shown below.

  1. JKS(Java Key Store) is easy to access from your own Java apps. JKS is limited only to Java and not accessible from outside Java.
  2. PKCS12: Public Key Cryptographic Standards, on the other hand, are a language-neutral way to store encrypted private keys and certificates and have been around long enough that it's supported just about everywhere.

You might also like: All About Spring Boot [Tutorials and Articles]

How to Generate Self-Signed Certificate

Type cmd in the search field in windows to locate the Command Prompt and right-click by Run as administrator. Use the keytool command as below. You could mention the certificate name that you want, shown below.

C:\Program Files\Java\jdk1.8.0_161\bin>

keytool -genkeypair -alias selfsigned_localhost_sslserver -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore ebininfosoft-ssl-key.p12 -validity 3650

The self-signed certificate is protected by password. Enter the password and other details as shown on the below screenshot.


Once you've followed the above steps, the PKS key is created and stored under the JDK Bin folder.

Applying the SSL to Spring Boot Application

  1. Copy the ebininfosoft-ssl-key from the JDK bin folder and place it under the src/main/resources on your Spring Boot Application.
  2. Add the SSL Key information into application.properties as shown below.
Java
x
 
1
#SSL Key Info
2
security.require-ssl=true
3
server.ssl.key-store-password=India@123
4
server.ssl.key-store=src/main/resources/ebininfosoft-ssl-key.p12
5
server.ssl.key-store-type=PKCS12

POM.XML

Below is the POM.xml that I used to specify the Spring Boot dependency.

XML
xxxxxxxxxx
1
26
 
1
<dependencies>
2
   <dependency>
3
      <groupId>org.springframework.boot</groupId>
4
      <artifactId>spring-boot-starter-web</artifactId>
5
   </dependency>
6
   <dependency>
7
      <groupId>org.springframework.boot</groupId>
8
      <artifactId>spring-boot-starter-data-jpa</artifactId>
9
   </dependency>
10
   <dependency>
11
      <groupId>com.h2database</groupId>
12
      <artifactId>h2</artifactId>
13
      <scope>runtime</scope>
14
   </dependency>
15
   <dependency>
16
      <groupId>org.springframework.boot</groupId>
17
      <artifactId>spring-boot-starter-test</artifactId>
18
      <scope>test</scope>
19
      <exclusions>
20
         <exclusion>
21
            <groupId>org.junit.vintage</groupId>
22
            <artifactId>junit-vintage-engine</artifactId>
23
         </exclusion>
24
      </exclusions>
25
   </dependency>
26
</dependencies>


Controller

The simple HomeController used to demonstrate the HTTPS Get request for your reference.

Java
xxxxxxxxxx
1
13
 
1
import org.springframework.web.bind.annotation.RequestMapping;
2
import org.springframework.web.bind.annotation.RequestMethod;
3
import org.springframework.web.bind.annotation.RestController;
4
5
@RestController
6
@RequestMapping("/home")
7
public class HomeController {
8
9
 @RequestMapping(value = "/", method = RequestMethod.GET)
10
 public String hello() {
11
  return "welcome to spring boot application";
12
 }
13
}


If you hit the Rest Endpoint (http://localhost:8080/home/) without HTTPS, you would get the below message in the browser.

"Bad Request"

This combination of host and port requires TLS.

If you hit the URL with HTTPS (https://localhost:8080/home/), you would get the response as below.

"Welcome to Spring Boot application."

I have placed the Source code for HTTPS spring boot along with CRUD operation using H2 in GitHub. Please refer the Java code from GitHub https://github.com/ebinezargnan/Billing

Further Reading

Building Your First Spring Boot Web Application

How to Enable HTTP/HTTPS on Spring Boot [Snippet]

Spring Framework HTTPS Spring Boot Web application

Opinions expressed by DZone contributors are their own.

Related

  • Using ZK With Spring Boot
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook