欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

詳解在SpringBoot中使用Https

本文介紹如何在Spring Boot中,使用Https提供服務(wù),并將Http請求自動重定向到Https。

甘孜州網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

Https證書

巧婦難為無米之炊,開始的開始,要先取得Https證書。你可以向證書機(jī)構(gòu)申請證書,也可以自己制作根證書。

創(chuàng)建Web配置類

在代碼中創(chuàng)建一個使用了Configuration注解的類,就像下面這段代碼一樣:

@Configuration
public class WebConfig {
    //Bean 定義...
}

配置Https

在配置類中添加EmbeddedServletContainerCustomizer Bean,并在其中配置Https證書和端口號。

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
  return new EmbeddedServletContainerCustomizer() {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
      Ssl ssl = new Ssl();
      //Server.jks中包含服務(wù)器私鑰和證書
      ssl.setKeyStore("server.jks");
      ssl.setKeyStorePassword("123456");
      container.setSsl(ssl);
      container.setPort(8443);
    }
  };
}

配置Http使其自動重定向到Https

Embedded默認(rèn)只有一個Connector,要在提供Https服務(wù)的同時支持Http,需要添加一個Connector。在配置類中添加如下配置:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
  TomcatEmbeddedServletContainerFactory factory =
    new TomcatEmbeddedServletContainerFactory() {
      @Override
      protected void postProcessContext(Context context) {
        //SecurityConstraint必須存在,可以通過其為不同的URL設(shè)置不同的重定向策略。
        SecurityConstraint securityConstraint = new SecurityConstraint();
        securityConstraint.setUserConstraint("CONFIDENTIAL");
        SecurityCollection collection = new SecurityCollection();
        collection.addPattern("/*");
        securityConstraint.addCollection(collection);
        context.addConstraint(securityConstraint);
      }
    };
  factory.addAdditionalTomcatConnectors(createHttpConnector());
  return factory;
}
 
private Connector createHttpConnector() {
  Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  connector.setScheme("http");
  connector.setSecure(false);
  connector.setPort(8080);
  connector.setRedirectPort(8443);
  return connector;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁題目:詳解在SpringBoot中使用Https
網(wǎng)頁URL:http://chinadenli.net/article46/gshehg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站網(wǎng)站建設(shè)標(biāo)簽優(yōu)化全網(wǎng)營銷推廣網(wǎng)站策劃關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)