tomcat9如何配置hsts
时间: 2024-09-28 21:01:50 浏览: 154
Tomcat 9 中配置 HTTP Strict Transport Security (HSTS) 需要在 web.xml 或 server.xml 文件中完成。以下是基本步骤:
1. **web.xml** (在应用级别):
- 打开 web.xml 文件,在 `<security-constraint>` 标签下添加 HSTS 相关信息:
```xml
<web-app>
...
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<http-config>
<secure-channel>https-only</secure-channel>
<!-- 添加以下HSTS配置 -->
<http-header>
<name>Strict-Transport-Security</name>
<value>max-age=63072000; includeSubDomains; preload</value>
</http-header>
</http-config>
</security-constraint>
...
</web-app>
```
2. **server.xml** (在全局或虚拟主机级别):
- 在 `<Connector>` 标签中,添加 `ssl` 属性并配置HTTPS监听:
```xml
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" scheme="https" secure="true"
SSLEnabled="true" keystoreFile="path/to/keystore.jks"
keystorePass="yourKeystorePassword"
clientAuth="false" sslProtocol="TLS">
<!-- 添加HSTS配置 -->
<redirectPort>8080</redirectPort>
<connectionTimeout>20000</connectionTimeout>
<keystoreType>JKS</keystoreType>
<URIEncoding>UTF-8</URIEncoding>
<header>
<name>Strict-Transport-Security</name>
<value>max-age=63072000; includeSubDomains; preload</value>
</header>
</Connector>
```
确保你的 keystore 和 key 被正确配置并且路径有效。
3. **重启 Tomcat**: 完成配置后,需要重启 Tomcat 服务,让新的设置生效。
阅读全文
相关推荐


















