springboot 同時啟用http/https的配置方法
1. 啟用HTTPS
修改配置
application.yml
server:
# port: 80
port: 443
ssl:
enabled: true
key-store: /key_store.jks
key-store-password: key_store_pwd
2. 添加http協(xié)議連接器
增加JAVA配置
@Bean
public ServletWebServerFactory servletWebServerFactory() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setPort(80);
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
擴(kuò)展知識點(diǎn):springboot如何配置,同時支持https和http
使用jdk自帶的keytools創(chuàng)建證書
keytool -genkey -alias tomcat -keyalg RSA -keystore ./server.keystore
按照提示完成操作
輸入密鑰庫口令:123456
再次輸入新口令:123456
您的名字與姓氏是什么?
[Unknown]: kaibowang
您的組織單位名稱是什么?
[Unknown]: yuxuelian
您的組織名稱是什么?
[Unknown]: yuxuelian
您所在的城市或區(qū)域名稱是什么?
[Unknown]: chengdu
您所在的省/市/自治區(qū)名稱是什么?
[Unknown]: chengdushi
該單位的雙字母國家/地區(qū)代碼是什么?
[Unknown]: china
CN=kaibowang, OU=yuxuelian, O=yuxuelian, L=chengdu, ST=chengdushi, C=china是否正確?
[否]: y輸入 <tomcat> 的密鑰口令
(如果和密鑰庫口令相同, 按回車):
再次輸入新口令:Warning:
JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore C:\Users\Administrator\.keystore -destkeystore C:\Users\Administrator\.keystore -deststoretype pkcs12" 遷移到行業(yè)標(biāo)準(zhǔn)格式 PKCS12。
創(chuàng)建完成后,可在用戶根目錄查看生成的keystore文件
將生成的keystore文件復(fù)制到項(xiàng)目的根目錄下

在application.yml中添加配置
server:
port: 443
ssl:
key-store: server.keystore
key-store-password: 生成server.keystore時輸入的密碼
key-alias: tomcat
key-store-type: JKS
在application啟動文件中添加配置
package com.cisdi.info.simple;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
/**
*
* @author CISDI
* @date 2018/04/27
*/
@SpringBootApplication(scanBasePackages = {"com.cisdi.info.simple.*"}, exclude = {SecurityAutoConfiguration.class})
@EntityScan("com.cisdi.info.simple.*")
@EnableDiscoveryClient(autoRegister = false)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
//配置http
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(8080);
return connector;
}
}
完成配置
以上就是springboot 同時啟用http/https的配置方法的詳細(xì)內(nèi)容,更多關(guān)于springboot啟用http/https的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于java中構(gòu)造函數(shù)的一些知識詳解
下面小編就為大家?guī)硪黄P(guān)于java中構(gòu)造函數(shù)的一些知識詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
IDEA java出現(xiàn)無效的源發(fā)行版14解決方案
這篇文章主要介紹了IDEA java出現(xiàn)無效的源發(fā)行版14解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
Java的面向?qū)ο缶幊袒靖拍顚W(xué)習(xí)筆記整理
這篇文章主要介紹了Java的面向?qū)ο缶幊袒靖拍顚W(xué)習(xí)筆記整理,包括類與方法以及多態(tài)等支持面向?qū)ο笳Z言中的重要特點(diǎn),需要的朋友可以參考下2016-01-01
Java 獲取當(dāng)前類名和方法名的實(shí)現(xiàn)方法
這篇文章主要介紹了 Java 獲取當(dāng)前類名和方法名的實(shí)現(xiàn)方法的相關(guān)資料,這里不僅提供了實(shí)現(xiàn)方法并比較幾種方法的效率,需要的朋友可以參考下2017-07-07
Spring Boot緩存實(shí)戰(zhàn) Caffeine示例
本篇文章主要介紹了Spring Boot緩存實(shí)戰(zhàn) Caffeine示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02

