【集羣下多個子服務之間的nginx代理https通信】+【自己頒發證書】的情境中,證書生成、安裝配置

本文重點在於證書上,對於nginx和tomcat以及resin怎麼啓用https服務,請自行百度:)。

簡述

      在自己頒發證書的測試環境下,不同域名下(相同的二級域名)的服務之間調用https服務,需要給每一臺機器的jdk都安裝指定域名的證書纔可以發起請求,否則jdk拒絕發送請求,因爲自己頒發的證書是不在jdk的信任列表裏的。但是我們不能給A【a.test.com】和B【b.test.com】分別生成對應全域名的證書再分別安裝到對方的jdk中,因爲如果還有C,D..等等多個服務的話,我們就需要生成a.test.com,b.test.com,c.test.com, d.test.com 等等的域名,再分別把這些證書裝到所有服務器的jdk中,那工作量可就大了。因此,我們生成一個泛域名證書,所有服務都使用這一個證書,相互之間依舊可以溝通。

      爲了完成服務的https功能,需要兩個文件:證書【.crt】文件和對應的KeyStore【.keystore】文件。

              A.    crt安裝到每一臺機器的jdk上,從而使得jdk信任該域名。

              B.    keystore文件引用到每一臺機器中,開啓服務器的https端口。

      做完上面的兩點後,服務A,B,C...等等的服務都在一個二級域名下就可以通信(當然了,因爲是我們自己的服務,所以都是相同的二級域名)。

      步驟綜述就是:

            1. 生成CA的羣文件【key,csr,crt】

            2. 再利用CA的羣文件給應用服務器生成羣文件【key,csr,crt】

            3. 利用上述的一些文件生成tomcat和resin能識別的keystore文件,

            4. 開啓服務器的https服務:給所有服務器安裝keystore文件

            5. 添加證書到信任列表:將證書添加到jdk信任列表

            6. nginx配置:最後,別忘了給nginx指定證書,它與tomcat、resin不同,需要的 【.crt】 和【 .key】 這兩個文件。

實現

1 生成CA證書

1.1 生成 CA 私鑰

                           - openssl genrsa -out ca.key 1024

1.2 生成csr 

                           - openssl req -new -key ca.key -out ca.csr

         linux的反饋示例:

                                    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) [XX]:CN

                                             State or Province Name (full name) []:BeiJing

                                             Locality Name (eg, city) [Default City]:BeiJing 

                                             Organization Name (eg, company) [Default Company Ltd]:My CA

                                             Organizational Unit Name (eg, section) []:

                                             Common Name (eg, your name or your server's hostname) []:test

                                             Email Address []:

                                            

                                             Please enter the following 'extra' attributes

                                             to be sent with your certificate request

                                             A challenge password []:

                                             An optional company name []:

                          

                          

1.3 生成crt

                  - openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt

 

2. 爲服務器端和客戶端【這裏服務器端和客戶端都當作同一個服務器,正如介紹中講的】準備公鑰、私鑰

2.1 生成服務器端私鑰

                           - openssl genrsa -out server.key 1024

2.2 生成服務器端公鑰【這一步的文件在後面沒有直接用到,先記錄下來】

                           - openssl rsa -in server.key -pubout -out server.pem

                          

3.生成服務器端證書和客戶端證書【這裏服務器端和客戶端都當作同一個服務器,正如介紹中講的】

3.1 服務器端需要向 CA 機構申請簽名證書,在申請簽名證書之前依然是創建自己的 CSR 文件

                           - openssl req -new -key server.key -out server.csr

        linux反饋示例:                   

                                    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) [XX]:CN

                                    State or Province Name (full name) []:BeiJing  

                                    Locality Name (eg, city) [Default City]:BeiJing

                                    Organization Name (eg, company) [Default Company Ltd]:My Server

                                    Organizational Unit Name (eg, section) []:

                                    Common Name (eg, your name or your server's hostname) []:*.test.com

                                    Email Address []:

                                   

                                    Please enter the following 'extra' attributes

                                    to be sent with your certificate request

                                    A challenge password []:

                                   

3.2 向自己的 CA 機構申請證書,簽名過程需要 CA 的證書和私鑰參與,最終頒發一個帶有 CA 簽名的證書

                  - openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt

                          

4. 接下來就是讓所有的服務支持https,因爲所有的服務都用的nginx生成的那個crt,所以我們把這個crt配置到每一個server【tomcat 、resin】中:

4.1 tomcat 、resin裏面需要安裝的是keystore文件,所以我們需要先生成這個文件。實現步驟:

I. 首先利用crt和key以及ca的crt生成p12 文件:

                  - openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12 -name tomcat -CAfile ca.crt -caname root -chain

                                             Enter Export Password:

                                             Verifying - Enter Export Password:

                                             [root@nginx-104-230 ssl]# ll

                                             total 36

II. 接下來生成它們需要的keystore:

                  - keytool -importkeystore -v  -srckeystore server.p12 -srcstoretype pkcs12 -srcstorepass 123456 -destkeystore server.keystore -deststoretype jks -deststorepass 123456

                                                      Importing keystore server.p12 to server.keystore...

                                                      Entry for alias tomcat successfully imported.

                                                      Import command completed:  1 entries successfully imported, 0 entries failed or cancelled

                                                      [Storing server.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 server.keystore -destkeystore server.keystore -deststoretype pkcs12".

4.2 將keystore引用到服務器裏面:

tomcat:

                 <Connector

                                   SSLEnabled="true"            clientAuth="false"     keystoreFile="/data/server.keystore"

                                   keystorePass="123456"        maxThreads="200" port="443"                                     

                                   protocol="org.apache.coyote.http11.Http11Protocol"

                                   scheme="https"                   secure="true"               sslProtocol="TLS"

                 />

resin:

                                    jsse_keystore_type : jks

                                    jsse_keystore_file : /data/server.keystore

                                    jsse_keystore_password : 123456

 

5. 還需要給jdk添加信任證書

 將server.crt導入到集羣中各個服務的jdk中:

                  - keytool -importcert -trustcacerts -alias server -file /data/server.crt -keystore cacerts -storepass changeit

6. 給nginx引用key和crt

server {
    listen       443;
    server_name email.test.com; 
    index index.html index.htm;
    ssl on;
        ssl_certificate ssl/server.crt;
        ssl_certificate_key ssl/server.key;
        ssl_session_timeout 5m;
        #ssl_protocols SSLv2 SSLv3 TLSv1;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
location / {
        proxy_next_upstream http_502 http_504 http_503 http_404  error timeout invalid_header;
        proxy_pass https://tdts_email;
}
location ~ .*\.(php|jsp|cgi|do|action|shtml)?$ {
        proxy_next_upstream http_502 http_504 http_503 http_404  error timeout invalid_header;
        proxy_pass https://tdts_email;
}

    access_log  /data/logs/nginx_www/access.log main;
    error_log   /data/logs/nginx_www/error.log  debug;

}

7. 完成

至此就可以在瀏覽器上訪問集羣中的某一個服務了,並且與此同時,各個子服務之間也可以直接java通信

感謝

在研究的時候參考了很多文章,以下文章給予了很大的幫助,謝謝這些大神:

https://www.linuxidc.com/Linux/2016-05/130943.htm
https://www.oschina.net/question/2266279_221175
https://blog.csdn.net/fighterandknight/article/details/53139923
https://blog.csdn.net/u011411069/article/details/79994716

 

tag:有時間和更多的理解後再來完善文章。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章