spring boot/cloud 添加支持https +docker中 - Go语言中文社区

spring boot/cloud 添加支持https +docker中


1、在阿里云上申请证书,在此不赘述

生成证书的zip压缩包解压后文件目录如下:


我们使用Tomcat里面的jks文件

2、application.yml配置https的相关信息

server:
  port: 8097
  ssl:
    key-store: classpath:www.xiaoyaozhou.xyz.jks
    key-store-password: 75zcpi2j1q76

3、将jks文件放在application.yml的同级目录下


4、在Application启动类上添加http自动转向https的bean

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.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class Eurekaclient1Application {
	
	@Value("${server.port}")
	Integer port;
	
	@Bean
	public ServletWebServerFactory 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");
		connector.setScheme("http");
		// Connector监听的http的端口号
		connector.setPort(8080);
		connector.setSecure(false);
		// 监听到http的端口号后转向到的https的端口号
		connector.setRedirectPort(port);
		return connector;
	}
	

	public static void main(String[] args) {
		SpringApplication.run(Eurekaclient1Application.class, args);
	}
	
	@RequestMapping("/hi")
	public String home(@RequestParam String name) {
		return "hi " + name + ",i am from port:" + port;
	}
}

注意:这个是spring boot 2.0.x版本以上的代码。如果是这个版本以下的,需要修改如下:

@Bean
	public EmbeddedServletContainerFactory servletContainer() {
		TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
			@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;
	}

5.启动spring boot项目,显示两个端口都启动了


6.docker中创建容器并运行

docker run -d -p 8097:8097 -p 8080:8080 --name test testImage

-p 要有将两个端口号都映射(之前不了解docker,走了些弯路)

test 是容器名

testImage是镜像名

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/xifanxifano/article/details/80733376
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 20:18:05
  • 阅读 ( 891 )
  • 分类:docker

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