From 8d2c24da337bd1f858c1e04aba83b5174b32965b Mon Sep 17 00:00:00 2001
From: geek <1163518793@qq.com>
Date: Fri, 17 Jan 2020 11:13:19 +0800
Subject: [PATCH 1/4] =?UTF-8?q?=E6=8F=90=E4=BA=A4dev=20=E5=88=86=E6=94=AF?=
=?UTF-8?q?=20https=20=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
spring-boot-demo-https/.gitignore | 31 ++++++++
spring-boot-demo-https/README.md | 68 ++++++++++++++++++
spring-boot-demo-https/pom.xml | 56 +++++++++++++++
.../SpringBootDemoHttpsApplication.java | 62 ++++++++++++++++
.../src/main/resources/application.yml | 12 ++++
.../src/main/resources/server.keystore | Bin 0 -> 2255 bytes
.../src/main/resources/static/index.html | 13 ++++
.../SpringBootDemoHttpsApplicationTests.java | 13 ++++
spring-boot-demo-https/ssl.png | Bin 0 -> 78633 bytes
9 files changed, 255 insertions(+)
create mode 100644 spring-boot-demo-https/.gitignore
create mode 100644 spring-boot-demo-https/README.md
create mode 100644 spring-boot-demo-https/pom.xml
create mode 100644 spring-boot-demo-https/src/main/java/com/xkcoding/springbootdemohttps/SpringBootDemoHttpsApplication.java
create mode 100644 spring-boot-demo-https/src/main/resources/application.yml
create mode 100644 spring-boot-demo-https/src/main/resources/server.keystore
create mode 100644 spring-boot-demo-https/src/main/resources/static/index.html
create mode 100644 spring-boot-demo-https/src/test/java/com/xkcoding/springbootdemohttps/SpringBootDemoHttpsApplicationTests.java
create mode 100644 spring-boot-demo-https/ssl.png
diff --git a/spring-boot-demo-https/.gitignore b/spring-boot-demo-https/.gitignore
new file mode 100644
index 000000000..a2a3040aa
--- /dev/null
+++ b/spring-boot-demo-https/.gitignore
@@ -0,0 +1,31 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**
+!**/src/test/**
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+
+### VS Code ###
+.vscode/
diff --git a/spring-boot-demo-https/README.md b/spring-boot-demo-https/README.md
new file mode 100644
index 000000000..aab869f6d
--- /dev/null
+++ b/spring-boot-demo-https/README.md
@@ -0,0 +1,68 @@
+# Getting Started
+
+### Reference Documentation
+For further reference, please consider the following sections:
+
+* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
+* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/maven-plugin/)
+
+
+
+1. 首先使用jdk 自带的keytool 命令生成证书(一般在用户目录下C:\Users\Administrator\server.keystore) 复制到项目中
+> 自己生成的证书浏览器会有危险提示,去ssl网站上使用金钱申请则不会
+
+
+
+
+2. 然后添加配置
+```yml
+server:
+ ssl:
+ # 证书路径
+ key-store: spring-boot-demo-https\src\main\resources\server.keystore
+ key-alias: tomcat
+ enabled: true
+ key-store-type: JKS
+ #与申请时输入一致
+ key-store-password: 123456
+ # 浏览器默认端口 和 80 类似
+ port: 443
+#debug: true
+
+
+```
+
+3. 需要与http 自动跳转再添加bean
+
+```java
+
+ @Bean
+ public Connector connector(){
+ Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
+ connector.setScheme("http");
+ connector.setPort(80);
+ connector.setSecure(false);
+ connector.setRedirectPort(443);
+ return connector;
+ }
+
+ @Bean
+ public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
+ TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
+ @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(connector);
+ return tomcat;
+ }
+
+```
+
+
diff --git a/spring-boot-demo-https/pom.xml b/spring-boot-demo-https/pom.xml
new file mode 100644
index 000000000..d90ec5eff
--- /dev/null
+++ b/spring-boot-demo-https/pom.xml
@@ -0,0 +1,56 @@
+
+
+ * SpringBoot启动类 + *
+ * + * @package: com.xkcoding.https + * @description: SpringBoot启动类 + * @author: Chen.Chao + * @date 2020.01.12 10:31 am + * @copyright: Copyright (c) + * @version: V1.0 + * @modified: Chen.Chao + */ +@SpringBootApplication +public class SpringBootDemoHttpsApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoHttpsApplication.class, args); + } + + + @Bean + public Connector connector(){ + Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol"); + connector.setScheme("http"); + connector.setPort(80); + connector.setSecure(false); + connector.setRedirectPort(443); + return connector; + } + + @Bean + public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){ + TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){ + @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(connector); + return tomcat; + } + + +} diff --git a/spring-boot-demo-https/src/main/resources/application.yml b/spring-boot-demo-https/src/main/resources/application.yml new file mode 100644 index 000000000..d6d2def4b --- /dev/null +++ b/spring-boot-demo-https/src/main/resources/application.yml @@ -0,0 +1,12 @@ +server: + ssl: + # 证书路径 + key-store: spring-boot-demo-https\src\main\resources\server.keystore + key-alias: tomcat + enabled: true + key-store-type: JKS + #与申请时输入一致 + key-store-password: 123456 + # 浏览器默认端口 和 80 类似 + port: 443 +#debug: true diff --git a/spring-boot-demo-https/src/main/resources/server.keystore b/spring-boot-demo-https/src/main/resources/server.keystore new file mode 100644 index 0000000000000000000000000000000000000000..a6b59ffd9cdb35963cb3374ffdfd1e42fe3c151e GIT binary patch literal 2255 zcmc(g`8U*y8^>p}&Dck_NtDPIpBZZ=SBy*9WeG*2vCYWX#!YBwgs!dZ`!=#=Eo7NN zMMYobibh16WH;7G4OgA}J?DP^g6|K{5AWAG?{l8>exCC@yGy%EAP@+$U*KQGO$qQP zcu@9bKz2f190}z*yirHxD}_e=^$ky2bD~m;ouoybefEP+@D(O
zW;e)4stje=460Sz{G@VQ5-*5u*or#t}e1hw|-?+@^uYlSMoz%W*uX_;e`vH6AuMHmY@Oc`Nwbz5hjh1
zGGZLM{>GA$w>C&u!x$m0otB>LpD8(I9u`S@TLiNh=6qZfml?LoRUXQMH_*7-;~|(L
z6I!FJkIaGh7cS%h1z^sKz2JRIve`tAh!^><6;&SlfSDz9*YcvXSfBB&pE}-gUJfI0
z%i^4M3iBpG;n0ROJ?8HBSb8;C`8_;ObV|ip~l2A35d@Qmm|uu
z{|o_2`le-;a)43o1p0j@fKPf&PaW#;$qTH9C(HD=Q