spring boot之支持http和https并行(http不跳转)
Posted heroinss
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot之支持http和https并行(http不跳转)相关的知识,希望对你有一定的参考价值。
首先需要去做个证书
yml配置文件中设置的是https监听端口
server:
port: 10007
tomcat:
basedir: /data/apps/temp
#配置SSL
ssl:
key-store: classpath:bdcourtyard.com.jks
key-store-password: 4a98pp121wv
keyStoreType: JKS
以下是启动类添加的代码
/** * it\'s for set http url auto change to https */ @Bean public TomcatEmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { SecurityConstraint securityConstraint = new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL");//confidential SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } }; tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; } @Bean public Connector httpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(9000); connector.setSecure(true); connector.setRedirectPort(10007); return connector; }
这个是同事配的,实测有效搬过来具体自己还没研究
以上是关于spring boot之支持http和https并行(http不跳转)的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot项目如何同时支持HTTP和HTTPS协议
Spring Boot工程支持HTTP和HTTPS,HTTP重定向HTTPS
Https系列之二:https的SSL证书在服务器端的部署,基于tomcat,spring boot