双向验证(Two Way SSL)不仅客户端需要验证服务端,服务端同样戒备心很重,也需要验证客户端是否是合法。
验证合法性通常是通过Trust Store,要求要把对方的cert装在自己的Trust Store里。
Trust Store:密钥文件可以存放私钥和公钥,具体就是可以存放自己的私钥、自己的公钥和别人的公钥(也可以存放别人的私钥,但这不合理,私钥必须私有)。一般地我们把自己的(私钥和公钥)存放在Key Store里,而把别人的公钥存放在Trust Store里。
#生成密钥文件jks
keytool -genkeypair -alias serverkey -keyalg RSA -dname "CN=Server,OU=Ziguo,O=Ziguo,L=Ziguo,S=NY,C=CN" -keypass bigkeystorepass -keystore server.jks -storepass bigkeystorepass -validity 3650
keytool -genkeypair -alias clientkey -keyalg RSA -dname "CN=Client,OU=Ziguo,O=Ziguo,L=Ziguo,S=NY,C=CN" -keypass bigkeystorepass -keystore client.jks -storepass bigkeystorepass -validity 3650
#导出cert文件
keytool -exportcert -alias clientkey -file client-public.cer -keystore client.jks -storepass bigkeystorepass
keytool -exportcert -alias serverkey -file server-public.cer -keystore server.jks -storepass bigkeystorepass
#把客户端的cert导入到服务端
keytool -importcert -keystore server.jks -alias clientcert -file client-public.cer -storepass bigkeystorepass -noprompt
#把服务端的cert导入到客户端
keytool -importcert -keystore client.jks -alias servercert -file server-public.cer -storepass bigkeystorepass -noprompt
#转换JKS格式为P12
keytool -importkeystore -srckeystore server.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore server.p12 -srcstorepass bigkeystorepass -deststorepass bigkeystorepass
# 检验服务端是否具有自己的private key和客户端的cert
keytool -list -v -keystore server.jks
最近配置spring boot ssl双向认证,日志不断报出 the trustAnchors parameter must be non-empty
再次配置springCloud双向验证遇到问题:
- CN参数配置访问域名,否则java.security.cert.CertificateException: No name matching localhost found executing,可以通过重写httpsconfig 不用验证域名。
- 重写FeignHttpsConfig遇到问题,涉及到https.protocols配置一致性,ENABLED_CIPHER_SUITES 列表等等。
- keytool导出证书,各种格式蒙圈。
- 以前配置Apache CXF双向认证,笔记做的太简单,很多细节无法回忆。
参考文章:
https://github.com/viniciusccarvalho/boot-two-way-ssl-example