spring boot 配置HTTPS
Posted 汉客先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 配置HTTPS相关的知识,希望对你有一定的参考价值。
spring boot 版本是<version>1.5.8.RELEASE</version>
1.配置文件里,看下不要有空格=[不要有空格]
2.别名
================
server.port=8095
server.ssl.key-store=*.pfx
server.ssl.key-store-password=**
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=alias//别名
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.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 扩展: 并将 http 自动转向 https
* @Description:类说明:
* @author: gzh
* @date: 2019年11月1日上午11:08:20
*/
@Configuration
public class HttpsConfiguration {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(){
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(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector(){
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8096);//表示用8080端口来供http访问(PB,kingdee)
connector.setSecure(false);//输入:my.com,跳到: http:// www.my.com
connector.setRedirectPort(8095);//自动重定向到8095,443端口
return connector;
}
}
以上是关于spring boot 配置HTTPS的主要内容,如果未能解决你的问题,请参考以下文章