1. 程式人生 > 實用技巧 >SpringBoot配置Https

SpringBoot配置Https

HTTPS (全稱:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全為目標的 HTTP 通道,在HTTP的基礎上通過傳輸加密和身份認證保證了傳輸過程的安全性 [1] 。HTTPS 在HTTP 的基礎下加入SSL 層,HTTPS 的安全基礎是 SSL,因此加密的詳細內容就需要 SSL。 HTTPS 存在不同於 HTTP 的預設埠及一個加密/身份驗證層(在 HTTP與 TCP 之間)。這個系統提供了身份驗證與加密通訊方法。它被廣泛用於全球資訊網上安全敏感的通訊,例如交易支付等方面

生成證書

使用Java JDK自帶生成SSL證書的工具keytool

生成證命令

C:\Users\Administrator>keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keysize 2048 -keystore "D:/tomcat.keystore"

通過此命令執行,則CMD命令視窗會提示輸入金鑰庫口令

-alias 別名
-keypass 指定生成金鑰的密碼
-keyalg 指定金鑰使用的加密演算法(如 RSA)
-keysize 金鑰大小
-validity 過期時間,單位天
-keystore 指定儲存金鑰的金鑰庫的生成路徑、名稱
-storepass 指定訪問金鑰庫的密碼

域名證書,可以通過阿里 或 騰訊雲 來進行申請

參考,騰訊雲域名證書申請流程:https://cloud.tencent.com/document/product/400/6814

專案配置證書

匯入證書,把生成的tomcat.keystore放在resources裡面

application.properties 或 application.yml 配置檔案中配置相關https內容

server.port=8443

# 開啟https,配置跟證書對應
server.ssl.enabled=true
server.ssl.key-store=classpath:tomcat.keystore
# server.ssl.key-store-type=JKS
server.ssl.key
-store-type=JKS # 密碼 server.ssl.key-store-password=123456 # springboot2.x不需要配置 server.ssl.key-password=123456 # 別名 server.ssl.key-alias=tomcat

配置http協議跳轉https

package com.dingsheng;

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.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DingshengApplication {

    public static void main(String[] args) {
        SpringApplication.run(DingshengApplication.class, args);
    }

    // SpringBoot2.x配置HTTPS,並實現HTTP訪問自動轉向HTTPS
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(){
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080); // 監聽Http的埠
        connector.setSecure(false);
        connector.setRedirectPort(8443); // 監聽Http埠後轉向Https埠
        return connector;
    }
}

相關其他博主文字,推薦大家可以參考

https://www.cnblogs.com/huanzi-qch/p/12133872.html