生成证书

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

自己生成的证书浏览器会有危险提示,去ssl网站上申请则不会「付费」

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
keytool -genkey -alias bjtcrj -dname "CN=Andy,OU=kfit,O=kfit,L=HaiDian,ST=BeiJing,C=CN" -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore server.p12 -validity 365

//输入密钥库口令:bjtcrj
// key-alias bjtcrj
// key-store server.p12
// key-store-type: PKCS12
// key-store-password: bjtcrj

-genkey :生成key;
-alias :key的别名;
-dname:指定证书拥有者信息
-storetype :密钥库的类型为JCEKS。常用的有JKS(默认),JCEKS(推荐),PKCS12,BKS,UBER。每个密钥库只可以是其中一种类型。
-keyalg :DSA或RSA算法(当使用-genkeypair参数),DES或DESede或AES算法(当使用-genseckey参数);
-keysize :密钥的长度为512至1024之间(64的倍数)
-keystore :证书库的名称
-validity : 指定创建的证书有效期多少天

dname的值详解:
CN(Common Name名字与姓氏)
OU(Organization Unit组织单位名称)
O(Organization组织名称)
L(Locality城市或区域名称)
ST(State州或省份名称)
C(Country国家名称)

2. 添加配置

  1. 在配置文件配置生成的证书;将 server.p12 文件放到 resources 目录下
1
2
3
4
5
6
7
8
9
10
11
server:
ssl:
# 证书路径
key-store: classpath:server.p12
key-alias: bjtcrj
enabled: true
key-store-type: PKCS12
#与申请时输入一致
key-store-password: bjtcrj
# 浏览器默认端口 和 80 类似
port: 443
  1. 打包配置 pom.xml

    打包不处理证书文件,否则内容变化后启动会失败:Caused by: java.lang.IllegalArgumentException: Invalid keystore format

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <excludes>
    <exclude>*.p12</exclude>
    </excludes>
    </resource>
    <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <includes>
    <include>*.p12</include>
    </includes>
    </resource>
    </resources>
    </build>
  2. 配置 Tomcat 【非必需】

    配置 http(80) -> 强制跳转到 https(443)

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
/**
* <p>
* HTTPS 配置类
* </p>
*
* @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命令参考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ 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 的用法