SpringBoot系列——啟用https


  前言

  有時候我們需要使用https安全協議,本文記錄在SpringBoot項目啟用https

 

  生成證書

  自簽名證書

  使用java jdk自帶的生成SSL證書的工具keytool生成自己的證書

  1、打開cmd

  2、輸入命令生成證書

keytool -genkeypair -alias tomcat_https -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore d:/tomcat_https.keystore -storepass 123456

    

    

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

 

 

  域名型證書

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

 

  2020-01-10更新:今天使用內網穿透工具分給我們的二級域名去騰訊雲申請證書,並記錄一下

  1、登錄騰訊雲  -> 證書管理 -> 申請免費證書

  2、按照表單要求正確填寫內容(填寫的域名不需要www開頭)

  3、使用“文件驗證”的方式進行域名驗證 (https://cloud.tencent.com/document/product/400/4142

  首先看文檔說明:

  

  

  在springBoot項目中的static文件夾新建,然后把文件內容復制進去

  

   啟動項目,訪問 http://XXXX/.well-known/pki-validation/fileauth.txt,返回文件內容

  等待 CA 機構掃描審核、頒發證書  

   

 

  另外,內網穿透隧道協議類型要改成https,本地端口改成443,其他的不用變

 

  效果

  

 

  項目配置

  導入證書

  把生成的tomcat_https.keystore放在resources里(任意安全目錄都可以)

  

 

   

  配置文件

#https默認端口:443,http默認端口:80
server.port=443
server.http-port=80

#開啟https,配置跟證書一一對應
server.ssl.enabled=true
#指定證書
server.ssl.key-store=classpath:tomcat_https.keystore
server.ssl.key-store-type=JKS
#別名
server.ssl.key-alias=tomcat_https
#密碼
server.ssl.key-password=123456
server.ssl.key-store-password=123456

spring.application.name=springboot-https

 

 

  測試與效果

  新增測試controller

package cn.huanzi.qch.springboothttps.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HttpsController {

    @GetMapping("/hello")
    public String hello() {
        return "SpringBoot系列——啟用https";
    }

}

 

  由於是自簽名證書,瀏覽器不認可

 

   選擇“高級”,選擇繼續訪問即可

 

   成功訪問

 

 

  客戶端信任證書

  每次打開瀏覽器都阻止訪問,很煩,因此需要導出.car文件證書,給客戶端安裝

keytool -keystore d:/tomcat_https.keystore -export -alias tomcat_https -file d:/server.cer

  雙擊安裝,選擇導入到受信任的跟證書頒發機構

 

 

   這樣訪問就不會再阻止了,但還是顯示證書無效

 

 

  http強制跳轉https

  注入TomcatServletWebServerFactory,監聽http重定向到https

package cn.huanzi.qch.springboothttps.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.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * http強制跳轉https
 */
@Configuration
public class Http2Https {

    @Value("${server.port}")
    private int sslPort;//https的端口

    @Value("${server.http-port}")
    private int httpPort;//http的端口

    @Bean
    public TomcatServletWebServerFactory servletContainerFactory() {
        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);
            }
        };
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        //設置將分配給通過此連接器接收到的請求的方案
        connector.setScheme("http");

        //true: http使用http, https使用https;
        //false: http重定向到https;
        connector.setSecure(false);

        //設置監聽請求的端口號,這個端口不能其他已經在使用的端口重復,否則會報錯
        connector.setPort(httpPort);

        //重定向端口號(非SSL到SSL)
        connector.setRedirectPort(sslPort);

        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }
}

 

  效果

  后記

  部分代碼參考:https://www.cnblogs.com/niumoo/p/11717657.html

  

  代碼開源

  代碼已經開源、托管到我的GitHub、碼雲:

  GitHub:https://github.com/huanzi-qch/springBoot

  碼雲:https://gitee.com/huanzi-qch/springBoot


免責聲明!

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



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