1、生成服务端密钥库(server.keystore)
keytool -genkey -dname "CN=江苏移动12590, OU=江苏移动, O=12590, L=南京, ST=江苏, C=中国" -keyalg RSA -keysize 1024 -alias server -keypass hisun123 -keystore ./server.keystore -storepass hisun123 -validity 3650
关键参数解释:
-dname:证书的身份信息(CN = 域名 / 名称,OU = 部门,O = 公司,L = 城市,ST = 省份,C = 国家)。-keyalg RSA:密钥算法为 RSA(非对称加密)。-keysize 1024:密钥长度(1024 是兼容版,推荐 2048)。-alias server:密钥库中该密钥对的别名(唯一标识)。-keypass hisun123:密钥的密码(保护私钥)。-storepass hisun123:密钥库文件的密码(保护整个密钥库)。-validity 3650:证书有效期(10 年)。
2、生成客户端 PKCS12 格式密钥库(client.p12)
keytool -genkey -v -alias client -keyalg RSA -storetype PKCS12 -keystore ./client.p12 -dname "CN=江苏移动12590, OU=江苏移动, O=12590访客, L=南京, ST=江苏, C=中国"
关键参数:
-storetype PKCS12:密钥库格式为 PKCS12(Java/Windows/Mac 通用)。-v:详细输出(便于排查问题)。- 注意:该命令未指定密码,执行时会交互式提示输入
storepass和keypass(你后续导出证书用了hisun123,建议执行时统一设为hisun123)。
3、导出客户端证书(client.cer)
keytool -export -alias client -keystore ./client.p12 -storetype PKCS12 -storepass hisun123 -rfc -file ./client.cer
关键参数:
-export:导出证书(仅公钥,无私钥)。-rfc:输出 Base64 编码的文本格式(便于复制 / 传输)。-file ./client.cer:导出的证书文件路径。
4、导出服务端证书(server.cer)
注意:该命令未指定 storepass,执行时会提示输入(需输入 hisun123)。
5、将客户端证书导入服务端信任库
# 将客户端证书导入服务端密钥库(作为信任的证书)
keytool -import -alias client -file ./client.cer -keystore ./server.keystore -storepass hisun123 -noprompt
-import:导入证书到密钥库(信任库)。
-noprompt:跳过交互式确认(直接信任该证书)。
6、Tomcat SSL 连接器配置(server.xml)
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
SSLEnabled="true" maxThreads="150" scheme="https"
secure="true" clientAuth="true" sslProtocol="TLS"
keystoreFile="/app/media2/keystore/server.keystore" keystorePass="hisun123"
truststoreFile="/app/media2/keystore/server.keystore" truststorePass="hisun123" />
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
核心作用:配置 Tomcat 的 HTTPS 端口(8443),启用双向 SSL 认证(服务端验证客户端证书,客户端验证服务端证书)。 关键参数解释:
clientAuth="true":强制要求客户端提供证书(双向认证核心);若设为want,则可选客户端证书。keystoreFile/keystorePass:服务端的密钥库(包含服务端私钥,用于解密客户端请求)。truststoreFile/truststorePass:服务端的信任库(需包含客户端证书,用于验证客户端证书合法性)。sslProtocol="TLS":SSL 协议(推荐用sslEnabledProtocols="TLSv1.2,TLSv1.3",禁用低版本)。- 8080 端口配置:HTTP 端口,访问时自动重定向到 8443(HTTPS)。
Categories:
系统运维