tomcat设置X-Frame-Option

1. 若是tomcat的lib目录下的catalina.jar中有org.apache.catalina.filters.HttpHeaderSecurityFilterjava

Apache tomcat 7.0.90 和 tomcat 8以上都有HttpHeaderSecurityFilter

能够在tomcat下的conf里的web.xml中增长如下过滤器web

<filter>
  <filter-name>httpHeaderSecurity</filter-name>
  <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
  <init-param>
    <param-name>antiClickJackingEnabled</param-name>
    <param-value>true</param-value>
  </init-param>
  <init-param>
    <param-name>antiClickJackingOption</param-name>
    <param-value>SAMEORIGIN</param-value>
  </init-param>
  <async-supported>true</async-supported>
</filter>
<filter-mapping>
  <filter-name>httpHeaderSecurity</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

 2. 若是没有HttpHeaderSecurityFilter,须要本身写过滤器,添加以下代码,本身在项目中配置拦截。apache

HttpServletResponse response = (HttpServletResponse) sResponse;
response.addHeader("x-frame-options","SAMEORIGIN");

X-Frame-Options 有三个值:tomcat

DENYapp

表示该页面不容许在 frame 中展现,即使是在相同域名的页面中嵌套也不容许。async

SAMEORIGIN网站

表示该页面能够在相同域名页面的 frame 中展现。url

ALLOW-FROM urispa

表示该页面能够在指定来源的 frame 中展现。code

换一句话说,若是设置为 DENY,不光在别人的网站 frame 嵌入时会没法加载,在同域名页面中一样会没法加载。

另外一方面,若是设置为 SAMEORIGIN,那么页面就能够在同域名页面的 frame 中嵌套。

相关文章
相关标签/搜索