1. 程式人生 > >http協議改為https協議(免費和證書和收費證書)

http協議改為https協議(免費和證書和收費證書)

1、免費證書

一、CMD執行以下程式碼:

%JAVA_HOME%\bin\keytool -genkey -alias server -keyalg RSA -keystore e:\server.keystore-validity 3650

-alias是指定一個別名,類似於Map的key 
-keyalg是指定演算法,預設是DSA 
-keystore是生成的檔案往哪裡放 
-validity是證書有效期,3650就是10年 

或(C:\"ProgramFiles"\Java\jdk1.6.0_43\bin\keytool -genkey -alias server -keyalgRSA -keystore e:\

server.keystore -validity3650)(保證有e盤,如果沒有改其他盤)

根據提示執行操作

二、設定的是E盤 執行完成會在E盤生成server.keystore

把server.keystore放在tomcat的根目錄下

開啟D:\apache-tomcat-6.0.372\conf\server.xml

新增

<Connectorport="443"  protocol="org.apache.coyote.http11.Http11NioProtocol" 

minSpareThreads="5"maxSpareThreads="75"  enableLookups="true"disableUploadTimeout="true" 

acceptCount="100" maxThreads="200"  scheme="https"  secure="true"  SSLEnabled="true"   

clientAuth="false"sslProtocol="TLS" 

keystoreFile="D:\apache-tomcat-6.0.372\server.keystore"   keystorePass="123456"/>

(注意紅色部分)

三、修改專案中的web.xml(實現http協議跳轉https協議)

增加

<security-constraint>   

       <web-resource-collection>    

              <web-resource-name>HtmlAdaptor</web-resource-name>   

              <description>An examplesecurity config that only allows users with the

              role JBossAdmin to access the HTMLJMX console web application</description>    

              <url-pattern>/</url-pattern>

              <!--<http-method>GET</http-method><http-method>POST</http-method> -->  

       </web-resource-collection>   

       <!--<auth-constraint><role-name>JBossAdmin</role-name> </auth-constraint>-->   

       <user-data-constraint>    

              <description>Protectionshould be CONFIDENTIAL</description>    

          <transport-guarantee>CONFIDENTIAL</transport-guarantee>  

       </user-data-constraint>  

</security-constraint>

server.xml配置,這步目的是讓非ssl的connector跳轉到ssl的connector去,redirectPort改成ssl的connector的埠443,重啟後便會生效。

<Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="443" />

2、付費證書

  通常我們使用付費的第三方證書,從認證網站上下載的證書中並不會包括TOMCAT可以使用的jks證書檔案,一般來說,我們拿到的證書檔案如下:(temp是檔名同樣也是我們以下示例中的證書名)

1. temp.crt(自己網站的證書檔案)

2. temp.key (私鑰,可以自己通過crt生成)

3. root.crt(證書認證機構自己的根證書)

2.1、可以通過資料夾內證書檔案和私鑰檔案通過線上轉換生成jks格式證書

2.2、通過openssl命令列轉換

第一步:

合併證書,生成temp.p12檔案

命令列輸入:

openssl pkcs12 -export -in temp.crt -inkey temp.key -out temp.p12 -name temp

此時會提示你建立自己的金鑰和重複輸入,這個密碼請記住,下面我們還會使用

第二步:

生成 keystore 檔案(jks) 需要輸入上一步的匯出密碼,及指定新的 keystore 密碼後面幾步的匯入需要用到這個密碼。

<pre name="code" class="plain">keytool -importkeystore -srckeystore temp.p12 -srcstoretype PKCS12 -destkeystore temp.jks

此時會提示你輸入第一步的密碼,並且輸入新的 keystore 密碼

附錄:

TOMCAT配置:

/data/app/test/temp.jks:你證書檔案

123456:你的證書密碼

 <Connectorport="8080" protocol="HTTP/1.1"  
              SSLEnabled="true" maxThreads="25" scheme="https"
              secure="true" clientAuth="true" sslProtocol="TLS"
              
keystoreFile="/data/app/test/temp.jks"keystorePass="123456" />

3、火狐瀏覽器https問題

    火狐伺服器金鑰交換握手資訊中 SSL 收到了一個弱臨時 Diffie-Hellman 金鑰。 (錯誤碼: ssl_error_weak_server_ephemeral_dh_key)解決方法

開啟火狐瀏覽器,下載外掛


重啟訪問:如圖


點選高階

點選新增例外


點選確認安全例外