http轉https項目


公司給客戶做的系統突然不能訪問了,查詢系統一切正常,只是外網不能訪問。聯系客戶IT,排查發現系統請求被防火牆攔截掉了,原因:

 

 

 

 

 這樣下去是不行的。

分析了一下,應該是項目沒開啟https的原因。

那么需要把現在的http方式訪問的項目改為https訪問。

目標確定了,就開始動手吧。

 

涉及技術:

1、spring-boot;

2、node;

3、nginx;

4、openssl;

平台:

windows7

 

步驟:

一、后台http轉https:

1、SSLConfig.java中加入以下代碼:

 1     @Bean
 2     public TomcatServletWebServerFactory servletContainer()
 3     {
 4 
 5         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory()
 6         {
 7 
 8             @Override
 9             protected void postProcessContext(Context context)
10             {
11 
12                 SecurityConstraint securityConstraint = new SecurityConstraint();
13                 securityConstraint.setUserConstraint("CONFIDENTIAL");
14                 SecurityCollection collection = new SecurityCollection();
15                 collection.addPattern("/*");
16                 securityConstraint.addCollection(collection);
17                 context.addConstraint(securityConstraint);
18             }
19         };
20         tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
21         return tomcat;
22     }
23 
24     private Connector initiateHttpConnector()
25     {
26         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
27         connector.setScheme("http");
28         connector.setPort(38080);
29         connector.setSecure(false);
30         connector.setRedirectPort(8443);
31         return connector;
32     }

2、application.properties中開啟ssl驗證

1 server.ssl.ciphers=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA,SSL_RSA_WITH_RC4_128_SHA,TLS_RSA_WITH_3DES_EDE_CBC_SHA,TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
2 server.ssl.client-auth=want
3 server.ssl.enabled=true
4 server.ssl.key-alias=spinfosec
5 server.ssl.key-store= classpath:keystore.p12
6 server.ssl.key-store-password=%密碼
7 server.ssl.key-store-type=PKCS12
8 server.ssl.protocol=TLS

3、瀏覽器訪問swagger,可以正常訪問。配置完成。

二、前端服務器http轉https:

1、node服務端創建cert文件夾,用於存放證書相關文件:

2、使用openssl工具創建相關文件:

openssl下載地址:https://slproweb.com/products/Win32OpenSSL.html

命令行生成證書:

#生成私鑰key文件:
$ openssl genrsa -out privatekey.pem 1024

#通過私鑰生成CSR證書簽名
$ openssl req -new -key privatekey.pem -out certsign.csr

# 通過私鑰和證書簽名生成證書文件
$ openssl x509 -req -in certsign.csr -signkey privatekey.pem -out certificate.crt

3、生成的證書文件:

 4、app.js中引用證書文件(以下為代碼片段):

 1 var https = require('https');
 2 
 3 //證書配置
 4 var options = {
 5     key: fs.readFileSync('./cert/privatekey.pem', 'utf8'), 
 6     cert: fs.readFileSync('./cert/certificate.crt', 'utf8')
 7 };
 8 var httpsServer = https.createServer(options, app);
 9 
10 //啟動
11 var SSLPORT = 8180;
12 httpsServer.listen(SSLPORT, function() {
13     console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);
14 });

5、瀏覽器訪問項目,可以正常訪問。配置完成。

 

三、使用https域名訪問項目:

前提配置:

1、支持開啟https的域名(需要備案);

2、能訪問百度雲服務的windows電腦一台。

步驟:

1、打開百度雲SSL服務:https://cloud.baidu.com/product/ssl.html

2、點擊購買,選擇免費dv型證書,證書有效期一年;

 3、點擊證書申請,選擇文件驗證;

4、在域名對應服務器上新建fileauth.txt,放入指定的字符串,等待百度雲服務器自動掃描,成功后會收到郵件;

 5、認證成功后下載證書;

 6、因為使用的是nginx反向代理,所以選擇PEM_Nginx下載;

 

 7、下載后的證書放在nginx目錄下;

 

 8、nginx.conf

 1     server {
 2         listen       8843;
 3         server_name  memory.mynatapp.cc;
 4 
 5         ssl_certificate      memory.mxxxxxp.cc.crt;
 6         ssl_certificate_key  memory.mxxxxxp.cc.key;
 7 
 8         ssl_session_cache    shared:SSL:1m;
 9         ssl_session_timeout  5m;
10 
11         ssl_ciphers  HIGH:!aNULL:!MD5;
12         ssl_prefer_server_ciphers  on;
13 
14       location / {#wechat
15         proxy_pass https://localhost:8181/;
16       }
17       location /web/ {#web
18        proxy_pass https://localhost:8180/;
19       }
20       location /api/ {#api
21        proxy_pass https://localhost:28443/;
22           proxy_http_version 1.1;
23           proxy_set_header Upgrade $http_upgrade;
24           proxy_set_header Connection "Upgrade";
25       }
26   }    

9、重啟nginx,打開瀏覽器查看效果;

 10、登錄系統,api請求正常。配置完成。

 

注:

1、項目開發期間使用跨域的接口調試方式,在正式上線之后需要關閉,確保瀏覽器的同源策略以保證安全性。

2、最近對安全方面知識很感興趣,有沒有推薦的web安全相關書籍?

 

注2:此處可能是我使用的natapp域名主動附帶的https,並不一定是我的配置正確並且生效使能夠訪問https

 

 


免責聲明!

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



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