未读代码 未读代码
首页
  • Java 18 新功能介绍
  • Java 17 新功能介绍
  • Java 16 新功能介绍
  • Java 15 新功能介绍
  • Java 14 新功能介绍
  • Java 8 新特性

    • Java 8 Lambda 表达式
    • Java 8 Stream 流式操作
    • Java 8 时间处理介绍
    • Java 8 Optional 介绍
  • Java 开发工具
Java 源码分析
Spring Boot 系列
  • Arthas 问题定位
  • JMH 基准测试
GitHub (opens new window)
首页
  • Java 18 新功能介绍
  • Java 17 新功能介绍
  • Java 16 新功能介绍
  • Java 15 新功能介绍
  • Java 14 新功能介绍
  • Java 8 新特性

    • Java 8 Lambda 表达式
    • Java 8 Stream 流式操作
    • Java 8 时间处理介绍
    • Java 8 Optional 介绍
  • Java 开发工具
Java 源码分析
Spring Boot 系列
  • Arthas 问题定位
  • JMH 基准测试
GitHub (opens new window)
  • SpringBoot教程

    • Spring Boot 入门篇
    • Spring Boot 配置文件
    • Spring Boot 自动配置
    • Spring Boot 日志框架
    • Spring Boot Web 开发之静态资源和模版引擎
    • Spring Boot Web 开发之拦截器和三大组件
    • Spring Boot Web 开发之异常错误处理机制剖析
    • Spring Boot 动态Banner与图片转字符图案的手动实现
    • Spring Boot 使用 Spring JDBC 和 Druid 数据源监控
    • Spring Boot 使用 Spring data jpa 访问数据库
    • Spring Boot 使用 Mybatis(自动生成插件) 访问数据库
    • Spring Boot 使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
    • Spring Boot 使用邮件服务
    • Spring Boot 迅速启用 HTTPS 加密你的网站
      • 1. 获取 HTTPS 证书
      • 2. 配置 HTTPS 证书
      • 3. 测试 HTTPS 证书
      • 4. HTTP 跳转 HTTPS
      • 附录
    • Spring Boot 如何编写自己的 Spring Boot starter
    • Spring Boot 使用 Swagger 文档
    • Spring Boot Admin 监控你的 Spring Boot 程序
    • Spring Boot 多模块开发与排坑指南
    • Spring Boot 的多数据源配置
  • SpringBoot
  • SpringBoot教程
程序猿阿朗
2019-08-07

Spring Boot 迅速启用 HTTPS 加密你的网站

HTTPS

# 1. 获取 HTTPS 证书

正常情况下 HTTPS 证书需要从证书授权中心获得,这样获得的证书才具有公信力,也会被各种浏览器客户端所认可。常见的证书品牌如 Symantec,GeoTrustm,TrustAsia,Symantec 等。不过在 Springboot 的 HTTPS 实验中就没有必要去申请了,我们可以使用 Java 自带的 keytool 生成 HTTPS 证书。

查看 keytool 工具使用说明。

D:\>keytool
密钥和证书管理工具
命令:
 -certreq            生成证书请求
 -changealias        更改条目的别名
 -delete             删除条目
 -exportcert         导出证书
 -genkeypair         生成密钥对
 -genseckey          生成密钥
 -gencert            根据证书请求生成证书
 -importcert         导入证书或证书链
 -importpass         导入口令
 -importkeystore     从其他密钥库导入一个或所有条目
 -keypasswd          更改条目的密钥口令
 -list               列出密钥库中的条目
 -printcert          打印证书内容
 -printcertreq       打印证书请求的内容
 -printcrl           打印 CRL 文件的内容
 -storepasswd        更改密钥库的存储口令

使用 "keytool -command_name -help" 获取 command_name 的用法

D:\>keytool -genkeypair --help
keytool -genkeypair [OPTION]...
生成密钥对
选项:
 -alias <alias>                  要处理的条目的别名
 -keyalg <keyalg>                密钥算法名称
 -keysize <keysize>              密钥位大小
 -sigalg <sigalg>                签名算法名称
 -destalias <destalias>          目标别名
 -dname <dname>                  唯一判别名
 -startdate <startdate>          证书有效期开始日期/时间
 -ext <value>                    X.509 扩展
 -validity <valDays>             有效天数
 -keypass <arg>                  密钥口令
 -keystore <keystore>            密钥库名称
 -storepass <arg>                密钥库口令
 -storetype <storetype>          密钥库类型
 -providername <providername>    提供方名称
 -providerclass <providerclass>  提供方类名
 -providerarg <arg>              提供方参数
 -providerpath <pathlist>        提供方类路径
 -v                              详细输出
 -protected                      通过受保护的机制的口令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

通过上面的 keytool ,我们生成自己的自签名证书。

D:\>keytool -genkeypair -alias tomcat_https -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore d:/tomcat_https.keystore -storepass 123456
您的名字与姓氏是什么?
  [Unknown]:  darcy
您的组织单位名称是什么?
  [Unknown]:  codingme
您的组织名称是什么?
  [Unknown]:  codingme
您所在的城市或区域名称是什么?
  [Unknown]:  ShangHai
