https://blog.csdn.net/a_squirrel/article/details/79729690
一、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,需要將其注釋掉,不然無法啟動服務。
/** * @Title: HttpsPort.java * @Description: 添加https * @author zxj * @version V1.0 */ package com.ewp.data.config; 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.beans.factory.annotation.Value; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; /** * @ClassName: HttpsPort * @Description: 添加https * @date 2018年3月14日 * */ @Configuration public class HttpsPort { @Value("${server.port}") private int sPort; @Value("${http.port}") private int hPort; private static class Tomcat extends 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); } } @Bean public EmbeddedServletContainerFactory servletContainer() {// 創建新的tomcat示例,指向定義的http連接 Tomcat tomcat = new Tomcat(); tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; } @Bean public Connector httpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(hPort); connector.setSecure(false); connector.setRedirectPort(sPort); return connector; } }
有Nginx配置:
1.打開linux下Nginx安裝目錄找到配置文件nginx.conf(一般路徑為:/usr/local/nginx/conf)進行如下配置
# HTTPS server
server {
listen 443 ssl;#網頁瀏覽端口,主要是用於HTTPS服務 server_name www.asquirrel.cn asquirrel.cn;#頂級域名,二級域名 ssl on; ssl_certificate 1_asquirrel.cn_bundle.crt;#證書路徑 ssl_certificate_key 2_asquirrel.cn.key;#證書秘鑰路徑 ssl_session_timeout 5m; fastcgi_param HTTPS on; fastcgi_param HTTP_SCHEME https; location / { proxy_pass https://ewp_web_contract; #代理轉發的路徑 proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; error_page 502 = /500.html; } }
2.將下載好的證書放在與nginx.conf同級目錄下,可新建文件夾,也可不建
證書用Nignx文件夾下的,不要用其他文件夾下的
3.監聽80端口,轉發請求
server {
listen 80;
server_name asquirrel.cn;#訪問的路徑
if ($host != 'www.asquirrel.cn') {
rewrite ^/(.*)$ https://www.asquirrel.cn/$1 permanent;#轉發的路徑
}
}
4.項目web模塊需做如下配置(主要作用於項目內部進行https交互)
第一:添加tomcat文件夾下的證書
第二:配置application.properties
五:重啟項目web層服務和Nignx,訪問域名就可以看到瀏覽器顯示安全標志
GitHub:https://github.com/acutesquirrel/spring-cloud/tree/master/add-https