Enabling HTTPS in Spring Boot application

Enabling HTTPS in Spring Boot application

Encrypting network communication using SSL is currently a standard. Therefore, the need for HTTPS configuration in web applications often arises. In today’s post I will present how easy it is to set up a secure HTTPS channel in SpringBoot application.

1. Sample application

To present the SSL configuration in the SpringBoot application I will use the HelloWorld example like below. After starting and entering http://localhost: 8080/ you can see that the site is not secure (says the http protocol itself). After changing the protocol to https, the page does not open.

package com.bettercoding.lab;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    public static class HelloController {

        public String index() {
            return "Greetings from Spring Boot!";

2. SSL configuration in the SpringBoot application

The entire SSL configuration in the SpringBoot application comes down to preparing or generating a JKS keystore with a key and certificate, and then configuring the application to encrypt network traffic using it.

2.1 Generating keystore with pair: key, certificate

To generate a certificate, you can use the keytool tool available in JDK. The following command allows you to generate a key/certificate pair, and save them in the keystore named tmp_keystore.jks.

keytool -genkey -keyalg RSA -alias selfsigned -keystore tmp_keystore.jks -storepass password -validity 360 -keysize 2048

2.2 Configuring the SpringBoot application

The last step is to configure the appropriate settings in the application. To do this, add the following properties into application.properties file. Of course, the application.properties file is just one of the configuration methods. You can find more about this on https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html


Then, after starting the application and entering https://localhost:443, you can check that the site is properly secured by means of SSL.

At the end… May I ask you for something?

If I helped you solve your problem, please share this post. Thanks to this, I will have the opportunity to reach a wider group of readers. Thank You

0 0 vote
Article Rating
Notify of
newest most voted
Inline Feedbacks
View all comments
1 year ago

Instead of src/main/resources/ in this line,


you can use classpath:

thus is becomes