Tomcat配置https協議、以及http協議自動REDIRECT到HTTPS

來源:互聯網
上載者:User
在命令提示字元視窗,進入Tomcat目錄,執行以下命令: 
keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass changeit -keystore server.keystore -validity 3600 
通過以上步驟產生server.keystore認證檔案 

將servlet.xml一下的的注釋開啟
<!-- Define a SSL HTTP/1.1 Connector on port 8443 -->   
<Connector protocol="org.apache.coyote.http11.Http11Protocol"     
                     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="server.keystore"     
           keystorePass="changeit"/>  
到這一步訪問https://ip:8443/ 

一般Tomcat預設的SSL連接埠號碼是8443,但是對於SSL標準連接埠號碼是443,這樣在訪問網頁的時候,直接使用https而不需要輸入連接埠號碼就可以訪問,如https://ip/ 
想要修改連接埠號碼,需要修改Tomcat的server.xml檔案: 
1.non-SSL HTTP/1.1 Connector定義的地方,一般如下: 
     <Connector port="80" maxHttpHeaderSize="8192" 
                maxThreads="500" minSpareThreads="25" maxSpareThreads="75" 
                enableLookups="false" redirectPort="443" acceptCount="100" 
                connectionTimeout="20000" disableUploadTimeout="true" /> 
將其中的redirectPort連接埠號碼改為:443 
2.SSL HTTP/1.1 Connector定義的地方,修改連接埠號碼為:443,如下: 
<Connector     
   port="443" maxHttpHeaderSize="8192" 
   maxThreads="150" minSpareThreads="25" 
   maxSpareThreads="75" 
   enableLookups="false" 
   disableUploadTimeout="true" 
   acceptCount="100" scheme="https" 
   secure="true" 
   clientAuth="false" sslProtocol="TLS" 
   keystoreFile="conf/tomcat.keystore" 
   keystorePass="123456" /> 
3.AJP 1.3 Connector定義的地方,修改redirectPort為443,如下: 
     <Connector port="8009" 
                enableLookups="false" redirectPort="443" protocol="AJP/1.3" /> 

重新啟動Tomcat就可以了。到這一步可以形成訪問方式 https://ip/

4、強制https訪問

  在tomcat\conf\web.xml中的</welcome-file-list>後面加上這樣一段:

<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> 

注意:(如果對方使用的機器連接埠被佔用)

需要切換連接埠來轉換資料:iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.