Enabling SSL using HTTPS in Spring Boot
At first step, we need to create a Self Signed Certificate using KeyTool command.
STEP - 1
C:\Users\gaurav>keytool -genkey -alias selfsigned_localhost_sslserver -keyalg RSA -keysize 2048 -
validity 700 -keypass changeit -storepass changeit -keystore name.jks
STEP - 2
To Verify the details available in the generated JKS file:-
C:\Users\gaurav>keytool -list -keystore ssl-server.jks
STEP - 3:- Go to Site http://start.spring.io/
Create a maven project by adding the required dependencies like Web and also select a spring boot version. Few dependency will come as default. Then generate the maven project and import in your IDE
Create a Spring Boot Project by following below steps
STEP - 4
After Import project Structure will look below:-
STEP - 5
Place the generated JKS files in resources folder.
STEP - 6
Create a controller called SSLServerController.java
package com.gaurav.boot.ssl.enable.tomcat;
import java.util.Date;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SSLServerController{
@RequestMapping(method = RequestMethod.GET, value="/rest")
public String greetingService(@RequestParam String name){
System.out.println("Inside REST controller method ()");
return "Welcome "+name;
}
}
STEP - 7
In application.properties file place the below mentioned properties
server.port=8443
server.ssl.key-alias=selfsigned_localhost_sslserver
server.ssl.key-password=changeit
server.ssl.key-store=classpath:ssl-server.jks
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
STEP - 8
Run the application as Spring Boot application by right clicking on Application.java -> Run As-> Spring Boot App.
STEP - 9
Now, Open the Browser and hit the REST URL like below:-
Here REST url will be https://localhost:8443/rest?name=Gaurav
No comments:
Post a Comment