现在的位置: 首页 > 编程语言 > 正文

springboot配置HTTPS代码实例

2020年02月13日 编程语言 ⁄ 共 1921字 ⁄ 字号 评论关闭

这篇文章主要介绍了spring boot 配置HTTPS代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

spring boot 版本是<version>1.5.8.RELEASE</version>

1.配置文件里,看下不要有空格=[不要有空格]

2.别名

================

server.port=8095server.ssl.key-store=*.pfxserver.ssl.key-store-password=**server.ssl.key-store-type=PKCS12server.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*/@Configurationpublic class HttpsConfiguration {@Beanpublic 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;}@Beanpublic 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.comconnector.setRedirectPort(8095);//自动重定向到8095,443端口return connector;}}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: spring boot 配置HTTPS代码实例

以上就上有关springboot配置HTTPS代码实例的相关介绍,要了解更多spring,boot,配置,HTTPS内容请登录学步园。

抱歉!评论已关闭.