springboot https正式證書三步打造完成

1荒吏、到阿里云購買正式證書沾谜,并進行下載膊毁,放到springboot的resource同級目錄,如

image

2基跑、配置文件進行配置application.properties

image

3婚温、主程序進行https的配置,如下:

image

其中StartAppHttps類為:

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.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;

    }

@Override

public void run(String... args) throws Exception {

    // TODO Auto-generated method stub

}

}

就這三步就打造完成媳否,此時登錄https://localhost:8080/login 便可以登錄了栅螟。。篱竭。

?著作權(quán)歸作者所有,轉(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
  • 正文 為了忘掉前任隧期,我火速辦了婚禮飒责,結(jié)果婚禮上赘娄,老公的妹妹穿的比我還像新娘。我一直安慰自己宏蛉,他們只是感情好遣臼,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著拾并,像睡著了一般揍堰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嗅义,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天屏歹,我揣著相機與錄音,去河邊找鬼之碗。 笑死蝙眶,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的褪那。 我是一名探鬼主播幽纷,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼武通!你這毒婦竟也來了霹崎?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤冶忱,失蹤者是張志新(化名)和其女友劉穎尾菇,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體囚枪,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡派诬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了链沼。 大學(xué)時的朋友給我發(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
  • 正文 我出身青樓怀浆,卻偏偏與公主長得像谊囚,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子执赡,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

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