spring boot 配置HTTPS代码实例

网友投稿 544 2023-07-13

spring boot 配置HTTPS代码实例

spring boot 配置HTTPS代码实例

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

spring boot 版本是1.5.8.RELEASE

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("/*");

http:// securityConstraint.addCollection(collection);

context.addConstraint(securityConstraint);

}

}

;

LSQfHI tomcat.addAdditionalTomcatConnectorshttp://(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:// my.com

connector.setRedirectPort(8095);

//自动重定向到8095,443端口

return connector;

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spring依赖注入知识点分享
下一篇:maven profile动态选择配置文件详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~