可以在web.xml启用Filter,如:<?xml version="1.0" encoding="UTF-8"?>
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">
httpHeaderSecurity
org.apache.catalina.filters.HttpHeaderSecurityFilter
blockContentTypeSniffingEnabled
true
httpHeaderSecurity
/pc/index.html
如果使用Tomcat8以上的版本,可以忽略这些配置,8.0以上版本已自带安全相关配置,如需用到,直接去tomcat/conf/web.xml启用即可。
Spring Boot 演示项目参见
传统web演示项目参见:
HttpHeaderSecurityFilter里的 blockContentTypeSniffingEnabled 参数即对应的是你要的
X-Content-Type-Options的值,可以是nosniff等。