將SpringBoot項目的HTTP站點改為HTTPS的全過程
一 新建springboot項目
我們先新建一個springboot項目,寫一個BasicController.java,內(nèi)容如下
package com.https.httpsdemo.demos.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class BasicController {
// http://127.0.0.1:8081/hello
@GetMapping("/hello")
@ResponseBody
public String hello() {
return "Hello ";
}
}
application.yaml內(nèi)容如下
server: port: 8081
瀏覽器訪問http://127.0.0.1:8081/hello如下
接下來開始把http請求改為https
二 https證書
首先需要有一個 https 證書,可以從各個云服務(wù)廠商處申請一個免費的(我申請的為阿里云SSL),也可以直接借助 Java 自帶的 JDK 管理工具 keytool 來生成一個免費的 https 證書。
找到命令窗口,右鍵選擇管理員打開

執(zhí)行如下命令
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
命令含義如下:
- genkey 表示要創(chuàng)建一個新的密鑰。
- alias 表示 keystore 的別名。
- keyalg 表示使用的加密算法是 RSA ,一種非對稱加密算法。
- keysize 表示密鑰的長度。
- keystore 表示生成的密鑰存放位置。
- validity 表示密鑰的有效時間,單位為天。

如上圖,以此輸入相關(guān)信息即可。
在C:\Windows\System32目錄可以看到生成了文件

把上邊這個文件復(fù)制到項目根目錄下

application.yml文件如下
# 應(yīng)用服務(wù) WEB 訪問端口
server:
port: 8081
ssl:
# 證書路徑,秘鑰文件路徑,也可以配置絕對路徑
key-store: keystore.p12
# 證書密碼,秘鑰生成時在終端輸入的秘鑰口令
key-store-password: 123456
# 秘鑰類型,與秘鑰生成命令一樣
key-store-type: PKCS12
# 證書別名,與秘鑰生成命令一樣
key-alias: tomcat
啟動類添加如下內(nèi)容
package com.https.httpsdemo;
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 HttpsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(HttpsDemoApplication.class, args);
}
@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(redirectConnector());
return tomcat;
}
/**
* 瀏覽器訪問http://127.0.0.1:8080/hello時,自動跳轉(zhuǎn)到https://127.0.0.1:8081/hello
* @return
*/
private Connector redirectConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8081);
return connector;
}
}
此時,我們在瀏覽器就可以通過https來訪問https://127.0.0.1:8081/hello了
因為我用的是自己生成的ssl證書,所以https請求是不被客戶端識別的,如下

點擊后,內(nèi)容如下

注意:生產(chǎn)環(huán)境中,則需要購買合法的 SSL 證書。
由于在啟動類已經(jīng)進(jìn)行了配置,所以我們也可以通過訪問http://127.0.0.1:8080/hello來自動跳轉(zhuǎn)到https://127.0.0.1:8081/hello

以上就是將SpringBoot項目的HTTP站點改為HTTPS的全過程的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot HTTP站點改為HTTPS的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Java的Socket編寫的C/S聊天程序?qū)崿F(xiàn)
這篇文章主要介紹了基于Java的Socket編寫的C/S聊天程序?qū)崿F(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
RocketMQ?消息Message的結(jié)構(gòu)和使用方式詳解
Message是RocketMQ的數(shù)據(jù)包,它不僅是業(yè)務(wù)數(shù)據(jù)的載體,更是路由、過濾、追蹤、延遲、事務(wù)等功能的基礎(chǔ),掌握Message,你就掌握了RocketMQ的語言,本文給大家介紹什么是?Message及理解Message的結(jié)構(gòu)、屬性、生命周期和使用方式,感興趣的朋友一起看看吧2025-08-08
關(guān)于Selenium的UI自動化測試屏幕截圖功能實例代碼
今天小編就為大家分享一篇關(guān)于Selenium的UI自動化測試屏幕截圖功能實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
SpringSecurity OAtu2+JWT實現(xiàn)微服務(wù)版本的單點登錄的示例
本文主要介紹了SpringSecurity OAtu2+JWT實現(xiàn)微服務(wù)版本的單點登錄的示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
每天練一練Java函數(shù)與算法Math函數(shù)總結(jié)與字符串轉(zhuǎn)換整數(shù)
這篇文章主要介紹了Java函數(shù)與算法Math函數(shù)總結(jié)與字符串轉(zhuǎn)換整數(shù),每天練一練,水平在不知不覺中提高,需要的朋友快過來看看吧2021-08-08
SpringBoot使用Redis對用戶IP進(jìn)行接口限流的示例詳解
使用接口限流的主要目的在于提高系統(tǒng)的穩(wěn)定性,防止接口被惡意打擊,這篇文章主要介紹了SpringBoot使用Redis對用戶IP進(jìn)行接口限流的示例代碼,需要的朋友可以參考下2023-07-07

