- 前言
- 基于keytool實作
- 1 生成自簽署證書
- 2 配置tomcat
- 3 效果
- 基于openssl實作
- 1 将自己的機器配置為私有CA
- 2 生成CSR
- 3 私有CA頒發證書
- 4 配置tomcat
- 41 基于APR-Connector的配置
- 42 基于NIOBIO-Connector的配置
- 5 效果
- 參考文章
前言
本篇将介紹在自己建立的私有CA下,tomcat啟用SSL/TLS支援。
私鑰CA除了在内網中使用,我還真不知道有什麼其他用處………………
至于SSL/TSL不熟悉的請自行百度或者看本人其他文章:
常見加密類型及通信安全:http://blog.csdn.net/hylexus/article/details/53048305
SSL、openSSL、CA:http://blog.csdn.net/hylexus/article/details/53058135
對于本篇文章來說,或許,我們隻需要知道:TLS(Transport Layer Layer)和他的前生SSL(Secure Socket Layer)是一種浏覽器和伺服器之間安全通信的技術。就夠了吧。
在tomcat中啟用SLL/TLS支援,至少有兩種方式(以下兩種叫法并不是專業術語):
- APR類型的Connector下啟用HTTPS
- BIO/NIO類型的Connector下啟用HTTPS
對于這幾種Connector不熟悉的,可以參考本人另一篇介紹APR的文章:http://blog.csdn.net/hylexus/article/details/53137721
Tomcat currently operates only on JKS, PKCS11 or PKCS12 format keystores. The JKS format is Java’s standard “Java KeyStore” format, and is the format created by the keytool command-line utility. This tool is included in the JDK. The PKCS12 format is an internet standard, and can be manipulated via (among other things) OpenSSL and Microsoft’s Key-Manager.
由以上這段來自tomcat官方文檔的介紹可知:
- tomcat目前支援JKS, PKCS11 或 PKCS12格式的keystore
- JKS是java标準的秘鑰管理格式,通過java内置的指令keytool來操作
- PKCS是網際網路通用的格式,可以用openssl或微軟的Key-Manager來操作
下文就這兩種(keytool和openssl)方式來實作tomcat對HTTPS的支援
1 基于keytool實作
1.1 生成自簽署證書
# 此處本人在目錄D:\java-env\apache-tomcat-7-80\ssl\ks下操作
keytool -genkeypair \
-alias tomcat \
-keyalg rsa \
-keysize 2048 \
-validity 365 \
-keystore keystore
# 此處将keystore的位置指定為:D:\java-env\apache-tomcat-7-80\ssl\ks\keystore
1.2 配置tomcat
此處使用NIO類型的Connector
<Connector
protocol="org.apache.coyote.http11.Http11NioProtocol"
port="8443" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
keystoreFile="${catalina.home}/ssl/ks/keystore" keystorePass="123456"
clientAuth="false" sslProtocol="TLS"/>
1.3 效果
2 基于openssl實作
2.1 将自己的機器配置為私有CA
這部分參加本人另一篇文章: http://blog.csdn.net/hylexus/article/details/53058135#4-openssl實作私有ca
2.2 生成CSR
此處本人在tomcat安裝目錄下建立ssl目錄,在ssl目錄中操作
# 生成私鑰tomcat.key
[[email protected] ssl]# (umask 077;openssl genrsa -out tomcat.key 2048)
# .....
# 生成證書頒發請求tomcat.csr
[[email protected] ssl]# openssl req -new -key tomcat.key -out tomcat.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name ( letter code) [CN]:
State or Province Name (full name) [ShangHai]:
Locality Name (eg, city) [ShangHai]:
Organization Name (eg, company) [Default Company Ltd]:KKBC
Organizational Unit Name (eg, section) [dev]:
Common Name (eg, your name or your server's hostname) []:test.com
Email Address []:[email protected]
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
2.3 私有CA頒發證書
此處由于CA是我們自己的私鑰CA,和應用程式在同一台主機上。是以直接簽署即可:
openssl ca -in tomcat.csr -out tomcat.crt -days
至此,看看我們的ssl目錄:
[[email protected] ssl]# tree
.
├── tomcat.crt # 2.3步驟中生成的證書
├── tomcat.csr # 2.2步驟中生成的證書頒發請求
└── tomcat.key # 2.2步驟中生成的應用程式的私鑰
2.4 配置tomcat
2.4.1 基于APR-Connector的配置
前提是要啟用APR,可以參考:http://blog.csdn.net/hylexus/article/details/53137721
<Connector
protocol="org.apache.coyote.http11.Http11AprProtocol"
port="8443" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
SSLCertificateFile="${catalina.base}/ssl/tomcat.crt"
SSLCertificateKeyFile="${catalina.base}/ssl/tomcat.key"
SSLVerifyClient="optional" SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"/>
2.4.2 基于NIO/BIO-Connector的配置
此處還是在tomcat安裝目錄下建立的ssl目錄下操作:
- 1) 生成私鑰和keystore
# 放置于tomcat安裝目錄的ssl目錄下的ks檔案中
keytool -genkeypair -keyalg rsa -keysize -keystore ks
- 2) 将私鑰CA自己的證書導入到keystore(ks)中
keytool -importcert \
# 這個檔案是私鑰CA自己的證書并不是私鑰CA頒發給tomcat的證書
-file /etc/pki/CA/cacert.pem \
-alias my_ca \ # 起名
-keystore ./ks \ # 導入到${catalina.home}/ssl/ks檔案中
-trustcacerts
- 3) 将私鑰CA頒發給tomcat的證書導入同一個keystore(ks)
keytool -importcert \
-file tomcat.crt \ # 這個是私鑰CA頒發給tomcat的證書
-alias tomcat_crt \
-keystore ./ks \
-trustcacerts
- 4) 配置server.xml
NIO-Connector配置
<Connector
protocol="org.apache.coyote.http11.Http11NioProtocol"
port="8443" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
keystoreFile="${catalina.home}/ssl/ks" keystorePass="123456"
clientAuth="false" sslProtocol="TLS"/>
BIO-Connector配置
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
connectionTimeout="20000" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="${catalina.home}/ssl/ks" keystorePass="123456"
/>
2.5 效果
此處注意修改hosts檔案,我們在CSR中寫的域名是test.com
參考文章
http://tomcat.apache.org/tomcat-7.0-doc/config/http.html#Special_Features
http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html