乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      讓你的Spring Boot工程支持HTTP和HTTPS

       feimishiwo 2017-02-22

      如今,企業(yè)級(jí)應(yīng)用程序的常見(jiàn)場(chǎng)景是同時(shí)支持HTTP和HTTPS兩種協(xié)議,這篇文章考慮如何讓Spring Boot應(yīng)用程序同時(shí)支持HTTP和HTTPS兩種協(xié)議。

      準(zhǔn)備

      為了使用HTTPS連接器,需要生成一份Certificate keystore,用于加密和機(jī)密瀏覽器的SSL溝通。

      如果你使用Unix或者M(jìn)ac OS,可以通過(guò)下列命令:keytool -genkey -alias tomcat -keyalg RSA,在生成過(guò)程中可能需要你填入一些自己的信息,例如我的機(jī)器上反饋如下:


      生成kestore

      可以看出,執(zhí)行完上述命令后在home目錄下多了一個(gè)新的.keystore文件。

      How Do

      • 首先在resources目錄下新建一個(gè)配置文件tomcat.https.properties,用于存放HTTPS的配置信息;
      custom.tomcat.https.port=8443
      custom.tomcat.https.secure=true
      custom.tomcat.https.scheme=https
      custom.tomcat.https.ssl=true
      custom.tomcat.https.keystore=${user.home}/.keystore
      custom.tomcat.https.keystore-password=changeit
      • 然后在WebConfiguration類中創(chuàng)建一個(gè)靜態(tài)類TomcatSslConnectorProperties;
      @ConfigurationProperties(prefix = "custom.tomcat.https")
      public static class TomcatSslConnectorProperties {
          private Integer port;
          private Boolean ssl = true;
          private Boolean secure = true;
          private String scheme = "https";
          private File keystore;
          private String keystorePassword;
          //這里為了節(jié)省空間,省略了getters和setters,讀者在實(shí)踐的時(shí)候要加上
      
          public void configureConnector(Connector connector) {
              if (port != null) {
                  connector.setPort(port);
              }
              if (secure != null) {
                  connector.setSecure(secure);
              }
              if (scheme != null) {
                  connector.setScheme(scheme);
              }
              if (ssl != null) {
                  connector.setProperty("SSLEnabled", ssl.toString());
              }
              if (keystore != null && keystore.exists()) {
                  connector.setProperty("keystoreFile", keystore.getAbsolutePath());
                  connector.setProperty("keystorePassword", keystorePassword);
              }
          }
      }
      • 通過(guò)注解加載tomcat.https.properties配置文件,并與TomcatSslConnectorProperties綁定,用注解修飾WebConfiguration類;
      @Configuration
      @PropertySource("classpath:/tomcat.https.properties")
      @EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)
      public class WebConfiguration extends WebMvcConfigurerAdapter {...}
      • 在WebConfiguration類中創(chuàng)建EmbeddedServletContainerFactory類型的Srping bean,并用它添加之前創(chuàng)建的HTTPS連接器。
      @Bean
      public EmbeddedServletContainerFactory servletContainer(TomcatSslConnectorProperties properties) {
          TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
          tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
          return tomcat;
      }
      
      private Connector createSslConnector(TomcatSslConnectorProperties properties) {
          Connector connector = new Connector();
          properties.configureConnector(connector);
          return connector;
      }
      • 通過(guò)mvn spring-boot:run啟動(dòng)應(yīng)用程序;
      • 在瀏覽器中訪問(wèn)URLhttps://localhost:8443/internal/tomcat.https.properties

      支持HTTPS協(xié)議
      • 在瀏覽器中訪問(wèn)URLhttp://localhost:8080/internal/application.properties

      同時(shí)支持HTTP協(xié)議

      分析

      根據(jù)之前的文章和官方文檔,Spring Boot已經(jīng)對(duì)外開(kāi)放了很多服務(wù)器配置,這些配置信息通過(guò)Spring Boot內(nèi)部的ServerProperties類完成綁定,若要參考Spring Boot的通用配置項(xiàng),請(qǐng)點(diǎn)擊這里

      Spring Boot不支持通過(guò)application.properties同時(shí)配置HTTP連接器和HTTPS連接器。在官方文檔70.8中提到一種方法,是將屬性值硬編碼在程序中。

      因此我們這里新建一個(gè)配置文件tomcat.https.properties來(lái)實(shí)現(xiàn),但是這并不符合“Spring Boot風(fēng)格”,后續(xù)有可能應(yīng)該會(huì)支持“通過(guò)application.properties同時(shí)配置HTTP連接器和HTTPS連接器”。我添加的TomcatSslConnectorProperties是模仿Spring Boot中的ServerProperties的使用機(jī)制實(shí)現(xiàn)的,這里使用了自定義的屬性前綴custom.tomcat而沒(méi)有用現(xiàn)有的server.前綴,因?yàn)镾erverProperties禁止在其他的配置文件中使用該命名空間。

      @ConfigurationProperties(prefix = "custom.tomcat.https")這個(gè)注解會(huì)讓Spring Boot自動(dòng)將custom.tomcat.https開(kāi)頭的屬性綁定到TomcatSslConnectorProperties這個(gè)類的成員上(確保該類的getters和setters存在)。值得一提的是,在綁定過(guò)程中Spring Boot會(huì)自動(dòng)將屬性值轉(zhuǎn)換成合適的數(shù)據(jù)類型,例如custom.tomcat.https.keystore的值會(huì)自動(dòng)綁定到File對(duì)象keystore上。

      使用@PropertySource("classpath:/tomcat.https.properties")來(lái)讓Spring Boot加載tomcat.https.properties文件中的屬性。

      使用@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)讓Spring Boot自動(dòng)創(chuàng)建一個(gè)屬性對(duì)象,包含上述通過(guò)@PropertySource導(dǎo)入的屬性。

      在屬性值導(dǎo)入內(nèi)存,并構(gòu)建好TomcatSslConnectorProperties實(shí)例后,需要?jiǎng)?chuàng)建一個(gè)EmbeddedServletContainerFactory類型的Spring bean,用于創(chuàng)建EmbeddedServletContainer。

      通過(guò)createSslConnector方法可以構(gòu)建一個(gè)包含了我們指定的屬性值的連接器,然后通過(guò)tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));設(shè)置tomcat容器的HTTPS連接器。

      參考資料

      1. 配置SSL

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多