<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] 參考: http://blog.csdn.net/lanwenbing/article/details/24741973 https://www.cnblogs.com/xxt19970908/p/6736370.html http://blog.csdn.net/zhuchunyan_aijia/article/details/53261305 **1.什么是HTTPS?** > HTTPS其實是有兩部分組成:HTTP + SSL / TLS, 也就是在HTTP上又加了一層處理加密信息的模塊,并且會進行身份的驗證。 > 問題: > Firebug和postman之類的瀏覽器調試工具,為什么獲取到的是明文? > 解答: > SSL是對傳輸的數據進行加密,針對的是傳輸過程的安全。 > firebug之類的瀏覽器調試工具, > 因為他們得到的是客戶端加密之前/解密之后的數據,因此是明文的。 **2.什么是自簽名證書?** > 就是自己生成的證書,并不是官方生成的證書。 > 除非是很正式的項目,否則使用自己簽發的證書即可,因為官方生成證書是要花錢的。 > **3. http與https對比** > 1. 不使用SSL/TLS的HTTP通信,就是不加密的通信。所有信息明文傳播,帶來了三大風險。 > (1) 竊聽風險(eavesdropping):第三方可以獲知通信內容。 > (2) 篡改風險(tampering):第三方可以修改通信內容。 > (3) 冒充風險(pretending):第三方可以冒充他人身份參與通信。 > 2. SSL/TLS協議是為了解決這三大風險而設計的,希望達到: (1) 所有信息都是加密傳播,第三方無法竊 (2) 具有校驗機制,一旦被篡改,通信雙方會立刻發現 (3) 配備身份證書,防止身份被冒充。 互聯網是開放環境,通信雙方都是未知身份,這為協議的設計帶來了很大的難度。而且,協議還必須能夠經受所有匪夷所思的攻擊,這使得SSL/TLS協議變得異常復雜。 ## 一、 使用JDK自帶工具KeyTool 生成自簽發證書! Keytool是一個Java數據證書的管理工具。Keytool將密鑰(key)和證書(certificates)存在一個稱為keystore的文件中在keystore里,包含兩種數據: 1. 密鑰實體(Key entity)——密鑰(secret key)又或者是私鑰和配對公鑰(采用非對稱加密) 2. 可信任的證書實體(trusted certificate entries)——只包含公鑰 Alias(別名):每個keystore都關聯這一個獨一無二的alias,這個alias通常不區分大小寫 第一步:為服務器生成證書(使用keytool命令生成證書) ~~~ keytool -genkey -alias tomcat -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore ./tomcat.keystore -storepass 123456 -genkey -alias tomcat(別名) -keypass 123456(別名密碼) -keyalg RSA(算法) -keysize 1024(密鑰長度) -validity 365(有效期,天單位) -keystore D:/keys/tomcat.keystore(指定生成證書的位置和證書名稱) -storepass 123456(獲取keystore信息的密碼) ~~~ 輸入命令后提示輸入: ~~~ What is your first and last name? #“您的名字與姓氏是什么?”這是必填項,并且必須是TOMCAT部署主機的域名或者IP[如:pvbutler.blog.51cto.com 或者 10.15.24.254],就是你將來要在瀏覽器中輸入的訪問地址 [Unknown]: 10.15.24.254 What is the name of your organizational unit? #“你的組織單位名稱是什么?”可以按照需要填寫也可以不填寫直接回車,實驗中直接回車 [Unknown]: What is the name of your organization? #“您的組織名稱是什么?”,同上直接回車 [Unknown]: What is the name of your City or Locality? #“您所在城市或區域名稱是什么?,同上直接回車 [Unknown]: What is the name of your State or Province? #“您所在的州或者省份名稱是什么?” [Unknown]: What is the two-letter country code for this unit? #“該單位的兩字母國家代碼是什么?” [Unknown]: Is CN=10.15.24.254, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct? #系統詢問“正確嗎?”時,對照輸入信息,如果符合要求則使用鍵盤輸入字母“y”,否則輸入“n”重新填寫上面的信息 [no]: y ~~~ 得到tomcat.keystore文件 ## 二、 springBoot http https 獲取一個tomcat.keystore的文件,將這個文件放到項目的目錄中 ![](https://box.kancloud.cn/e5a655017bab95fa95cd44ba7efba8dc_450x140.png) 配置SSL 編輯application.properties這個文件 ~~~ server.port=8443 server.ssl.key-store=tomcat.keystore server.ssl.key-password=changeit server.ssl.key-store-type=JKS server.ssl.key-alias=tomca ~~~ 我這里用的是默認密碼:changeit http轉向https 1) 這里需要配置使用TomcatEmbeddedServletContainerFactory這個類在啟動方法類中加入以下: 2)注意端口的修改 ~~~ package com.example; import org.apache.catalina.Context; import org.apache.catalina.connector.Connector; import org.apache.tomcat.util.descriptor.web.SecurityCollection; import org.apache.tomcat.util.descriptor.web.SecurityConstraint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; @SpringBootApplication public class SpringBootHttpsApplication { public static void main(String[] args) { SpringApplication.run(SpringBootHttpsApplication.class, args); } @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { SecurityConstraint constraint = new SecurityConstraint(); constraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); constraint.addCollection(collection); context.addConstraint(constraint); } }; tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; } @Bean public Connector httpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); //Connector監聽的http的端口號 connector.setPort(8080); connector.setSecure(false); //監聽到http的端口號后轉向到的https的端口號 connector.setRedirectPort(8443); return connector; } ~~~ 測試使用 1、查看啟動信息 ![image](https://box.kancloud.cn/76aacf533db104f5b9470e0bb4f15be2_800x296.png) 2、訪問地址localhost:8080/AmazeUI-2.7.2/login.html 我自定義了一個html網頁,它已經轉向到了8443端口 ![image](https://box.kancloud.cn/1b1ebe14e63b355e87fbfd2ed486b171_797x397.png) 3、瀏覽器的地址欄中顯示不安全:因為這個證書是不收信任的,傳統一般都企業都是需要購買此證書的 三、 強制tomcat自動http轉https 前提:按照第1步步驟生成證書(.keystore文件),tomcat版本(8.5.15) 1.修改tomcat的server.xml,找到以下配置,取消注釋并按照以下配置修改 ~~~ <!-- 設置https端口:31101 --> <Connector port="31101" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="/home/tuna/tomcat.keystore" <!-- 引用生成的KeyStore文件 --> keystorePass="123456"> <!-- <SSLHostConfig> <Certificate certificateKeystoreFile="conf/localhost-rsa.jks" type="RSA" /> </SSLHostConfig>--> </Connector> ~~~ 另外將 `<Connector port="31001" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/` 將http的31001轉向https的31101端口 `<Connector port="31001" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="31101" URIEncoding="UTF-8"/>` 將 ~~~ <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" / ~~~ 改成(對于來自于集成的http服務器發來的請求,也轉成https) ~~~ <Connector port="8009" protocol="AJP/1.3" redirectPort="31101" / ~~~ 重啟后,用戶也可以去掉端口同時訪問http和https了 2. 為客戶端生成證書(和生成KeyStore一樣) 為瀏覽器生成證書,以便讓服務器來驗證它。為了能將證書順利導入至IE和Firefox,證書格式應該是PKCS12 `keytool -genkey -alias client1 -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -storetype PKCS12 -keystore ./client1.p12 -storepass 12345` 3. 讓服務器信任客戶端證書 1) 由于不能直接將PKCS12格式的證書庫導入,必須先把客戶端證書導出為一個單獨的CER文件,使用如下命令: keytool -export -alias client -keystore ./client.p12 -storetype PKCS12 -keypass 123456 -file ./client.cer 注意:Keypass:指定CER文件的密碼,但會被忽略,而要求重新輸入 2) 將該文件導入到服務器的證書庫(最先生成的.keystore文件),添加為一個信任證書: ~~~ keytool -import -v -file D:/keys/client.cer -keystore D:/keys/tomcat.keystore -storepass 123456 ~~~ 3) 完成之后通過list命令查看服務器的證書庫, 可以看到兩個證書,一個是服務器證書,一個是受信任的客戶端證書: ~~~ keytool -list -v -keystore ./tomcat.keystore ~~~ 4. 讓客戶端信任服務器證書 1) 由于是雙向SSL認證,客戶端也要驗證服務器證書,因此,必須把服務器證書添加到瀏覽器的“受信任的根證書頒發機構”。由于不能直接將keystore格式的證書庫導入,必須先把服務器證書導出為一個單獨的CER文件,使用如下命令: keytool -keystore ./tomcat.keystore -export -alias tomcat -file ./server.cer 2) 雙擊server.cer文件,按照提示安裝證書,將證書填入到“受信任的根證書頒發機構”。 填入方法: 打開瀏覽器 - 【工具】- 【internet選項】-【內容】- 【證書】- 【導入】把server.cer導入到“受信任的根頒發機構”就OK了。 5. http自動轉https 修改web.xml,在該文件</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 ~~~ ## 三、haproxy負載下的https **實現haproxy負載下的http請求轉https主要有兩種方式:** 第一種:把ssl證書設置在haproxy上 > 1、需要編譯haproxy時 加入對ssl的支持 > 2、 支持對tcp四層和http七層負載均衡 > 3、解碼SSL連接并發送非加密連接到后端應用tomcat,這意味著負載均衡器負責解碼SSL連接,這與SSL穿透相反,它是直接向代理服務器發送SSL連接的。 第二種:把ssl證書設置在tomcat上 > 1、haproxy服務器本身只提供代理,后面的web服務器走https(配置ssl證書) > 2、失去增加或修改HTTP報頭的能力,因為連接只是簡單地從負載均衡器路由到tomcat服務器,這意味著應用服務器會失去獲取 X-Forwarded-* 報頭的能力,這個報頭包含了客戶端IP地址、端口和使用的協議。 > 2、需要web端對ssl的支持(配置ssl證書) > 3、haproxy使用tcp模式把連接當作信息流來轉發到其他web服務器 ### 1. 第一種實時步驟(開發環境) 安裝包:鏈接:https://pan.baidu.com/s/1qZyb5uO 密碼:f1cj > 192.168.2.25 :haproxy(負載) > 192.168.2.81:tomcat(web) > 192.168.2.82:tomcat(web) 一、生成證書(192.168.2.25) ~~~ cd /root/tuna mkdir ssl cd ssl openssl genrsa -out ./timing-dev.key 2048 openssl req -new -key ./timing-dev.key -out ./timing-dev.csr openssl x509 -req -days 3655 -in timing-dev.csr -signkey timing-dev.key -out timing-dev.crt cat timing-dev.crt timing-dev.key | tee timing-dev.pem ~~~ 二、編譯haproxy支持https setup.sh腳本中加入了 USE_OPENSSL=1 ADDLIB=-lz對ssl的支持 ~~~ make TARGET=linux2628 ARCH=x86_64 USE_OPENSSL=1 ADDLIB=-lz ~~~ 執行腳本 ~~~ ./setup.sh ~~~ 2. 修改haproxy配置 1) 備份 ~~~ cp /usr/local/haproxy/conf/haproxy.cfg /usr/local/haproxy/conf/haproxy.cfg2018012 ~~~ 2)修改配置 ~~~ vim /usr/local/haproxy/conf/haproxy.cfg ~~~ ~~~ 原https配置: listen timing bind 0.0.0.0:31001 mode tcp balance source server timing1 192.168.2.81:31101 weight 1 maxconn 10000 check inter 10s server timing2 192.168.2.82:31101 weight 1 maxconn 10000 check inter 10s 修改為: frontend timing bind 0.0.0.0:31001 reqirep (.*):31001 \1 # 修改URL,否則會跳轉https://xxxxxxxx:31001 redirect scheme https if !{ ssl_fc } bind 0.0.0.0:443 ssl crt /root/tuna/ssl/timing-dev.pem acl is_timing hdr(host) -i 192.168.2.25 192.168.2.25:443 use_backend timing if is_timing backend timing balance source server timing1 192.168.2.81:31001 weight 1 maxconn 10000 check inter 10s server timing2 192.168.2.82:31001 weight 1 maxconn 10000 check inter 10s ~~~ 3) 重啟haproxy ~~~ service haproxy restart ~~~ ### 2. 第二種方式:步驟如下 一、步驟 1)把ssl證書設置在tomcat處 參考web服務器對ssl的支持 ,完成其中第一項(證書的生成)和第三項( 強制tomcat自動http轉https) 2)修改haproxy配置文件(以25的haproxy為例) 首先、關閉haproxy,然后修改haproxy.cfg文件 ~~~ listen timing bind 0.0.0.0:31001 mode http balance source server timing1 192.168.2.81:31001 weight 1 maxconn 10000 check inter 10s server timing2 192.168.2.82:31001 weight 1 maxconn 10000 check inter 10s 修改為 listen timing bind 0.0.0.0:31001 mode tcp balance source server timing1 192.168.2.81:31101 weight 1 maxconn 10000 check inter 10s server timing2 192.168.2.82:31101 weight 1 maxconn 10000 check inter 10s ~~~ 最后重啟haproxy ## 四、pem轉成KeyStore 1、生成pkcs12格式的密鑰文件: ~~~ openssl pkcs12 -export -in timing-prod.crt -inkey timing-prod.pem -out timing-prod.pk12 -name timing # timing-prod.crt :公鑰 # timing-prod.pem:包含公鑰和私鑰 ~~~ 2、生成keystore: ~~~ keytool -importkeystore -deststorepass '4rfv$RFV' -destkeypass '4rfv$RFV' -destkeystore timing-prod.keystore -srckeystore timing-prod.pk12 -srcstoretype PKCS12 -srcstorepass '4rfv$RFV' -alias timing ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看