Tomcat使用Https協議_TOMCAT

來源:互聯網
上載者:User

http://blog.csdn.net/a35038438/article/details/50598429


在配置Https協議之前你需要知道:

1.ssl

2.keystore

3.<security-constraint>認證類型



目錄: 1.產生安全性憑證
2.配置Tomcat

3.測試頁面

4.https與http協議轉換

5.頁面強制使用https協議



1.產生安全性憑證(cmd):

[plain]  view plain  copy 建立認證命令:keytool -genkeypair -alias "tomcat" -keyalg "RSA" -validity 90 -keystore "D:\tomcat.keystore"  

"D:\tomcat.keystore"  產生的keystore位置和名稱

-keyalg "RSA"  密碼編譯演算法

-validity 90 有效天數


PS:值得注意的是"輸入keystore密碼"在你輸入時,並不會顯示你的密碼

2.配置Tomcat(conf檔案夾中的server.xml):

 [html]  view plain  copy 添加此段代碼:<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"                   maxThreads="150" SSLEnabled="true" scheme="https" secure="true"                   clientAuth="false" sslProtocol="TLS"                   keystoreFile="D:/tomcat.keystore"              keystorePass="tomcat" />   
 

 3.此時在瀏覽器地址欄輸入https://localhost:8443進行訪問進入到如下頁面則配置成功: 

4.https與http協議轉換(有些頁面需要使用https訪問,有些頁面使用http訪問)

[html]  view plain  copy 找到此段代碼:<Connector port="8080" protocol="HTTP/1.1"                  connectionTimeout="20000"                  redirectPort="8009" />    [html]  view plain  copy 修改為:      <Connector port="80" protocol="HTTP/1.1"                  connectionTimeout="20000"                  redirectPort="443" />  
[html]  view plain  copy 找到此段代碼:<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"                   maxThreads="150" SSLEnabled="true" scheme="https" secure="true"                   clientAuth="false" sslProtocol="TLS"                   keystoreFile="D:/tomcat.keystore"              keystorePass="tomcat" />  

 [html]  view plain  copy 修改為:     <Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol"                   maxThreads="150" SSLEnabled="true" scheme="https" secure="true"                   clientAuth="false" sslProtocol="TLS"                   keystoreFile="D:/tomcat.keystore"              keystorePass="tomcat" />   
 
 PS:80和443是http和https協議的預設連接埠號碼,這樣設定,是為了在瀏覽器地址欄不需要輸入連接埠號碼就能訪問頁面,需要訪問https協議頁面,就直接在地址欄輸入https://localhost。 

5.頁面強制使用https協議(例如:登入頁面必須使用https協議)

[html]  view plain  copy 在項目資源中的web.xml或tomcat/conf/web.xml檔案中的</welcome-file-list>後加上:   <web-resource-collection >       <web-resource-name >SSL</web-resource-name>       <url-pattern>/*</url-pattern><!-- 這裡表示所有頁面都必須使用https協議 -->   </web-resource-collection>       <user-data-constraint>            <transport-guarantee>CONFIDENTIAL</transport-guarantee>       </user-data-constraint>    </security-constraint>  

 配置完成後,使用http(不輸入連接埠號碼)協議方式進入頁面也會自動轉換為https協議進行訪問。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.