Springboot項目綁定域名,使用Nginx配置Https


一、https 簡介
     HTTPS(全稱:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全為目標的HTTP通道,簡單講是HTTP的安全版。即HTTP下加入SSL層,HTTPS的安全基礎是SSL,因此加密的詳細內容就需要SSL。 它是一個URI scheme(抽象標識符體系),句法類同http:體系。用於安全的HTTP數據傳輸。https:URL表明它使用了HTTP,但HTTPS存在不同於HTTP的默認端口及一個加密/身份驗證層(在HTTP與TCP之間)。這個系統的最初研發由網景公司(Netscape)進行,並內置於其瀏覽器Netscape Navigator中,提供了身份驗證與加密通訊方法。現在它被廣泛用於萬維網上安全敏感的通訊,例如交易支付方面。  
二、springcloud添加 https

    直接配置(無Nginx):
  1. 首先需要SSL證書,可以自己生成(瀏覽器不認可),也可購買。
  2. 自己生成方法(不推薦):https://www.cnblogs.com/zhangzb/p/5200418.html
  3. 免費一年證書(推薦):https://buy.cloud.tencent.com/ssl?fromSource=ssl
  4. 下載完成后解壓目錄如下:
  springcloud(springboot)一般默認嵌入tomcat中間件(服務器),如果有其他中間件選擇對應文件夾下就好。
  

    tomcat打開如下:.jks證書文件, keystorePass.txt 為證書秘鑰

    

    5. 將證書文件放入要添加https服務的resources下,一般為web層服務,然后打開springcloud(springboot)配置文件application/bootstarp.yml(properties)

   

      server.port  注冊服務端口也是項目訪問端口  

  server.ssl.key-store 證書路徑  

  server.ssl.key-store-password 秘鑰(.txt中內容) 

  http.port 做轉發的端口,如果不做端口轉發可以不用配置,名字可以隨便起 ,端口號也可自定義(不要沖突就好)。接下來直接啟動服務即可,然后即可用https://+項目路徑,進行訪問。
     6. https配置類編寫(不做端口轉發可以不寫)
注意:如果項目配置文件中同時有management.port,需要將其注釋掉,不然無法啟動服務。

 1 /**
 2 * @Title: HttpsPort.java
 3 * @Description: 添加https
 4 * @author zxj
 5 * @version V1.0
 6 */
 7 package com.ewp.data.config;
 8  
 9 import org.apache.catalina.Context;
10 import org.apache.catalina.connector.Connector;
11 import org.apache.tomcat.util.descriptor.web.SecurityCollection;
12 import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
13 import org.springframework.beans.factory.annotation.Value;
14 import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
15 import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
16 import org.springframework.context.annotation.Bean;
17 import org.springframework.stereotype.Component;
18  
19 /**
20  * @ClassName: HttpsPort
21  * @Description: 添加https
22  * @date 2018年3月14日
23  *
24  */
25 @Configuration 
26 public class HttpsPort {
27     
28     @Value("${server.port}")
29     private  int sPort;
30     
31     @Value("${http.port}")
32     private  int hPort;
33     
34     private static class Tomcat extends TomcatEmbeddedServletContainerFactory{// 靜態內部類
35         @Override
36         protected void postProcessContext(Context context) {
37             SecurityConstraint constraint = new SecurityConstraint();
38             constraint.setUserConstraint("CONFIDENTIAL");
39             SecurityCollection collection = new SecurityCollection();
40             collection.addPattern("/*");
41             constraint.addCollection(collection);
42             context.addConstraint(constraint);
43         }
44     }
45     
46     @Bean
47     public EmbeddedServletContainerFactory servletContainer() {// 創建新的tomcat示例,指向定義的http連接
48         Tomcat tomcat = new Tomcat();
49         tomcat.addAdditionalTomcatConnectors(httpConnector());
50         return tomcat;
51     }
52     
53     @Bean
54     public Connector httpConnector() {
55         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
56         connector.setScheme("http");
57         connector.setPort(hPort);       
58         connector.setSecure(false);
59         connector.setRedirectPort(sPort);
60         return connector;
61     }
62     
63 }

    Nginx配置:

 1.打開linux下Nginx安裝目錄找到配置文件nginx.conf(一般路徑為:/usr/local/nginx/conf)進行如下配置

 1 # HTTPS server    
 2     server {
 3         listen       443 ssl;#網頁瀏覽端口,主要是用於HTTPS服務
 4         server_name  www.asquirrel.cn asquirrel.cn;#頂級域名,二級域名
 5     ssl on;
 6         ssl_certificate 1_asquirrel.cn_bundle.crt;#證書路徑
 7         ssl_certificate_key 2_asquirrel.cn.key;#證書秘鑰路徑
 8         ssl_session_timeout  5m;
 9     fastcgi_param   HTTPS               on;
10     fastcgi_param   HTTP_SCHEME         https;
11         location / {       
12         proxy_pass https://ewp_web_contract; #代理轉發的路徑
13         proxy_redirect default;
14         proxy_set_header Host $host;
15         proxy_set_header X-Real-IP $remote_addr;
16         proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
17         proxy_set_header X-Forwarded-Proto https;
18         error_page 502 = /500.html;       
19         }
20     
21     }

 2.將下載好的證書放在與nginx.conf同級目錄下,可新建文件夾,也可不建。證書用Nignx文件夾下的,不要用其他文件夾下的

 3.監聽80端口,轉發請求

1 server {
2         listen       80;
3         server_name  asquirrel.cn;#訪問的路徑
4     if ($host != 'www.asquirrel.cn') {
5         rewrite ^/(.*)$ https://www.asquirrel.cn/$1 permanent;#轉發的路徑
6     }              
7         
8     }

   4.項目web模塊需做如下配置(主要作用於項目內部進行https交互)

 第一:添加tomcat文件夾下的證書

 第二:配置application.properties

 

  5.重啟項目web層服務和Nignx,訪問域名就可以看到瀏覽器顯示安全標志


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM