声明:文章的创建引用两位大佬的blog,地址:
一、创建一个spring boot工程
目录结构:
新建package
二、生成我们需要证书文件
1.生成证书命令:(前提是我们已经安装了jdk)
keytool -genkey -alias ox -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
2.把生成的证书放到项目中
三、修改配置及编码
1.修改我们的配置文件
2.新建配置类及controller
2.1.配置类:
package com.hn.y.config;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
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;
@Configuration
public class TomcatHttpsConfig {
@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(redirectConnector());
return tomcat;
}
private Connector redirectConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(8081);
connector.setSecure(true);//设置为false表示支持http和https两种请示,true表示只支持https
connector.setRedirectPort(8080);
return connector;
}
}
2.2.controller类
package com.hn.y.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@RequestMapping(value = "/index",produces = "text/plain;charset=UTF-8")
public String index(){
return "Hello Spring Boot https";
}
}
3.启动效果:
by the way.
如果是spring boot版本是1.5.10配置类需要如下:
@Configuration
public class TomcatHttpConfig {
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory() {
@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);
}
};
factory.addAdditionalTomcatConnectors(initiateHttpConnector());
return factory;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(true);设置为false表示支持http和https两种请示,true表示只支持https
connector.setRedirectPort(8100);
return connector;
}
其他版本没有测试过。具体原因:


如有大佬觉得有问题,欢迎批评指正。谢谢