Linux下Tomcat6配置HTTPS單向認證__Linux
最後更新:2018-07-28
來源:互聯網
上載者:User
### tomcat6配置:
- 1.單向認證,就是傳輸的資料加密過了,但是不會校正用戶端的來源
- 2.雙向認證,如果用戶端瀏覽器沒有匯入用戶端認證,是訪問不了web系統的,找不到地址
- 如果只是加密,單向就行;如果想要用系統的人沒有認證就訪問不了系統的話,就採用雙向
- http預設使用的是8080連接埠,如果URL裡面連接埠號碼也懶得輸,就改為80.
- https預設使用的是8443連接埠,如果URL裡面連接埠號碼也懶得輸,就改為443.
### 1.進入到linux目錄下
- cd /data1/tomcat/conf/
### 2.產生認證
- keytool -genkey -alias tomcat -keyalg RSA -validity 36500 -keystore /data1/tomcat/conf/.keystore
- 參數簡要說明:/data1/tomcat/conf/.keystore含義是將認證檔案的儲存路徑,認證檔案名稱是.keystore
- -validity 36500 含義是認證有效期間,36500表示100年,預設值是90天 tomcat 為自訂認證名稱
```
[root@test_server conf]# keytool -genkey -alias tomcat -keyalg RSA -validity 36500 -keystore /data1/tomcat/conf/.keystore
Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]: 180.*.*.*
What is the name of your organizational unit?
[Unknown]: xxx
What is the name of your organization?
[Unknown]: xxx
What is the name of your City or Locality?
[Unknown]: chongqing
What is the name of your State or Province?
[Unknown]: chongqing
What is the two-letter country code for this unit?
[Unknown]: cn
Is CN=180.*.*.*, OU=xxx, O=xxx, L=chongqing, ST=chongqing, C=cn correct?
[no]: y
Enter key password for <tomcat>
(RETURN if same as keystore password):
Re-enter new password:
```
- 在命令列填寫必要參數:
- A、 輸入keystore密碼:此處需要輸入大於6個字元的字串(1q2w3e4r)。
- B、 “您的名字與姓氏是什麼。”這是必填項,並且必須是TOMCAT部署主機的網域名稱或者IP[如:zhao.com 或者 10.1.25.251](就是你將來要在瀏覽器中輸入的訪問地址),否則瀏覽器會彈出警告視窗,提示使用者認證與所在域不匹配。在本地做開發測試時,應填入“localhost”。
- C、 你的組織單位名稱是什麼。”、“您的組織名稱是什麼。”、“您所在城市或地區名稱是什麼。”、“您所在的州或者省份名稱是什麼。”、“該單位的兩字母國家代碼是什麼。”可以按照需要填寫也可以不填寫直接斷行符號,在系統詢問“正確嗎。”時,對照輸入資訊,如果符合要求則使用鍵盤輸入字母“y”,否則輸入“n”重新填寫上面的資訊。
- D、 輸入<tomcat>的主密碼,這項較為重要,會在tomcat設定檔中使用,建議輸入與keystore的密碼一致,設定其它密碼也可以,完成上述輸入後,直接斷行符號則在你在第二步中定義的位置找到產生的檔案。
### 3.配置tomcat/conf/目錄下的server.xml
```
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
maxPostSize="0"
redirectPort="443"
URIEncoding="UTF-8" />
<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol"
SSLEnabled="true" maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="/data1/tomcat/conf/.keystore"
keystorePass="1q2w3e4r"
connectionTimeout="20000"
maxPostSize="0"
redirectPort="443"
URIEncoding="UTF-8" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="443" />
```
- clientAuth="false" 即是單向認證
### 4.應用程式HTTP自動跳轉到HTTPS
- 在應用程式中web.xml中加入以下代碼,即可控制那些路徑走http協議,那些路徑走https協議:
```
<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>
```
### 5.瀏覽器訪問https
- 重啟tomcat
- 訪問:https://180.*.*.*/***