How to Enable HTTP/HTTPS in Spring Boot

This commit is contained in:
Umesh Awasthi
2019-01-27 15:27:00 -08:00
parent 3cebe0f7bd
commit 6d9cd35349
11 changed files with 602 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
package com.javadevjournal;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class HpptHttpsSpringBootApplication {
//HTTP port
@Value("${http.port}")
private int httpPort;
public static void main(String[] args) {
SpringApplication.run(HpptHttpsSpringBootApplication.class, args);
}
// Let's configure additional connector to enable support for both HTTP and HTTPS
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(httpPort);
return connector;
}
}

View File

@@ -0,0 +1,13 @@
package com.javadevjournal.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@GetMapping(value = "/greeting")
public String greeting(){
return "I am working with both HTTP and HTTPS";
}
}

View File

@@ -0,0 +1,13 @@
# The format used for the keystore. for JKS, set it as JKS
server.ssl.key-store-type=PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=classpath:keystore/javadevjournal.p12
# The password used to generate the certificate
server.ssl.key-store-password=your password
# The alias mapped to the certificate
server.ssl.key-alias=javadevjournal
# Run Spring Boot on HTTPS only
server.port=8443
#HTTP port
http.port=8080

View File

@@ -0,0 +1,17 @@
package com.javadevjournal;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HpptHttpsSpringBootApplicationTests {
@Test
public void contextLoads() {
}
}