springboot 1.5和2.0版本tomcat的配置https证书

首先在配置文件添加

server.ssl.key-store=classpath:xxx.p12(证书)
server.ssl.key-store-password=123456(密码)
server.ssl.key-store-type=PK(加密类型)
server.ssl.key-alias=tomcat(别名)
server.ssl.enabled=true

然后将文件xxx.p12放入resource目录下

1.5版本

@Bean
	public EmbeddedServletContainerFactory servletContainer() {
		TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
			@Override
			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");
	   //将http的访问端口重定向到https的访问端口
	   connector.setScheme("http");
	   //http的访问端口
	   connector.setPort(80);
	   connector.setSecure(false);
	   //https的访问端口 就是ssl证书的端口  默认是443
	   connector.setRedirectPort(8888);
	   return connector;
	}

2.0版本

@Bean
	public TomcatServletWebServerFactory servletContainer() {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
			@Override
			protected void postProcessContext(Context context) {
				SecurityConstraint constraint = new SecurityConstraint();
				constraint.setUserConstraint("CONFIDENTIAL");
				SecurityCollection collection = new SecurityCollection();
				collection.addPattern("/*");
				constraint.addCollection(collection);
				context.addConstraint(constraint);
			}
		};
		tomcat.addAdditionalTomcatConnectors(httpConnector());
		return tomcat;
	}
	@Bean
	public Connector httpConnector() {
		Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
		//将http的访问端口重定向到https的访问端口
		connector.setScheme("http");
		//http的访问端口
		connector.setPort(80);
		connector.setSecure(false);
		//https的访问端口 就是ssl证书的端口  默认是443
		connector.setRedirectPort(8888);
		return connector;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值