欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

spring boot 配置HTTPS代码实例

时间:2021-03-29 09:41:36|栏目:JAVA代码|点击:

这篇文章主要介绍了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;
	}
}

上一篇:Spring4整合Hibernate5详细步骤

栏    目:JAVA代码

下一篇:JDBC SQL语法

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

本文地址:http://www.codeinn.net/misctech/90227.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有