Spring Boot https Example – JavaDream

Home / Spring Boot https Example – JavaDream

In this post we will see spring boot https. We Know that our data is going through the network. And if we are using http then our data is visible and anybody can see or intercept this data without any difficulty. spring boot https allows us to encrypt our data over the network so nobody can see our data over the network while transmitting.

For enable HTTPS we follow the below steps.

  1. Go to the bin folder of your JRE installed location and create a certificate using the following command.

Syntax :

keytool -genkey -keystore 
{ GIVE YOUR LOCATION } /{GIVE ANY NAME}.jks
alias tomcat -keyalg RSA -keysize 4096

Example

keytool -genkey -keystore
/usr/local/vasuCertificate.jks 
alias tomcat -keyalg RSA -keysize 4096
2- Copy this certificate inside your resources folder of Project.
3- Make following entry in your application.properties.
# for https
server.port=8442
server.ssl.key-store=classpath:vasuCertificate.jks
server.ssl.key-store-password=vasurajput
server.ssl.key-password=vasurajput
4- Now Make A Controller Class to create some end point.
@RestController
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger("HomeController.class");

  @GetMapping("/")
   public String test() {
    logger.info("Test Controller Call");
     return "index";
   }
}
5- Now create a configuration class for enable HTTPS.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.vasu.HttpsDemo.Controller;

import java.io.File;
import java.io.IOException;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
*
* @author vasu rajput
*/

@Configuration
public class Https {

@Bean
public ServletWebServerFactory servletContainer() {

TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {

@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
  }
};
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
 }

private Connector createStandardConnector() {

Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
  }
}

As we know for https we define 8443 port. So in above configuration class code we define connector.setRedirectPort(8443) this port.

Now if you follow all above steps so you have successfully confgured the https in spring boot.

Now go to your browser and type http://localhost:8080 you will automatically redirect to https://localhost:8443/ port which uses https.

You may also like:

Let’s Encrypt Certificate in Java with Apache Tomcat

Spring Boot Security With JWT and MySQL

Help Others, Please Share

About Author

Leave a Reply

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