Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

How to Add HTTPS Support to Your Spring Boot App?


On this submit, we’ll stroll via how you can configure HTTPS in a Spring Boot utility utilizing self-signed certificates to encrypt connections and add transport layer safety.

What You may Want

  • Docker
  • Area Title
  • AWS EC2 Occasion
  • Java
  • Spring Boot
  • Code Editor
  • Terminal Entry

What You Ought to Know

  • Primary Docker instructions like run, construct, and so forth.
  • How one can create an A report in DNS.
  • Spring Boot fundamentals – making a challenge, operating with Gradle/Maven and so forth.
  • Primary Linux instructions like cd, ls, vim and so forth to navigate the terminal.
  • How one can launch an EC2 occasion on AWS and join through SSH.

Generate Certificates

Why do we want certificates?
Certificates enable safe HTTPS connections. They include public keys that encrypt knowledge despatched between a server and shoppers. This protects the info from spying. Certificates additionally validate that you’re related to the best server.

What we have to generate certificates?
To generate certificates, you want:

  • A Docker put in server like an AWS EC2 occasion
  • The server’s IP handle related to a site identify utilizing DNS
  • An ‘A report’ added to DNS to map the area to the server’s IP

How one can generate certificates?

  1. Hook up with server utilizing ssh.
  2. Create a Docker quantity known as certs docker quantity create certs. We want quantity to retailer certificates.
  3. Run this command beneath to get certificates and retailer it to quantity certs.
docker run -d 
--name certbot 
-v certs:/and so forth/letsencrypt 
-v certs-data:/var/lib/letsencrypt 
-p 80:80 
-p 443:443 
certonly --standalone --preferred-challenges http --email youremail@gmail.com -d yourdomain.com --agree-tos
Enter fullscreen mode

Exit fullscreen mode

Exchange youremail@gmail.com and yourdomain.com together with your actual e mail and area earlier than operating certbot!

The Docker container will exit after producing the certificates. Verify the container logs to see the place the certificates recordsdata had been saved.

Excellent we generated certificates and saved to docker quantity certs and after we run our Spring Boot App it’s going to take certificates from quantity.

Configure Spring Boot for HTTPS

Create a Spring Boot App

  • Generate Spring Boot app utilizing this hyperlink start.spring.io
  • Add easy @GetMapping to test is our app working.
public class SpringBootHttpsApplication {

    public static void major(String[] args) {
        SpringApplication.run(SpringBootHttpsApplication.class, args);

    public String hey() {
        return "Good day World! 🚀";
Enter fullscreen mode

Exit fullscreen mode

  • Now go to utility.properties and configure ssl.
Enter fullscreen mode

Exit fullscreen mode

Should you’re utilizing .yml then use this configuration.

    enabled: true
    certificates: ${FULLCHAINPEM}
    certificate-private-key: ${PRIVKEYPEM}
Enter fullscreen mode

Exit fullscreen mode

Containerizing Spring Boot App

To containerize your Spring Boot app, that you must create a Dockerfile. The Dockerfile tells Docker how you can bundle your app.
I used Gradle as bundle supervisor and my Dockerfile appears like beneath.

FROM openjdk:20 as construct
COPY . ./
RUN microdnf set up findutils
RUN ./gradlew construct -x check

FROM openjdk:20-jdk-slim
COPY --from=construct /app/construct/libs/spring-boot-https-0.0.1.jar .
CMD ["java", "-jar", "spring-boot-https-0.0.1.jar"]
Enter fullscreen mode

Exit fullscreen mode

Construct Docker Picture and Push to Dockerhub
To construct Docker Picture, that you must execute this command

docker construct -t spring-boot-https:1 .
Enter fullscreen mode

Exit fullscreen mode

When constructing Docker photographs on macOS to run on Linux, there may be platform compatibility points. The picture wants to focus on linux/amd64 to work correctly on Ubuntu.

To construct a suitable picture, use the Docker buildx command:

docker buildx construct --platform linux/amd64 -t spring-boot-https:1 .
Enter fullscreen mode

Exit fullscreen mode

After constructing docker picture push your picture to your dockerhub utilizing these instructions

docker tag spring-boot-https:1 <Dockerhub Username>/spring-boot-https:1

docker push <Dockerhub Username>/spring-boot-https:1
Enter fullscreen mode

Exit fullscreen mode

We used Dockerhub for Demo. You should utilize different registry like AWS ECR and so forth.

After pushing picture to Dockerhub we might apply it to any server.

Run Spring Boot App on Server

To run the Dockerized Spring Boot app, we are able to use Docker Compose. This defines how you can run the container with a yaml config file.


    container_name: spring-boot-https
    picture: beksultancs/spring-boot-https:1
    restart: unless-stopped
      - "443:443"
      - certs:/certs
      - SERVER_PORT=443
      - FULLCHAINPEM=/certs/stay/hello.beksultan.dev/fullchain.pem
      - PRIVKEYPEM=/certs/stay/hello.beksultan.dev/privkey.pem
    exterior: true
Enter fullscreen mode

Exit fullscreen mode

Now we want switch compose.yml to server or we are able to create this file in server. Should you needed to create compose.yml regionally and switch to server you need to use scp command.

scp -I <PrivateKey> compose.yml <Username>:<Host>:~/ 
Enter fullscreen mode

Exit fullscreen mode

Or you may create file on server utilizing vim or nano and duplicate paste it.

Remaining step we should always run our compose.yml utilizing command
docker compose -f compose.yml up -d

That is all! Our app ought to run on our area with https https://yourdomain/hello

Image description

Should you do not need to use docker compose, then u simply have to execute this command on server

docker run -d 
  --name spring-boot-https 
  -p 443:443 
  -v certs:/certs 
  -e SERVER_PORT=443  
  -e FULLCHAINPEM=/certs/stay/hello.beksultan.dev/fullchain.pem 
  -e PRIVKEYPEM=/certs/stay/hello.beksultan.dev/privkey.pem 
Enter fullscreen mode

Exit fullscreen mode

And that is it! We’ve got configured a Spring Boot utility to make use of HTTPS with a self-signed certificates. The steps we coated had been:

  • Producing a self-signed certificates utilizing Certbot
  • Including the certificates recordsdata to a Docker quantity
  • Configuring the Spring Boot utility properties for SSL
  • Containerizing the app with a Dockerfile
  • Operating the Docker picture on a server utilizing docker-compose

With these steps, your Spring Boot app can now serve content material over HTTPS securely. The self-signed certificates permits encrypting connections without having a certificates from a CA.

I hope this tutorial was useful! Let me know within the feedback in case you have every other questions on configuring Spring Boot for HTTPS. I am planning to put in writing a comply with up submit on how you can add HTTPS to a React app with Nginx as a reverse proxy. So keep tuned!


Add a Comment

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

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?