一、SSL证书简介
要想使用https, 首先,我们需要有SSL证书,证书可以通过两个渠道获得:
- 1.自己生成
虽然安全性不是那么高,但胜在成本低.
目前证书有以下常用文件格式:JKS(.keystore),微软(.pfx),PEM(.key + .crt)。其中,tomcat使用JKS格式,nginx使用PEM格式.
- 2.公开可信认证机构
例如CA,但是申请一般是收费的。
二、自己生成
JDK自带了一个生成证书 keytool ,目录在 /bin 下面
1、生成证书
输入:(如果你没有配环境变量需要cd到bin目录执行,否则做不到keytool命令)
keytool -genkey -v -alias HaC -keyalg RSA -validity 3650 -keystore ~/cert/HaC.keystore
- alias: 别名 这里起名HaC
- keyalg: 证书算法,RSA
- validity:证书有效时间,10年
- keystore:证书生成的目标路径和文件名,替换成你自己的路径即可,我定义的是~/Lee/HaC.keystore
然后随便输入,但是密码要记得:
[root@VM-8-8-centos ~]# keytool -genkey -v -alias HaC -keyalg RSA -validity 3650 -keystore ~/cert/HaC.keystore
Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]: a
What is the name of your organizational unit?
[Unknown]: b
What is the name of your organization?
[Unknown]: c
What is the name of your City or Locality?
[Unknown]: d
What is the name of your State or Province?
[Unknown]: e
What is the two-letter country code for this unit?
[Unknown]: f
Is CN=a, OU=b, O=c, L=d, ST=e, C=f correct?
[no]: y
Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 3,650 days
for: CN=a, OU=b, O=c, L=d, ST=e, C=f
Enter key password for <HaC>
(RETURN if same as keystore password):
Re-enter new password:
[Storing /root/cert/HaC.keystore]
Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore /root/cert/HaC.keystore -destkeystore /root/cert/HaC.keystore -deststoretype pkcs12".
2、其他操作,可以不用理会
导出证书:
123456是我在上面输入的密码。
keytool -exportcert -rfc -alias HaC -file ~/cert/HaC.cer -keystore ~/cert/HaC.keystore -storepass 123456
[root@VM-8-8-centos apache-tomcat-8.0.53]# keytool -exportcert -rfc -alias HaC -file ~/cert/HaC.cer -keystore ~/cert/HaC.keystore -storepass 123456
Certificate stored in file </root/cert/HaC.cer>
导入到jvm的库
-storepass changeit
表示默认密码是 changeit ,changeit 是jvm的默认密码。不是上面的123456。
keytool -import -v -trustcacerts -alias HaC -file ~/cert/HaC.cer -storepass changeit -keystore /var/www/web/jdk/jdk1.8.0_261/jre/lib/security/cacerts
删除
keytool -delete -alias testkey -keystore /var/www/web/jdk/jdk1.8.0_261/jre/lib/security/cacerts -storepass changeit