In this example, I will explain how to enable HTTPS(SSL) in Spring Boot. The self-signed certificate is used in this example, and configure a simple REST Spring Boot application.
+ Java 11
+ Spring Boot 2.5.8
+ Spring Boot Rest API
+ SSL
The first step is to generate will use the JDK’s keytool to generate a self-sign certificate in PKCS12 format using JDK Keytool. Below command will create a PKCS12 cert after that put this file into the resources folder under SpringBoot project
keytool -genkeypair -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore springboothttps.p12 -validity 365
server.port=8443
server.ssl.key-store=classpath:springboothttps.p12
server.ssl.key-store-password=password
server.ssl.keyStoreType=PKCS12
Create simple Rest endpoint which return "Hello HTTPS"
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello HTTPS";
}
}
Start the server, It is ready to be tested now.
Postman provides a way to set SSL certificates on a per-domain basis. To manage your client certificates, go to Settings, and select the Certificates tab. Add your certificate as below