配置使用https

springboot内部已经集成了,引入密钥文件,修改相关配置文件就可以使用https了,

server:
port: 8443
ssl:
key-store: classpath:www.aaa.cn.jks
key-store-password: aaaaaa
key-store-type: JKS

配置http跳转至https

监听一个http端口,请求这个跳转后请求会转发到对应的https端口

@Configuration
public class CommonConfig {
@Value("${http.port}")
private Integer httpPort;
@Value("${server.port}")
private Integer serverPort;

@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}

@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector监听的http的端口号
connector.setPort(8080);
connector.setSecure(false);
//监听到http的端口号后转向到的https的端口号
connector.setRedirectPort(8443);
return connector;
}
}

就这么简单就完成了。