Skip to content

Commit 86fb3db

Browse files
committed
✨ spring-boot-demo-https 完成
1 parent 6bf6da5 commit 86fb3db

File tree

9 files changed

+103
-185
lines changed

9 files changed

+103
-185
lines changed

spring-boot-demo-https/.mvn/wrapper/MavenWrapperDownloader.java

Lines changed: 0 additions & 118 deletions
This file was deleted.
-49.5 KB
Binary file not shown.

spring-boot-demo-https/.mvn/wrapper/maven-wrapper.properties

Lines changed: 0 additions & 2 deletions
This file was deleted.

spring-boot-demo-https/README.md

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
1-
# Getting Started
1+
# spring-boot-demo-https
22

3-
### Reference Documentation
4-
For further reference, please consider the following sections:
3+
> 此 demo 主要演示了 Spring Boot 如何集成 https
54
6-
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
7-
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/maven-plugin/)
5+
## 1. 生成证书
86

7+
首先使用 jdk 自带的 keytool 命令生成证书复制到项目的 `resources` 目录下(生成的证书一般在用户目录下 C:\Users\Administrator\server.keystore)
98

10-
11-
1. 首先使用jdk 自带的keytool 命令生成证书(一般在用户目录下C:\Users\Administrator\server.keystore) 复制到项目中
129
> 自己生成的证书浏览器会有危险提示,去ssl网站上使用金钱申请则不会
1310
1411
![ssl 命令截图](ssl.png)
1512

13+
## 2. 添加配置
14+
15+
1. 在配置文件配置生成的证书
1616