您所在的省/市/自治区名称是什么?
  [Unknown]:  ShangHai
该单位的双字母国家/地区代码是什么?
  [Unknown]:  ZN
CN=darcy, OU=codingme, O=codingme, L=ShangHai, ST=ShangHai, C=ZN是否正确?
  [否]:  y
D:\>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

这时候已经在我们指定的位置下生成了证书文件,如果需要查看证书信息,可以使用 keytool 的 list 命令,可以看到密钥库类型是 JKS,在后面的配置里会用到。

D:\>keytool -list -keystore tomcat_https.keystore
输入密钥库口令:

密钥库类型: JKS
密钥库提供方: SUN

您的密钥库包含 1 个条目

tomcat_https, 2019-4-21, PrivateKeyEntry,
证书指纹 (SHA1): 1E:5F:15:9C:45:BD:D3:2A:7E:7F:1F:83:56:B8:74:E0:8B:CA:FD:F6

D:\>
1
2
3
4
5
6
7
8
9
10
11
12

自己生成的 HTTPS 证书只能用来自己测试,真正用于网络上时,浏览器会显示证书无法信息。因此如果想要得到一个真实有效的证书,请看文章末尾。

# 2. 配置 HTTPS 证书

创建一个 Springboot 项目这里不提,拷贝上一步骤中生成的 tomcat_https.keystore 证书文件到src/main/resource 文件夹下,先看下总体的项目结构。

项目结构如下

然后在 application.yml 文件中配置 HTTPS 相关信息。直接配置了端口号为 443,443是 HTTPS 的默认端口,这样在使用 HTTPS 就行访问的时候就不需要写额外的端口号了。

# 配置 HTTPS 相关信息
server:
  port: 443
  http-port: 80 # 为了后面的配置使用,暂时无用
  ssl:
    enabled: true
    key-store: classpath:tomcat_https.keystore # 证书文件
    key-password: 123456  # 密码
    key-store-type: JKS # 密钥库类型
    key-alias: tomcat_https
1
2
3
4
5
6
7
8
9
10

这时,已经可以通过 HTTPS 进行页面访问了。

# 3. 测试 HTTPS 证书

直接编写一个 接口用于测试。

/**
 * <p>
 * Https 接口控制类
 *
 * @Author niujinpeng
 * @Date 2019/4/20 22:59
 */
@RestController
public class HttpsController {

    @GetMapping(value = "/hello")
    public String hello() {
        return "Hello HTTPS";
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

启动之后可以通过 https://localhost/hello (opens new window) 进行访问了。

HTTPS 访问测试

当然,由于是自己生成的证书,会提示不安全,继续访问即可,如果是正常申请或者购买的证书就不会有这个问题的。

# 4. HTTP 跳转 HTTPS

在上面的测试里,HTTPS 已经可以访问了,但是 HTTP 却不能访问,大多数情况下在启用了 HTTPS 之后,都会希望 HTTP 的请求会自动跳转到 HTTPS,这个在 Springboot 里自然也是可以实现的。我们只需要写一个配置类把 HTTP 请求直接转发到 HTTPS 即可。

/**
 * <p>
 * HTTP 强制跳转 HTTPS
 *
 * @Author niujinpeng
 * @Date 2019/4/21 17:47
 */
@Configuration
public class Http2Https {

    @Value("${server.port}")
    private int sslPort;
    @Value("${server.http-port}")
    private int httpPort;

    @Bean
    public TomcatServletWebServerFactory servletContainerFactory() {
        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);
            }
        };
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(httpPort);
        connector.setRedirectPort(sslPort);
        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

再次启动之后,使用 http://localhost/hello (opens new window) 访问会自动跳转到 https://localhost/hello (opens new window).

# 附录

如果需要申请免费证书,可以在腾讯云上免费申请,请参考:

  • 免费版 DV SSL 证书申请 (opens new window)。

如果想要自己安装证书,请参考:

  • Apache 服务器证书安装 (opens new window)
  • Nginx 服务器证书安装 (opens new window)
  • Tomcat 服务器证书安装 (opens new window)
  • Windows IIS 服务器证书安装 (opens new window)

🐟 文章相关代码已经上传 Github Spring Boot https (opens new window), 欢迎⭐Star️,欢迎 Fork !

订阅

文章持续更新,订阅可以关注「 程序猿阿朗 」公众号或者未读代码博客。

文章作者: 程序猿阿朗
文章链接:https://www.wdbyte.com/2019/08/springboot/springboot-14-https/
版权声明:本网站当前文章采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 未读代码!
#Springboot#Https
上次更新: 2022/12/05, 08:18:32
Spring Boot 使用邮件服务
Spring Boot 如何编写自己的 Spring Boot starter

← Spring Boot 使用邮件服务 Spring Boot 如何编写自己的 Spring Boot starter→

最近更新
01
如何搭建一个自己的音乐服务器
12-04
02
JUnit 5 单元测试教程
11-17
03
使用 StringUtils.split 的坑
11-02
更多文章>

提示:评论前请刷新页面,否则评论的可能不是当前文章。

Theme by Vdoing | Copyright © 2018-2022 程序猿阿朗 | MIT License | 皖ICP备20000567号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式