keytool--生成證書與Tomcat SSL配置
阿新 • • 發佈:2019-02-15
keytool -genkey -alias tomcat -keyalg RSA -keystore d:/mykeystore -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN" -keypass changeit -storepass -validity 36500
引數說明:
-genkey表示要建立一個新的金鑰
-dname表示金鑰的Distinguished Names,
CN=commonName
OU=organizationUnit
O=organizationName
L=localityName
S=stateName
C=country
Distinguished Names表明了金鑰的發行者身份
-keyalg使用加密的演算法,這裡是RSA
-alias金鑰的別名
-keypass私有金鑰的密碼,這裡設定為changeit
-keystore 金鑰儲存在D:盤目錄下的mykeystore檔案中
-storepass 存取密碼,這裡設定為changeit,這個密碼提供系統從mykeystore檔案中將資訊取出
-validity該金鑰的有效期為 36500表示100年 (預設為90天)
cacerts證書檔案(The cacerts Certificates File)
改證書檔案存在於java.home/lib/security目錄下,是Java系統的CA證書倉庫
二、準備工作
1. 驗證是否已建立過同名的證書
Window : keytool -list -v -alias tomcat -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit
Linux : keytool -list -v -alias tomcat -keystore "$JAVA_HOME/jre/lib/security/cacerts" -storepass changeit
2. 刪除已建立的證書
Window : keytool -delete -alias tomcat -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit Linux : keytool -delete -alias tomcat -keystore "$JAVA_HOME/jre/lib/security/cacerts" -storepass changeit
三、建立證書
1. 伺服器中生成證書:
(注:生成證書時,CN要和伺服器的域名相同,如果在本地測試,則使用localhost)Window : keytool -genkey -alias tomcat -keyalg RSA -keystore d:/my.keystore -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN" -keypass changeit -storepass changeit
Linux : keytool -genkey -alias tomcat -keyalg RSA -keystore ~/my.keystore -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN" -keypass changeit -storepass changeit
2. 匯出證書,由客戶端安裝:
window : keytool -export -alias tomcat -keystore d:/my.keystore -file d:/mycerts.cer -storepass changeit
Linux : keytool -export -alias tomcat -keystore ~/my.keystore -file ~/mycerts.cer -storepass changeit
3. 客戶端配置:為客戶端的JVM匯入金鑰(將伺服器下發的證書匯入到JVM中)
window : keytool -import -trustcacerts -alias tomcat -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -file d:/mycerts.cer -storepass changeit
Linux : keytool -import -trustcacerts -alias tomcat -keystore "$JAVA_HOME/jre/lib/security/cacerts" -file ~/mycerts.cer -storepass changeit
四、配置Tomcat SSL
修改server.xml中的SSL服務Window :
<Connector port="8443" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="d:/my.keystore" keystorePass="changeit"/>
Linux:
<Connector port="8443" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="~/my.keystore" keystorePass="changeit"/>
五、常見問題
1. 未找到可信任的證書
主要原因為在客戶端未將伺服器下發的證書匯入到JVM中,可以用以下命令來檢視證書是否真的匯入到JVM中。keytool -list -alias tomcat -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit
2. keytool錯誤:java.io.IOException:keystore was tampered with,or password was incorrect
原因是在你的home目錄下是否還有.keystore存在。如果存在那麼把他刪除掉,後再執行或者刪除"%JAVA_HOME%/jre/lib/security/cacerts 再執行
建議直接刪掉cacerts再匯入
Tomcat配置https及訪問http自動跳轉至https
完成上述操作就可以通過https://www.xxx.com:8443 或者 http://www.xxx.com:[port]訪問網站;
第二步:配置Tomcat
開啟$CATALINA_HOME/conf/server.xml,修改如下:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
修改成
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" />
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"/>
修改成
<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="/etc/tomcat.keystore" keystorePass="changeit"/>
<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="8443" />
修改成
<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="443" />
第三步:配置tomcat的web.xml在該檔案末尾增加:強制https訪問
及輸入http:// 自動跳轉https://
配置如下:
<login-config>
<!-- Authorization setting for SSL -->
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<!-- Authorization setting for SSL -->
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>