tomcat系列-05-HTTPS支持-私有CA頒發(fā)證書

[TOC]

前言

本篇將介紹在自己創(chuàng)建的私有CA下,tomcat啟用SSL/TLS支持。

私鑰CA除了在內(nèi)網(wǎng)中使用,我還真不知道有什么其他用處………………

至于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支持,至少有兩種方式(以下兩種叫法并不是專業(yè)術語):

  • 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內(nèi)置的命令keytool來操作
  • PKCS是互聯(lián)網(wǎng)通用的格式,可以用openssl或微軟的Key-Manager來操作

下文就這兩種(keytool和openssl)方式來實現(xiàn)tomcat對HTTPS的支持

1 基于keytool實現(xiàn)

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實現(xiàn)

2.1 將自己的機器配置為私有CA

這部分參加本人另一篇文章: http://blog.csdn.net/hylexus/article/details/53058135#4-openssl實現(xiàn)私有ca

2.2 生成CSR

此處本人在tomcat安裝目錄下新建ssl目錄,在ssl目錄中操作

# 生成私鑰tomcat.key
[root@hylexus ssl]# (umask 077;openssl genrsa -out tomcat.key 2048)
# .....


# 生成證書頒發(fā)請求tomcat.csr
[root@hylexus 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 (2 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 []:hylexus@163.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

2.3 私有CA頒發(fā)證書

此處由于CA是我們自己的私鑰CA,和應用程序在同一臺主機上。所以直接簽署即可:

openssl ca -in tomcat.csr -out tomcat.crt -days 365

至此,看看我們的ssl目錄:

[root@hylexus ssl]# tree
.
├── tomcat.crt # 2.3步驟中生成的證書
├── tomcat.csr # 2.2步驟中生成的證書頒發(fā)請求
└── 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 2048 -keystore ks
    1. 將私鑰CA自己的證書導入到keystore(ks)中
keytool -importcert \
    # 這個文件是私鑰CA自己的證書并不是私鑰CA頒發(fā)給tomcat的證書
    -file /etc/pki/CA/cacert.pem \
    -alias my_ca \ # 起名
    -keystore ./ks \ # 導入到${catalina.home}/ssl/ks文件中
    -trustcacerts 
    1. 將私鑰CA頒發(fā)給tomcat的證書導入同一個keystore(ks)
keytool -importcert \
    -file tomcat.crt \ # 這個是私鑰CA頒發(fā)給tomcat的證書
    -alias tomcat_crt \
    -keystore ./ks \
    -trustcacerts
    1. 配置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

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容