Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

javaweb项目,html文件放在了WebRoot下,如何拦截访问html的请求呀?

主要目的是,想设置响应头,
给响应头添加上“X-Content-Type-Options”
可是在webroot下的html是直接就访问到了,也没有响应呀?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可以在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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...