springboot啟動https

一.證書生成

在使用https時躺屁,我們可以先生成一個證書在本地進行測試沸毁,生產(chǎn)中一般需要去購買https證殿漠。

1.本地生成

keytool -genkey -alias tomcat  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore 1122.p12 -validity 3650 

參數(shù)說明

1.-storetype 指定密鑰倉庫類型 
2.-keyalg 生成證書的算法名稱,RSA是一種非對稱加密算法 
3.-keysize 證書大小 
4.-keystore 生成的證書文件的存儲路徑 
5.-validity 證書的有效期

二.配置

1.配置文件設置https屬性

# https 開啟
server.ssl.key-store: classpath:1122.pfx
server.ssl.key-store-password: 1122
server.ssl.keyAlias: tomcat

2.將https文件,112.pfx卤妒,1122.key,1122.pem放到項目resource目錄下

3.設置https轉(zhuǎn)發(fā)

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.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableAutoConfiguration
public class StartAppHttps implements CommandLineRunner {

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                //Due to CONFIDENTIAL and /*, this will cause Tomcat to redirect every request to HTTPS.
                //You can configure multiple patterns and multiple constraints if you need more control over what is and is not redirected.

                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;

    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

        //Set the scheme that will be assigned to requests received through this connector
        //@param scheme The new scheme
        connector.setScheme("http");

        //Set the port number on which we listen for requests.
        // @param port The new port number
        connector.setPort(80);

        //Set the secure connection flag that will be assigned to requests received through this connector.
        //@param secure The new secure connection flag
        //if connector.setSecure(true),the http use the http and https use the https;else if connector.setSecure(false),the http redirect to https;
        connector.setSecure(false);

        //redirectPort The redirect port number (non-SSL to SSL)
        connector.setRedirectPort(443);
        return connector;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(StartHdfsAppHttps.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        // TODO Auto-generated method stub
    }
}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末丸边,一起剝皮案震驚了整個濱河市叠必,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌妹窖,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件收叶,死亡現(xiàn)場離奇詭異骄呼,居然都是意外死亡,警方通過查閱死者的電腦和手機判没,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門蜓萄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人澄峰,你說我怎么就攤上這事嫉沽。” “怎么了俏竞?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵绸硕,是天一觀的道長。 經(jīng)常有香客問我魂毁,道長玻佩,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任席楚,我火速辦了婚禮咬崔,結果婚禮上,老公的妹妹穿的比我還像新娘烦秩。我一直安慰自己垮斯,他們只是感情好,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布只祠。 她就那樣靜靜地躺著兜蠕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪铆农。 梳的紋絲不亂的頭發(fā)上牺氨,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機與錄音墩剖,去河邊找鬼猴凹。 笑死,一個胖子當著我的面吹牛岭皂,可吹牛的內(nèi)容都是我干的郊霎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼爷绘,長吁一口氣:“原來是場噩夢啊……” “哼书劝!你這毒婦竟也來了进倍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤购对,失蹤者是張志新(化名)和其女友劉穎猾昆,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體骡苞,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡垂蜗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了解幽。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片贴见。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖躲株,靈堂內(nèi)的尸體忽然破棺而出片部,到底是詐尸還是另有隱情,我是刑警寧澤霜定,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布档悠,位于F島的核電站,受9級特大地震影響然爆,放射性物質(zhì)發(fā)生泄漏站粟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一曾雕、第九天 我趴在偏房一處隱蔽的房頂上張望奴烙。 院中可真熱鬧,春花似錦剖张、人聲如沸切诀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽幅虑。三九已至,卻和暖如春顾犹,著一層夾襖步出監(jiān)牢的瞬間倒庵,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工炫刷, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留擎宝,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓浑玛,卻偏偏與公主長得像绍申,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理极阅,服務發(fā)現(xiàn)胃碾,斷路器,智...
    卡卡羅2017閱讀 134,662評論 18 139
  • https://nodejs.org/api/documentation.html 工具模塊 Assert 測試 ...
    KeKeMars閱讀 6,338評論 0 6
  • HTTPS介紹 超文本傳輸安全協(xié)議(英語:Hypertext Transfer Protocol Secure筋搏,縮...
    齊滇大圣閱讀 8,930評論 8 96
  • 每個男生心里仆百,都有一個養(yǎng)狗的情節(jié)吧。因為狗狗是忠誠的代名詞奔脐,而每個男生都想有一種被忠誠服侍的感覺儒旬。
    我媳婦叫文靜閱讀 226評論 0 0
  • JZW
    克塞爾閱讀 207評論 0 1