17-
2. 然后添加配置
18-
```yml
17+
```yaml
1918
server:
2019
ssl:
2120
# 证书路径
22-
key-store: spring-boot-demo-https\src\main\resources\server.keystore
21+
key-store: classpath:server.keystore
2322
key-alias: tomcat
2423
enabled: true
2524
key-store-type: JKS
2625
#与申请时输入一致
2726
key-store-password: 123456
2827
# 浏览器默认端口 和 80 类似
2928
port: 443
30-
#debug: true
31-
32-
3329
```
3430
35-
3. 需要与http 自动跳转再添加bean
31+
2. 配置 Tomcat
3632
3733
```java
38-
34+
/**
35+
* <p>
36+
* HTTPS 配置类
37+
* </p>
38+
*
39+
* @author yangkai.shen
40+
* @date Created in 2020/1/19 10:31
41+
*/
42+
@Configuration
43+
public class HttpsConfig {
44+
/**
45+
* 配置 http(80) -> 强制跳转到 https(443)
46+
*/
3947
@Bean
40-
public Connector connector(){
41-
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
48+
public Connector connector() {
49+
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
4250
connector.setScheme("http");
4351
connector.setPort(80);
4452
connector.setSecure(false);
@@ -47,13 +55,13 @@ server:
4755
}
4856

4957
@Bean
50-
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
51-
TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
58+
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
59+
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
5260
@Override
5361
protected void postProcessContext(Context context) {
54-
SecurityConstraint securityConstraint=new SecurityConstraint();
62+
SecurityConstraint securityConstraint = new SecurityConstraint();
5563
securityConstraint.setUserConstraint("CONFIDENTIAL");
56-
SecurityCollection collection=new SecurityCollection();
64+
SecurityCollection collection = new SecurityCollection();
5765
collection.addPattern("/*");
5866
securityConstraint.addCollection(collection);
5967
context.addConstraint(securityConstraint);
@@ -62,7 +70,41 @@ server:
6270
tomcat.addAdditionalTomcatConnectors(connector);
6371
return tomcat;
6472
}
65-
73+
}
6674
```
6775

76+
## 3. 测试
77+
78+
启动项目,浏览器访问 http://localhost 将自动跳转到 https://localhost
79+
80+
## 4. 参考
81+
82+
- `keytool`命令参考
83+
84+
```bash
85+
$ keytool --help
86+
密钥和证书管理工具
87+
88+
命令:
89+
90+
-certreq 生成证书请求
91+
-changealias 更改条目的别名
92+
-delete 删除条目
93+
-exportcert 导出证书
94+
-genkeypair 生成密钥对
95+
-genseckey 生成密钥
96+
-gencert 根据证书请求生成证书
97+
-importcert 导入证书或证书链
98+
-importpass 导入口令
99+
-importkeystore 从其他密钥库导入一个或所有条目
100+
-keypasswd 更改条目的密钥口令
101+
-list 列出密钥库中的条目
102+
-printcert 打印证书内容
103+
-printcertreq 打印证书请求的内容
104+
-printcrl 打印 CRL 文件的内容
105+
-storepasswd 更改密钥库的存储口令
106+
107+
使用 "keytool -command_name -help" 获取 command_name 的用法
108+
```
68109

110+
- [Java Keytool工具简介](https://blog.csdn.net/liumiaocn/article/details/61921014)

spring-boot-demo-https/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.xkcoding</groupId>
76
<artifactId>spring-boot-demo-https</artifactId>
87
<version>0.0.1-SNAPSHOT</version>
98
<name>spring-boot-demo-https</name>
109
<description>Demo project for Spring Boot</description>
1110

12-
1311
<parent>
1412
<groupId>com.xkcoding</groupId>
1513
<artifactId>spring-boot-demo</artifactId>
@@ -23,10 +21,6 @@
2321
</properties>
2422

2523
<dependencies>
26-
<dependency>
27-
<groupId>org.springframework.boot</groupId>
28-
<artifactId>spring-boot-starter</artifactId>
29-
</dependency>
3024
<dependency>
3125
<groupId>org.springframework.boot</groupId>
3226
<artifactId>spring-boot-starter-web</artifactId>
@@ -35,12 +29,6 @@
3529
<groupId>org.springframework.boot</groupId>
3630
<artifactId>spring-boot-starter-test</artifactId>
3731
<scope>test</scope>
38-
<exclusions>
39-
<exclusion>
40-
<groupId>org.junit.vintage</groupId>
41-
<artifactId>junit-vintage-engine</artifactId>
42-
</exclusion>
43-
</exclusions>
4432
</dependency>
4533
</dependencies>
4634

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.xkcoding.https;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* <p>
8+
* 启动类
9+
* </p>
10+
*
11+
* @author Chen.Chao
12+
* @date Created in 2020/1/12 10:31
13+
*/
14+
@SpringBootApplication
15+
public class SpringBootDemoHttpsApplication {
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(SpringBootDemoHttpsApplication.class, args);
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
1-
package com.xkcoding.springbootdemohttps;
1+
package com.xkcoding.https.config;
22

33
import org.apache.catalina.Context;
44
import org.apache.catalina.connector.Connector;
55
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
66
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
7-
import org.springframework.boot.SpringApplication;
8-
import org.springframework.boot.autoconfigure.SpringBootApplication;
97
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
108
import org.springframework.context.annotation.Bean;
11-
9+
import org.springframework.context.annotation.Configuration;
1210

1311
/**
1412
* <p>
15-
* SpringBoot启动类
13+
* HTTPS 配置类
1614
* </p>
1715
*
18-
* @package: com.xkcoding.https
19-
* @description: SpringBoot启动类
20-
* @author: Chen.Chao
21-
* @date 2020.01.12 10:31 am
22-
* @copyright: Copyright (c)
23-
* @version: V1.0
24-
* @modified: Chen.Chao
16+
* @author Chen.Chao
17+
* @date Created in 2020/1/12 10:31
2518
*/
26-
@SpringBootApplication
27-
public class SpringBootDemoHttpsApplication {
28-
29-
public static void main(String[] args) {
30-
SpringApplication.run(SpringBootDemoHttpsApplication.class, args);
31-
}
32-
33-
19+
@Configuration
20+
public class HttpsConfig {
21+
/**
22+
* 配置 http(80) -> 强制跳转到 https(443)
23+
*/
3424
@Bean
35-
public Connector connector(){
36-
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
25+
public Connector connector() {
26+
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
3727
connector.setScheme("http");
3828
connector.setPort(80);
3929
connector.setSecure(false);
@@ -42,13 +32,13 @@ public Connector connector(){
4232
}
4333

4434
@Bean
45-
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
46-
TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
35+
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
36+
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
4737
@Override
4838
protected void postProcessContext(Context context) {
49-
SecurityConstraint securityConstraint=new SecurityConstraint();
39+
SecurityConstraint securityConstraint = new SecurityConstraint();
5040
securityConstraint.setUserConstraint("CONFIDENTIAL");
51-
SecurityCollection collection=new SecurityCollection();
41+
SecurityCollection collection = new SecurityCollection();
5242
collection.addPattern("/*");
5343
securityConstraint.addCollection(collection);
5444
context.addConstraint(securityConstraint);
@@ -57,6 +47,4 @@ protected void postProcessContext(Context context) {
5747
tomcat.addAdditionalTomcatConnectors(connector);
5848
return tomcat;
5949
}
60-
61-
6250
}

0 commit comments

Comments
 (0)