ssl如何實(shí)現(xiàn)在spring boot中配置 ?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
紫陽網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
ssl協(xié)議位于tcp/ip協(xié)議與各種應(yīng)用協(xié)議之間,為數(shù)據(jù)通信提供安全支持。
ssl協(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í)我們?cè)L問http://localhost:8080 就會(huì)跳轉(zhuǎn)到 https://localhost:8088
關(guān)于ssl如何實(shí)現(xiàn)在spring boot中配置 問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
當(dāng)前文章:ssl如何實(shí)現(xiàn)在springboot中配置
轉(zhuǎn)載來于:http://www.chinadenli.net/article4/igppoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、微信公眾號(hào)、Google、面包屑導(dǎo)航、ChatGPT、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)