1. 程式人生 > 程式設計 >Spring Boot專案如何同時支援HTTP和HTTPS協議的實現

Spring Boot專案如何同時支援HTTP和HTTPS協議的實現

如今,企業級應用程式的常見場景是同時支援HTTP和HTTPS兩種協議,這篇文章考慮如何讓Spring Boot應用程式同時支援HTTP和HTTPS兩種協議。

準備

為了使用HTTPS聯結器,需要生成一份Certificate keystore,用於加密和機密瀏覽器的SSL溝通。

如果你使用Unix或者Mac OS,可以通過下列命令:keytool -genkey -alias tomcat -keyalg RSA,在生成過程中可能需要你填入一些自己的資訊,例如我的機器上反饋如下:

可以看出,執行完上述命令後在home目錄下多了一個新的.keystore檔案。

實戰

首先在resources目錄下新建一個配置檔案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類中建立一個靜態類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;
  //這裡為了節省空間,省略了getters和setters,讀者在實踐的時候要加上
  
  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);
    }
  }
}

通過註解載入tomcat.https.properties配置檔案,並與TomcatSslConnectorProperties繫結,用註解修飾WebConfiguration類;

@Configuration
@PropertySource("classpath:/tomcat.https.properties")
@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)
public class WebConfiguration extends WebMvcConfigurerAdapter {...}

在WebConfiguration類中建立EmbeddedServletContainerFactory型別的Srping bean,並用它新增之前建立的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;
}

通過mvn spring-boot:run啟動應用程式;

在瀏覽器中訪問URLhttps://localhost:8443/internal/tomcat.https.properties

在瀏覽器中訪問URLhttp://localhost:8080/internal/application.properties

分析

根據之前的文章和官方文件,Spring Boot已經對外開放了很多伺服器配置,這些配置資訊通過Spring Boot內部的ServerProperties類完成繫結,若要參考Spring Boot的通用配置項,請點選這裡

Spring Boot不支援通過application.properties同時配置HTTP聯結器和HTTPS聯結器。在官方文件70.8中提到一種方法,是將屬性值硬編碼在程式中。

因此我們這裡新建一個配置檔案tomcat.https.properties來實現,但是這並不符合“Spring Boot風格”,後續有可能應該會支援“通過application.properties同時配置HTTP聯結器和HTTPS聯結器”。我新增的TomcatSslConnectorProperties是模仿Spring Boot中的ServerProperties的使用機制實現的,這裡使用了自定義的屬性字首custom.tomcat而沒有用現有的server.字首,因為ServerProperties禁止在其他的配置檔案中使用該名稱空間。

@ConfigurationProperties(prefix = "custom.tomcat.https")這個註解會讓Spring Boot自動將custom.tomcat.https開頭的屬性繫結到TomcatSslConnectorProperties這個類的成員上(確保該類的getters和setters存在)。值得一提的是,在繫結過程中Spring Boot會自動將屬性值轉換成合適的資料型別,例如custom.tomcat.https.keystore的值會自動繫結到File物件keystore上。

使用@PropertySource("classpath:/tomcat.https.properties")來讓Spring Boot載入tomcat.https.properties檔案中的屬性。

使用@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)讓Spring Boot自動建立一個屬性物件,包含上述通過@PropertySource匯入的屬性。

在屬性值匯入記憶體,並構建好TomcatSslConnectorProperties例項後,需要建立一個EmbeddedServletContainerFactory型別的Spring bean,用於建立EmbeddedServletContainer。

通過createSslConnector方法可以構建一個包含了我們指定的屬性值的聯結器,然後通過tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));設定tomcat容器的HTTPS聯結器。

參考資料

配置SSL

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。