springboot項(xiàng)目開啟https協(xié)議的項(xiàng)目實(shí)現(xiàn)
1、在windows以管理員身份運(yùn)行cmd,輸入如下命令生成證書
keytool -genkey -alias myhttps -keyalg RSA -keysize 2048 -validity 36500 -keystore "D:/tmp/ssl/myhttps.keystore"
注釋
命令:keytool -genkey -alias testhttps -keyalg RSA -keysize 2048 -validity 36500 -keystore "D:/tmp/ssl/testhttps.keystore"
命令解釋:
- -genkey 表示要創(chuàng)建一個(gè)新的密鑰。
- -alias 表示 keystore 的別名。
- -keyalg 表示使用的加密算法是 RSA。
- -keysize 表示密鑰的長度.。
- -keystore 表示生成的密鑰存放位直。
- -validity 表示密鑰的有效時(shí)間,單位為天。
2、將目錄下的myhttps.keystore文件移動到resource下面

3、配置文件
server: ? port: 9987 ? non-ssl-port: 8089 # 用于 非ssl請求 強(qiáng)制轉(zhuǎn)成 ssl 請求 # 當(dāng)使用 訪問地址:http://127.0.0.1:8089/hello 訪問時(shí) 后臺會 將請求 轉(zhuǎn)換成 https://127.0.0.1:9987/hello # ?servlet: # ? ?context-path: /ssl-service ? ssl: ? ? key-store: classpath:myhttps.keystore ?#類路徑下的自簽證書 ? ? key-alias: myhttps # 證書別名 ? ? key-store-password: 123456 #證書密碼 ? ? key-store-type: JKS # 證書類型 ? ? enabled: true ?# 開啟證書驗(yàn)證
4、配置http強(qiáng)制跳轉(zhuǎn)https配置類
package com.example.springboot3.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;
/**
?* @author oukele
?* http 請求 強(qiáng)制跳轉(zhuǎn)成 https
?*/
@Configuration
public class HttpToHttpsConfig {
? ? /**
? ? ?* 項(xiàng)目指定的端口號
? ? ?*/
? ? @Value("${server.port}")
? ? private int serverPort;
? ? /**
? ? ?* 用于 非ssl請求 強(qiáng)制轉(zhuǎn)成 ssl 請求 的端口號
? ? ?*/
? ? @Value("${server.non-ssl-port}")
? ? private int port;
? ? @Bean
? ? public TomcatServletWebServerFactory servletContainerFactory() {
? ? ? ? TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
? ? ? ? ? ? @Override
? ? ? ? ? ? protected void postProcessContext(Context context) {
? ? ? ? ? ? ? ? //設(shè)置安全性約束
? ? ? ? ? ? ? ? SecurityConstraint securityConstraint = new SecurityConstraint();
? ? ? ? ? ? ? ? securityConstraint.setUserConstraint("CONFIDENTIAL");
? ? ? ? ? ? ? ? //設(shè)置約束條件
? ? ? ? ? ? ? ? SecurityCollection collection = new SecurityCollection();
? ? ? ? ? ? ? ? //攔截所有請求
? ? ? ? ? ? ? ? collection.addPattern("/*");
? ? ? ? ? ? ? ? securityConstraint.addCollection(collection);
? ? ? ? ? ? ? ? context.addConstraint(securityConstraint);
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
? ? ? ? //設(shè)置將分配給通過此連接器接收到的請求的方案
? ? ? ? connector.setScheme("http");
? ? ? ? //true: http使用http, https使用https;
? ? ? ? //false: http重定向到https;
? ? ? ? connector.setSecure(false);
? ? ? ? //設(shè)置監(jiān)聽請求的端口號,這個(gè)端口不能其他已經(jīng)在使用的端口重復(fù),否則會報(bào)錯
? ? ? ? connector.setPort(port);
? ? ? ? //重定向端口號(非SSL到SSL)
? ? ? ? connector.setRedirectPort(serverPort);
? ? ? ? tomcat.addAdditionalTomcatConnectors(connector);
? ? ? ? return tomcat;
? ? }
}5、在瀏覽器中測試

到此這篇關(guān)于springboot項(xiàng)目開啟https協(xié)議的項(xiàng)目實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot開啟https內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot如何配置ssl支持https
- SpringBoot配置HTTPS及開發(fā)調(diào)試的操作方法
- springboot實(shí)現(xiàn)的https單向認(rèn)證和雙向認(rèn)證(java生成證書)
- SpringBoot配置Https訪問的詳細(xì)步驟
- SpringBoot配置Https入門實(shí)踐
- SpringBoot的HTTPS配置實(shí)現(xiàn)
- springboot配置http跳轉(zhuǎn)https的過程
- springboot如何將http轉(zhuǎn)https
- springboot支持https請求的實(shí)現(xiàn)
- SpringBoot中支持Https協(xié)議的實(shí)現(xiàn)
- SpringBoot整合HTTPS的項(xiàng)目實(shí)踐
相關(guān)文章
Java使用fill()數(shù)組填充的實(shí)現(xiàn)
這篇文章主要介紹了Java使用fill()數(shù)組填充的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之線上水果超市商城的實(shí)現(xiàn)
這是一個(gè)使用了java+SSM+springboot+redis開發(fā)的網(wǎng)上水果超市商城,是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有水果超市商城該有的所有功能,感興趣的朋友快來看看吧2022-01-01
一次"java:程序包org.aspectj.lang不存在"問題解決實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了一次"java:程序包org.aspectj.lang不存在"問題解決的實(shí)戰(zhàn)過程,這個(gè)錯誤提示意味著你的Java程序中引用了org.aspectj.lang這個(gè)包,但是該包并不存在,文章通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
SpringBoot中打印SQL語句的幾種方法實(shí)現(xiàn)
本文主要介紹了SpringBoot中打印SQL語句的幾種方法實(shí)現(xiàn),,通過打印SQL語句可以幫助開發(fā)人員快速了解數(shù)據(jù)庫的操作情況,進(jìn)而進(jìn)行性能分析和調(diào)試,感興趣的可以了解一下2023-11-11

