Composer 中的 ThingWorx 模型定義 > 安全性 > 在 Apache Tomcat 中啟用 HSTS
在 Apache Tomcat 中啟用 HSTS
欲在 Apache Tomcat 中啟用「HTTP 強制安全傳輸技術」(HSTS),PTC 建議使用以下提供的資訊。
簡介
HTTP HSTS 是一種機制,可讓網站宣告它們只能透過安全連線 (HTTPS) 存取。此機制由 RFC6797 指定,並使用 Strict-Transport-Security 回應標題來通知使用者使用者代理程式 (UA) 有關網站所需安全原則的資訊。
HSTS 可解決下列威脅:
使用者將 http://myDomain.com 加入書籤或手動輸入上列網址容易遭受攔截式攻擊。
HSTS 會自動將 HTTP 請求重新導向至目標網域的 HTTPS。
原本是純 HTTPS 的 Web 應用程式不慎包含 HTTP 連結或透過 HTTP 提供內容。
HSTS 會自動將 HTTP 請求重新導向至目標網域的 HTTPS。
攔截式攻擊者嘗試使用無效憑證攔截來自受害使用者的流量,並希望使用者接受錯誤憑證。
HSTS 不允許使用者取代無效的憑證訊息。
HSTS 也不需要 HTTP 到 HTTPS 重新導向,因此可以減少存取延遲。
Strict-Transport-Security 標題:
僅在透過 HTTPS 連線傳送時才會辨識。網站仍可允許使用者使用 HTTP 與網站互動,以提供與非 HTTPS 使用者代理程式之間的相容性。
必須包含 max-age 指令。值可以指定 UA 將主機視為已知 HSTS 主機的秒數 (值 0 表示停止處理)。
其可包含 includeSubdomain 指令,該指令若存在的話可通知 UA,HSTS 原則會套用至 HSTS 主機以及主機網域名稱的任何子網域。
指示網站擁有者同意預先載入其網域的預先載入旗標。預先載入網域後,瀏覽器即已知曉主機需要使用 SSL/TLS,因此會移除仍能發生攻擊的較小期間:初始 HTTP 到 HTTPS 重新導向。網站擁有者必須將網域提交至清單 ( 此處)。
Tomcat 中的 HSTS
Apache Tomcat v8.0.23 提供新的 HttpHeaderSecurityFilter,其會將 Strict-Transport-SecurityX-Frame-OptionsX-Content-Type-Options HTTP 標題新增至回應。可以像其他任何篩選器一樣,透過 web.xml 檔案新增及配置篩選器。有關篩選器的描述,請參閱 此處,Tomcat 安裝也在 conf/web.xml 中隨附了一個範例。以下範例即是 web.xml 檔案的一個程式碼片段,可讓您在 Tomcat 中啟用 HSTS:
<!-- ================== Built In Filter Definitions ===================== -->

<!-- A filter that sets various security related HTTP Response headers. -->
<!-- This filter supports the following initialization parameters -->
<!-- (default values are in square brackets): -->
<!-- -->
<!-- hstsEnabled Should the HTTP Strict Transport Security -->
<!-- (HSTS) header be added to the response? See -->
<!-- RFC 6797 for more information on HSTS. [true] -->
<!-- -->
<!-- hstsMaxAgeSeconds The max age value that should be used in the -->
<!-- HSTS header. Negative values will be treated -->
<!-- as zero. [0] -->
<!-- -->
<!-- hstsIncludeSubDomains -->
<!-- Should the includeSubDomains parameter be -->
<!-- included in the HSTS header. -->
<!-- -->
<!-- antiClickJackingEnabled -->
<!-- Should the anti click-jacking header -->
<!-- X-Frame-Options be added to every response? -->
<!-- [true] -->
<!-- -->
<!-- antiClickJackingOption -->
<!-- What value should be used for the header. Must -->
<!-- be one of DENY, SAMEORIGIN, ALLOW-FROM -->
<!-- (case-insensitive). [DENY] -->
<!-- -->
<!-- antiClickJackingUri IF ALLOW-FROM is used, what URI should be -->
<!-- allowed? [] -->
<!-- -->
<!-- blockContentTypeSniffingEnabled -->
<!-- Should the header that blocks content type -->
<!-- sniffing be added to every response? [true] -->
<filter>
<filter-name>httpHeaderSecurity</filter-name>
<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>hstsEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>hstsMaxAgeSeconds</param-name>
<param-value>31536000</param-value>
</init-param>
<init-param>
<param-name>hstsIncludeSubDomains</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<!-- The mapping for the HTTP header security Filter -->
<filter-mapping>
<filter-name>httpHeaderSecurity</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
只有在 UA 透過 HTTP 存取網站時,才會傳回 Strict-Transport-Security 標題,因此必須使用 SSL/TLS 配置 Tomcat (有關安全 Tomcat 設定,請參閱 此處)。由於只有在連線安全時才會傳回 Strict-Transport-Security,因此網站擁有者必須決定下列事項:
他們是否也為了實現回溯相容性而透過不安全連線為網站提供服務。
他們是否將不安全連線重新導向至安全連線 (所需的)。
如果是第二種情況,可以透過在 web.xml 中新增安全性條件約束,並在 server.xml 中新增重新導向,來在 Tomcat 中設定重新導向。例如:
<security-constraint>
<web-resource-collection>
<web-resource-name>twx-portal</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
設定 HTTP 到 HTTPS 重新導向 (選項)
如需強制將 HTTP 流量重新導向至安全連線,請確定在 server.xml 中所定義 HTTP 連接器的 redirectPort 屬性設定為指向正確埠。如果未定義連接器,請予以新增。Tt 看起來應該類似於以下內容:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
WEB-INF/web.xml 中,新增安全性條件約束:
<security-constraint>
<web-resource-collection>
<web-resource-name>twx-portal</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>