可以在web.xml启用Filter,如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>httpHeaderSecurity</filter-name>
<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
<init-param>
<param-name>blockContentTypeSniffingEnabled</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>httpHeaderSecurity</filter-name>
<url-pattern>/pc/index.html</url-pattern>
</filter-mapping>
</web-app>
如果使用Tomcat8以上的版本,可以忽略这些配置,8.0以上版本已自带安全相关配置,如需用到,直接去tomcat/conf/web.xml启用即可。
Spring Boot 演示项目参见
传统web演示项目参见:
HttpHeaderSecurityFilter里的 blockContentTypeSniffingEnabled 参数即对应的是你要的
X-Content-Type-Options的值,可以是nosniff等。
参考:
https://stackoverflow.com/que...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…