这篇文章主要介绍了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;
- }
- }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。