正规渠道申请并签发的证书,以下部分步骤可以不看,SpringBoot配置方面的倒是可以参考参考。
# 自签证书
MACOS下操作的,Windows和Linux的,与其类似。
# 制作CA根证书
- 创建私钥
openssl genrsa -out ca-key.pem 2048
- 创建证书请求
openssl req -new -out ca-req.csr -key ca-key.pem -config /System/Library/OpenSSL/openssl.cnf
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:GUANGDONG
Locality Name (eg, city) []:ZHUHAI
Organization Name (eg, company) [Internet Widgits Pty Ltd]:YIIM
Organizational Unit Name (eg, section) []:YIIM.NET
Common Name (e.g. server FQDN or YOUR name) []:YIIM Global Root CA
Email Address []:ikantech@163.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:YiIM123!@#
An optional company name []:YIIM
2
3
4
5
6
7
8
9
10
11
12
13
14
- 生成CA自签名证书
openssl x509 -req -in ca-req.csr -out ca-cert.pem -signkey ca-key.pem -days 3650
生成后的CA证书(10年有效):
# 制作服务端证书
- 用Java自带的
keytool
工具生成密钥,命令中如果是IP方式访问用-ext SAN=ip:192.168.18.83,如果是域名用 -ext SAN=dns:www.123.com。
注意
“您的名字与姓氏是什么?”就是你的IP或域名,其他的看着写。
我就用电脑本地的IP来生成了,密码用的是随机密码,正式生成的话,最好keypass和storepass用两个不同的密码。
keytool -genkey -alias yiim -ext SAN=ip:192.168.0.3 -validity 3650 -keyalg RSA -keysize 2048 -keypass kKw01tgQk09wm8OY -storepass kKw01tgQk09wm8OY -keystore yiim.jks
您的名字与姓氏是什么?
[Unknown]: 192.168.0.3
您的组织单位名称是什么?
[Unknown]: YIIM.NET
您的组织名称是什么?
[Unknown]: YIIM
您所在的城市或区域名称是什么?
[Unknown]: ZHUHAI
您所在的省/市/自治区名称是什么?
[Unknown]: GUANGDONG
该单位的双字母国家/地区代码是什么?
[Unknown]: CN
CN=192.168.0.3, OU=YIIM.NET, O=YIIM, L=ZHUHAI, ST=GUANGDONG, C=CN是否正确?
[否]: y
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 用keytool工具生成证书请求
keytool -certreq -alias yiim -sigalg SHA256withRSA -file yiim-req.csr -keypass kKw01tgQk09wm8OY -keystore yiim.jks -storepass kKw01tgQk09wm8OY
- 解决Chrome提示不安全问题
创建一个yiim.ext文件,文件内容:
keyUsage=nonRepudiation,digitalSignature,keyEncipherment
extendedKeyUsage=serverAuth,clientAuth
subjectAltName=@SubjectAlternativeName
[ SubjectAlternativeName ]
IP.1=127.0.0.1
IP.2=192.168.0.3
2
3
4
5
6
7
- 根据证书请求,用CA签证,生成服务端证书
openssl x509 -req -in yiim-req.csr -out yiim-cert.pem -CA ca-cert.pem -CAkey ca-key.pem -days 3650 -CAcreateserial -extfile yiim.ext
生成后的服务端证书(10年有效):
# 向keystore密钥库中导入证书
- 导入CA证书
keytool -import -v -trustcacerts -keypass kKw01tgQk09wm8OY -storepass kKw01tgQk09wm8OY -alias ca -file ca-cert.pem -keystore yiim.jks
- 导入服务端证书
keytool -import -v -trustcacerts -storepass kKw01tgQk09wm8OY -alias yiim -file yiim-cert.pem -keystore yiim.jks
# 制作客户端证书
- 用Java自带的
keytool
工具生成密钥
keytool -genkey -alias client -validity 3650 -keyalg RSA -keysize 2048 -keypass zBy6PCjmfwFrm3lk -storepass zBy6PCjmfwFrm3lk -keystore client.jks
您的名字与姓氏是什么?
[Unknown]: client
您的组织单位名称是什么?
[Unknown]: YIIM.NET
您的组织名称是什么?
[Unknown]: YIIM
您所在的城市或区域名称是什么?
[Unknown]: ZHUHAI
您所在的省/市/自治区名称是什么?
[Unknown]: GUANGDONG
该单位的双字母国家/地区代码是什么?
[Unknown]: CN
CN=client, OU=YIIM.NET, O=YIIM, L=ZHUHAI, ST=GUANGDONG, C=CN是否正确?
[否]: y
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 将JKS转化为P12
keytool -importkeystore -srckeystore client.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore client.p12
- 导出客户端证书
keytool -export -alias client -file client.cer -keystore client.jks -storepass zBy6PCjmfwFrm3lk
- 制作服务端可信证书
keytool -import -v -trustcacerts -storepass wGG1x2u31jcDop1M -alias client -file client.cer -keystore trust_client.jks
# SpringBoot配置
# 配置SSL
server:
port: 443
ssl:
# 服务端证书路径
key-store: /tmp/yiim.jks
# 证书密码
key-store-password: kKw01tgQk09wm8OY
# 证书类型
key-store-type: JKS
# 证书别名
key-alias: yiim
# 证书密码
key-password: kKw01tgQk09wm8OY
# 暂时不开双向认证
client-auth: none
# 开启SSL
enabled: true
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 信任CA证书
# Macos
- 安装CA证书 在MACOS双击ca-cert.pem,点击添加按钮,安装证书
信任CA证书
浏览器查看
在Chrome中打开:https://192.168.0.3/testGet?p=testp
# Linux
直接在Chrome中,Settings->Privacy and Security->Security->Manage certificates->Authorities
然后点击Import,导入CA证书ca-cert.pem
,全部信任并导入就行,然后在Chrome打开https://192.168.0.3/testGet?p=testp
,就正常了。
# Firefox
要在设置里导入CA证书
导入完成之后,就可以正常访问了。
# Windows
- 在Edge中导入
在Edge的设置里,找到管理证书
- 在certmgr中导入
或者在命令提示符中,输入certmgr
,找到受信任的根证书颁发机构->证书
,右键->所有任务->导入。
需要把ca-cert.pem
后缀名改成crt。
- 选择要导入的CA证书
- 证书存储到
受信任的根证书颁发机构
- 信任并导入
Edge导入完成后,需要重启Edge才行。
# 开启双向认证
# SpringBoot配置
在原来配置的基础上,增加trust-store相关配置
server:
ssl:
trust-store: /tmp/trust_client.jks
trust-store-password: wGG1x2u31jcDop1M
trust-store-type: JKS
trust-store-provider: SUN
2
3
4
5
6
重启后,用浏览器访问是会失败的,MACOS及Windows需要先安装client.p12。
在用Chrome浏览器打开后,就会弹出选择证书的窗口。
选择我们刚刚生成的client证书,就可以正常访问了。
Firefox和Safari浏览时,也会弹出选择证书的对话框,选择client证书,即可正常访问。
# Linux导入客户端证书
可以在浏览器中安装,Chrome是在Settings->Privacy and Security->Security->Manage certificates->Your certificates,点击Import按钮导入client.p12,输入密码导入即可。
# curl发起双向认证请求
- 生成client.key
openssl pkcs12 -nocerts -in client.p12 -out client.key
- 生成client.pem
openssl pkcs12 -clcerts -nokeys -in client.p12 -out client.pem
- 发起请求
curl --cert client.pem --key client.key -k -v "https://192.168.0.3/testGet?p=testp"
* Trying 192.168.0.3...
* TCP_NODELAY set
* Connected to 192.168.0.3 (192.168.0.3) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
Enter PEM pass phrase:
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Request CERT (13):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Certificate (11):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS handshake, CERT verify (15):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: C=CN; ST=GUANGDONG; L=ZHUHAI; O=YIIM; OU=YIIM.NET; CN=192.168.0.3
* start date: Apr 7 16:07:00 2023 GMT
* expire date: Apr 4 16:07:00 2033 GMT
* issuer: C=CN; ST=GUANGDONG; L=ZHUHAI; O=YIIM; OU=YIIM.NET; CN=YIIM Global Root CA; emailAddress=ikantech@163.com
* SSL certificate verify ok.
> GET /testGet?p=testp HTTP/1.1
> Host: 192.168.0.3
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Sat, 08 Apr 2023 06:14:42 GMT
<
* Connection #0 to host 192.168.0.3 left intact
{"code":0,"message":"success","param1":"testp"}
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