From a08bc5fd5112cae1a0a18d61e9497346f28ad351 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 20 Aug 2022 13:59:37 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=85=B6=E4=BB=96=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B9=8B=20https=20=E6=A1=88=E4=BE=8B=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo-https/.gitignore | 31 ---- demo-https/README.md | 110 ------------- demo-https/src/main/resources/application.yml | 11 -- demo-https/src/main/resources/server.keystore | Bin 2255 -> 0 bytes demo-https/ssl.png | Bin 78633 -> 0 bytes demo-others/demo-others-https/README.md | 148 ++++++++++++++++++ .../demo-others-https}/pom.xml | 22 +-- .../com/xkcoding/https/HttpsApplication.java | 4 +- .../xkcoding/https/config/HttpsConfig.java | 4 +- .../src/main/resources/application.yml | 13 ++ .../src/main/resources/spring-boot-demo.key | Bin 0 -> 2812 bytes .../src/main/resources/static/index.html | 4 +- .../xkcoding/https/HttpsApplicationTests.java | 4 +- demo-others/pom.xml | 4 + pom.xml | 1 - 15 files changed, 184 insertions(+), 172 deletions(-) delete mode 100644 demo-https/.gitignore delete mode 100644 demo-https/README.md delete mode 100644 demo-https/src/main/resources/application.yml delete mode 100644 demo-https/src/main/resources/server.keystore delete mode 100644 demo-https/ssl.png create mode 100644 demo-others/demo-others-https/README.md rename {demo-https => demo-others/demo-others-https}/pom.xml (78%) rename demo-https/src/main/java/com/xkcoding/https/SpringBootDemoHttpsApplication.java => demo-others/demo-others-https/src/main/java/com/xkcoding/https/HttpsApplication.java (72%) rename {demo-https => demo-others/demo-others-https}/src/main/java/com/xkcoding/https/config/HttpsConfig.java (95%) create mode 100644 demo-others/demo-others-https/src/main/resources/application.yml create mode 100644 demo-others/demo-others-https/src/main/resources/spring-boot-demo.key rename {demo-https => demo-others/demo-others-https}/src/main/resources/static/index.html (61%) rename demo-https/src/test/java/com/xkcoding/https/SpringBootDemoHttpsApplicationTests.java => demo-others/demo-others-https/src/test/java/com/xkcoding/https/HttpsApplicationTests.java (69%) diff --git a/demo-https/.gitignore b/demo-https/.gitignore deleted file mode 100644 index a2a3040..0000000 --- a/demo-https/.gitignore +++ /dev/null @@ -1,31 +0,0 @@ -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/demo-https/README.md b/demo-https/README.md deleted file mode 100644 index 042cb6f..0000000 --- a/demo-https/README.md +++ /dev/null @@ -1,110 +0,0 @@ -# spring-boot-demo-https - -> 此 demo 主要演示了 Spring Boot 如何集成 https - -## 1. 生成证书 - -首先使用 jdk 自带的 keytool 命令生成证书复制到项目的 `resources` 目录下(生成的证书一般在用户目录下 C:\Users\Administrator\server.keystore) - -> 自己生成的证书浏览器会有危险提示,去ssl网站上使用金钱申请则不会 - - - -## 2. 添加配置 - -1. 在配置文件配置生成的证书 - -```yaml -server: - ssl: - # 证书路径 - key-store: classpath:server.keystore - key-alias: tomcat - enabled: true - key-store-type: JKS - #与申请时输入一致 - key-store-password: 123456 - # 浏览器默认端口 和 80 类似 - port: 443 -``` - -2. 配置 Tomcat - -```java -/** - *
- * HTTPS 配置类 - *
- * - * @author yangkai.shen - * @date Created in 2020-01-19 10:31 - */ -@Configuration -public class HttpsConfig { - /** - * 配置 http(80) -> 强制跳转到 https(443) - */ - @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; - } -} -``` - -## 3. 测试 - -启动项目,浏览器访问 http://localhost 将自动跳转到 https://localhost - -## 4. 参考 - -- `keytool`命令参考 - -```bash -$ keytool --help -密钥和证书管理工具 - -命令: - - -certreq 生成证书请求 - -changealias 更改条目的别名 - -delete 删除条目 - -exportcert 导出证书 - -genkeypair 生成密钥对 - -genseckey 生成密钥 - -gencert 根据证书请求生成证书 - -importcert 导入证书或证书链 - -importpass 导入口令 - -importkeystore 从其他密钥库导入一个或所有条目 - -keypasswd 更改条目的密钥口令 - -list 列出密钥库中的条目 - -printcert 打印证书内容 - -printcertreq 打印证书请求的内容 - -printcrl 打印 CRL 文件的内容 - -storepasswd 更改密钥库的存储口令 - -使用 "keytool -command_name -help" 获取 command_name 的用法 -``` - -- [Java Keytool工具简介](https://blog.csdn.net/liumiaocn/article/details/61921014) diff --git a/demo-https/src/main/resources/application.yml b/demo-https/src/main/resources/application.yml deleted file mode 100644 index 21ad6fc..0000000 --- a/demo-https/src/main/resources/application.yml +++ /dev/null @@ -1,11 +0,0 @@ -server: - ssl: - # 证书路径 - key-store: classpath:server.keystore - key-alias: tomcat - enabled: true - key-store-type: JKS - #与申请时输入一致 - key-store-password: 123456 - # 浏览器默认端口 和 80 类似 - port: 443 diff --git a/demo-https/src/main/resources/server.keystore b/demo-https/src/main/resources/server.keystore deleted file mode 100644 index a6b59ffd9cdb35963cb3374ffdfd1e42fe3c151e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 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