詳解spring boot配置 ssl
ssl協(xié)議位于tcp/ip協(xié)議與各種應(yīng)用協(xié)議之間,為數(shù)據(jù)通信提供安全支持。
ssl協(xié)議分為兩層:
- ssl記錄協(xié)議,它建立在可靠傳輸協(xié)議之上,為高層協(xié)議提供數(shù)據(jù)封裝、壓縮、加密等基本功能支持。
- ssl握手協(xié)議,它建立在ssl記錄協(xié)議之上,用于實(shí)際數(shù)據(jù)傳輸開始前,通信雙方進(jìn)行身份認(rèn)證、協(xié)商加密算法、交換加密密鑰等
基于B/S的web應(yīng)用,是通過https來實(shí)現(xiàn)ssl的。https是http的安全版,即在http下加入ssl層,https的安全基礎(chǔ)是ssl;
我們開始在spring boot中使用ssl設(shè)置;
1.生成證書
每一個(gè)jdk或者jre中都有一個(gè)工具叫keytool,它是一個(gè)證書管理工具,可以用來生成自簽名的證書;打開cmd,進(jìn)入jdk/bin路徑,敲入命令
keytool -genkey -alias tomcat
在用戶路徑下生成 .keystore文件 ,這就是我們要使用的證書文件。

2.spring boot配置ssl
將.keystore文件復(fù)制到項(xiàng)目根目錄,然后配置application.properties中做ssl配置
server.ssl.key-store=.keystore server.ssl.key-store-password=密碼 server.ssl.keyStoreType = JKS server.ssl.keyAlias=tomcat
啟動(dòng)項(xiàng)目
訪問地址 https://localhost:8080

3、http轉(zhuǎn)https
要實(shí)現(xiàn)這個(gè)功能,我們需要配置TomcatEmbeddedServletContainerFactory,并且添加tomcat的connector來實(shí)現(xiàn)。
package com.example;
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.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created by xingzhuipingye on 2017/5/7.
*/
@Controller
@SpringBootApplication
public class ApplicationMy {
@RequestMapping("/")
public String index(Model model){
Person single = new Person("aa",11);
List<Person> list = new ArrayList<>();
Person p1 = new Person("xx",11);
Person p2 = new Person("yy",22);
Person p3 = new Person("zz",33);
list.add(p1);
list.add(p2);
list.add(p3);
model.addAttribute("singlePerson",single);
model.addAttribute("people",list);
return "index";
}
public static void main(String[] args){
SpringApplication.run(ApplicationMy.class);
}
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(){
@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);
connector.setSecure(false);
connector.setRedirectPort(8088);
return connector;
}
}
注:我在application.properties 中修改了端口為8088
此時(shí)我們訪問http://localhost:8080 就會(huì)跳轉(zhuǎn)到 https://localhost:8088
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot從配置文件properties讀取字符串亂碼的解決
這篇文章主要介紹了Springboot從配置文件properties讀取字符串亂碼的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
java GUI實(shí)現(xiàn)加法計(jì)算器
這篇文章主要為大家詳細(xì)介紹了java GUI實(shí)現(xiàn)加法計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Java中獲取時(shí)間戳的三種方式對比實(shí)現(xiàn)
這篇文章主要介紹了Java中獲取時(shí)間戳的三種方式對比實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
淺談SpringSecurity注解與AOP切面執(zhí)行順序
這篇文章主要介紹了淺談SpringSecurity注解與AOP切面執(zhí)行順序,引入Spring Security后,在Controller的方法中會(huì)出現(xiàn)Spring Security的方法注解與AOP同時(shí)存在的問題,這是就會(huì)設(shè)計(jì)順序問題,需要的朋友可以參考下2023-10-10
springboot?集成easy-captcha實(shí)現(xiàn)圖像驗(yàn)證碼顯示和登錄
本文主要介紹了springboot?集成easy-captcha實(shí)現(xiàn)圖像驗(yàn)證碼顯示和登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

