SpringBoot使用ssl


自签CA与证书生成可参考《TLS证书与自签ca》

在上述操作中,仿造serverca的证书签发,再签发一个clientca

于是得到

文件 用途
ws-client.p12 根ca
ws-serverca.p12 服务端证书
ws-trust.p12 服务端可信证书列表

SpringBoot使用ssl

写一个http接口

1
2
3
4
5
6
7
8
9
@RestController
public class HttpsTestController {

@GetMapping("/ssl")
public String helloHttps() {

return "https ws-ssl";
}
}

http单向认证

将上述 ws-serverca.p12 文件放置 resources/ssl/certificate/ 下

配置文件如下:

1
2
3
4
5
6
7
8
9
server:
port: 8443
ssl:
enabled: true
key-store: classpath:ssl/certificate/ws-serverca.p12
key-store-type: PKCS12
key-store-password: ws-servercaKeyPwd
key-password: ws-servercaKeyPwd
key-alias: ws-serverca

测试结果:浏览器警告并显示不安全连接

单项-测试

http双向认证

将上述 ws-trust.p12 文件放置 resources/ssl/trust/ 下

配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
server:
port: 8443
ssl:
enabled: true
key-store: classpath:ssl/certificate/ws-serverca.p12
key-store-type: PKCS12
key-store-password: ws-servercaKeyPwd
key-password: ws-servercaKeyPwd
key-alias: ws-serverca
trust-store: classpath:ssl/trust/ws-trust.p12
trust-store-type: PKCS12
trust-store-password: ws-trustKeystorePwd
client-auth: need # 双向认证

访问接口进行测试,会无法访问

双向测试1

此时给浏览器导入客户端p12证书,重启浏览器后即可访问成功!

导入证书