2020-01-19 10:38:43 +08:00
|
|
|
|
# spring-boot-demo-https
|
2020-01-12 12:22:42 +08:00
|
|
|
|
|
2020-01-19 10:38:43 +08:00
|
|
|
|
> 此 demo 主要演示了 Spring Boot 如何集成 https
|
2020-01-12 12:22:42 +08:00
|
|
|
|
|
2020-01-19 10:38:43 +08:00
|
|
|
|
## 1. 生成证书
|
2020-01-12 12:22:42 +08:00
|
|
|
|
|
2020-01-19 10:38:43 +08:00
|
|
|
|
首先使用 jdk 自带的 keytool 命令生成证书复制到项目的 `resources` 目录下(生成的证书一般在用户目录下 C:\Users\Administrator\server.keystore)
|
2020-01-12 12:22:42 +08:00
|
|
|
|
|
|
|
|
|
|
> 自己生成的证书浏览器会有危险提示,去ssl网站上使用金钱申请则不会
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
2020-01-19 10:38:43 +08:00
|
|
|
|
## 2. 添加配置
|
|
|
|
|
|
|
|
|
|
|
|
1. 在配置文件配置生成的证书
|
2020-01-12 12:22:42 +08:00
|
|
|
|
|
2020-01-19 10:38:43 +08:00
|
|
|
|
```yaml
|
2020-01-12 12:22:42 +08:00
|
|
|
|
server:
|
|
|
|
|
|
ssl:
|
|
|
|
|
|
# 证书路径
|
2020-01-19 10:38:43 +08:00
|
|
|
|
key-store: classpath:server.keystore
|
2020-01-12 12:22:42 +08:00
|
|
|
|
key-alias: tomcat
|
|
|
|
|
|
enabled: true
|
|
|
|
|
|
key-store-type: JKS
|
|
|
|
|
|
#与申请时输入一致
|
|
|
|
|
|
key-store-password: 123456
|
|
|
|
|
|
# 浏览器默认端口 和 80 类似
|
|
|
|
|
|
port: 443
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2020-01-19 10:38:43 +08:00
|
|
|
|
2. 配置 Tomcat
|
2020-01-12 12:22:42 +08:00
|
|
|
|
|
|
|
|
|
|
```java
|
2020-01-19 10:38:43 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
* HTTPS 配置类
|
|
|
|
|
|
* </p>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author yangkai.shen
|
2020-10-25 11:27:29 +08:00
|
|
|
|
* @date Created in 2020-01-19 10:31
|
2020-01-19 10:38:43 +08:00
|
|
|
|
*/
|
|
|
|
|
|
@Configuration
|
|
|
|
|
|
public class HttpsConfig {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 配置 http(80) -> 强制跳转到 https(443)
|
|
|
|
|
|
*/
|
2020-01-12 12:22:42 +08:00
|
|
|
|
@Bean
|
2020-01-19 10:38:43 +08:00
|
|
|
|
public Connector connector() {
|
|
|
|
|
|
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
|
2020-01-12 12:22:42 +08:00
|
|
|
|
connector.setScheme("http");
|
|
|
|
|
|
connector.setPort(80);
|
|
|
|
|
|
connector.setSecure(false);
|
|
|
|
|
|
connector.setRedirectPort(443);
|
|
|
|
|
|
return connector;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
2020-01-19 10:38:43 +08:00
|
|
|
|
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
|
|
|
|
|
|
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
|
2020-01-12 12:22:42 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
protected void postProcessContext(Context context) {
|
2020-01-19 10:38:43 +08:00
|
|
|
|
SecurityConstraint securityConstraint = new SecurityConstraint();
|
2020-01-12 12:22:42 +08:00
|
|
|
|
securityConstraint.setUserConstraint("CONFIDENTIAL");
|
2020-01-19 10:38:43 +08:00
|
|
|
|
SecurityCollection collection = new SecurityCollection();
|
2020-01-12 12:22:42 +08:00
|
|
|
|
collection.addPattern("/*");
|
|
|
|
|
|
securityConstraint.addCollection(collection);
|
|
|
|
|
|
context.addConstraint(securityConstraint);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
tomcat.addAdditionalTomcatConnectors(connector);
|
|
|
|
|
|
return tomcat;
|
|
|
|
|
|
}
|
2020-01-19 10:38:43 +08:00
|
|
|
|
}
|
2020-01-12 12:22:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2020-01-19 10:38:43 +08:00
|
|
|
|
## 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 的用法
|
|
|
|
|
|
```
|
2020-01-12 12:22:42 +08:00
|
|
|
|
|
2020-10-25 11:27:29 +08:00
|
|
|
|
- [Java Keytool工具简介](https://blog.csdn.net/liumiaocn/article/details/61921014)
|