1.服務(wù)器端單項(xiàng)認(rèn)證
在Tomcat的server.xml文件中,已經(jīng)提供了現(xiàn)成的配置SSL連接器的代碼,只要把<Connector>元素的注釋去掉即可:
<!— ?Define a SSL HTTP/1.1 Connector on port 8443 ?This connector uses the JSSE configuration, when using APR, the ? connector should be using the OpenSSL style configuration ?described in the APR documentation ? --> ?
<Connector port="8443" ?protocol="HTTP/1.1" ??SSLEnabled="true" ? ? maxThreads="150" scheme="https" ?secure="true" ?clientAuth="false" sslProtocol="TLS" ?keystoreFile="F:/server.keystore" ?keystorePass="123456" />?
實(shí)際上,基于SSL的HTTPS使用的默認(rèn)端口是443。但Tomcat在這里將HTTPS端口設(shè)置為8443。<Connector>配置里的一些屬性參數(shù)如下表:
clientAuth如果設(shè)為true(即雙向認(rèn)證)
keystoreFile指定keystore文件的存放位置
keystorePass指定keystore的密碼
2.服務(wù)器和客戶(hù)端雙向認(rèn)證
Server需要:
1)KeyStore: 其中保存服務(wù)端的私鑰
2)Trust KeyStore:其中保存客戶(hù)端的授權(quán)證書(shū)
同樣,Client需要:
1)KeyStore:其中保存客戶(hù)端的私鑰
2)Trust KeyStore:其中保存服務(wù)端的授權(quán)證書(shū)
?
生成key和證書(shū)
1)生成服務(wù)端私鑰,并且導(dǎo)入到服務(wù)端KeyStore文件中
keytool -genkey -alias serverkey -keystore serverKey.keystore
2)根據(jù)私鑰,導(dǎo)出服務(wù)端證書(shū)
keytool -export -alias serverkey -keystore serverKey.keystore -file server.crt
server.crt就是服務(wù)端的證書(shū)
3)將服務(wù)端證書(shū),導(dǎo)入到客戶(hù)端的Trust KeyStore中
keytool -import -alias serverkey -file server.crt -keystore serverCrt.keystore
tclient.keystore是給客戶(hù)端用的,其中保存著受信任的證書(shū)
?
采用同樣的方法,生成客戶(hù)端的私鑰,客戶(hù)端的證書(shū),并且導(dǎo)入到服務(wù)端的Trust KeyStore中
1)keytool -genkey -alias clientkey -keystore clientKey.keystore
2)keytool -export -alias clientkey -keystore clientKey.keystore -file client.crt
3)keytool -import -alias clientkey -file client.crt -keystore clientCrt.keystore
?
如此一來(lái),生成的文件分成兩組
服務(wù)端保存:serverKey.keystore clientCrt.keystore
客戶(hù)端保存:clientKey.keystore serverCrt.kyestore
client采用clientKey.keystore中的clientkey私鑰進(jìn)行數(shù)據(jù)加密,發(fā)送給server
server采用clientCrt.keystore中的client.crt證書(shū)(包含了clientkey的公鑰)對(duì)數(shù)據(jù)解密,如果解密成功,證明消息來(lái)自client,進(jìn)行邏輯處理
server采用serverKey.keystore中的serverkey私鑰進(jìn)行數(shù)據(jù)叫米,發(fā)送給client
client采用serverCrt.kyestore中的server.crt證書(shū)(包含了serverkey的公鑰)對(duì)數(shù)據(jù)解密,如果解密成功,證明消息來(lái)自server,進(jìn)行邏輯處理
如果過(guò)程中,解密失敗,那么證明消息來(lái)源錯(cuò)誤。不進(jìn)行邏輯處理。這樣就完成了雙向的身份認(rèn)證。
tomcat配置:
? ? <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"?? maxThreads="150" ?scheme="https" ?secure="true"?? clientAuth="true" ??sslProtocol="TLS"?
? ? ?keystoreFile="f:/serverKey.keystore" keystorePass="123456" keystoreType="JKS"?
? ??truststoreFile="f:/clientCrt.keystore" truststorePass="123456" truststoreType="JKS"
? />
truststoreFile指定truststore(受信任的客戶(hù)端證書(shū)庫(kù))文件的存放位置
truststorePass指定truststore(受信任的客戶(hù)端證書(shū)庫(kù))的密碼
- 對(duì)單個(gè)WEB項(xiàng)目使用默認(rèn)SSL安全訪問(wèn)
要使你自己的WEB程序應(yīng)用SSL安全訪問(wèn),請(qǐng)遵循如下配置
在你應(yīng)用的 web.xml 文件的 <web-app></web-app> 中加入如下配置
<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>
?
你會(huì)發(fā)現(xiàn),即使使用 http://....:8080 來(lái)訪問(wèn)你的應(yīng)用程序,它也會(huì)重定向?yàn)?https://....8443 訪問(wèn),也就是說(shuō),你的應(yīng)用已經(jīng)強(qiáng)制使用SSL安全訪問(wèn)層。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
