X-Frame-Options HTTP 响应头是用来给浏览器指示允许一个页面可否在 <frame>, </iframe> 或者 <object> 中展现的标记。网站可以使用此功能,来确保自己网站的内容没有被嵌套到别人的网站中去,也从而避免了点击劫持 (clickjacking) 的攻击。

X-Frame-Options三个参数:

1、DENY

表示该页面不允许在frame中展示,即便是在相同域名的页面中嵌套也不允许。

2、SAMEORIGIN

表示该页面可以在相同域名页面的frame中展示。

3、ALLOW-FROM uri

表示该页面可以在指定来源的frame中展示。

换一句话说,如果设置为DENY,不光在别人的网站frame嵌入时会无法加载,在同域名页面中同样会无法加载。另一方面,如果设置为SAMEORIGIN,那么页面就可以在同域名页面的frame中嵌套。正常情况下我们通常使用SAMEORIGIN参数。

Apache配置

需要把下面这行添加到 'site' 的配置中

1

Header always append X-Frame-Options SAMEORIGIN

nginx配置

需要添加到 ‘http’, ‘server’ 或者 ‘location’ 的配置项中,个人来讲喜欢配置在‘server’ 中

正常情况下都是使用SAMEORIGIN参数,允许同域嵌套

1

add_header X-Frame-Options SAMEORIGIN;

允许单个域名iframe嵌套

1

add_header X-Frame-Options ALLOW-FROM http://whsir.com/;

允许多个域名iframe嵌套,注意这里是用逗号分隔

1

add_header X-Frame-Options "ALLOW-FROM http://whsir.com/,https://cacti.org.cn/";

IIS配置

添加下面的配置到 ‘Web.config’文件中

1

2

3

4

5

6

7

8

9

<system.webServer>

...

<httpProtocol>

<customHeaders>

<add name="X-Frame-Options" value="SAMEORIGIN" />

</customHeaders>

</httpProtocol>

...

</system.webServer>

HAProxy配置

添加下面这行到 ‘front-end, listen, or backend’配置中

1

rspadd X-Frame-Options:\ SAMEORIGIN

Tomcat配置

在 ‘conf/web.xml’填加以下配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<filter>

<filter-name>httpHeaderSecurity</filter-name>

<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>

<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>

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

</filter-mapping>

配置后如何确定X-Frame-Options是否已生效呢?我这里以Google浏览器为例,打开网站按F12键,选择Network,找到对应的Headers,如下图所示

EnableWebSecurity配置

-Frame-Options 有三个值:
DENY
表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。
SAMEORIGIN
表示该页面可以在相同域名页面的 frame 中展示。
ALLOW-FROM uri
表示该页面可以在指定来源的 frame 中展示。

spring boot支持EnableWebSecurity 这个anotation来设置不全的安全策略。 具体如下:

import com.alibaba.spring.websecurity.DefaultWebSecurityConfigurer;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.header.writers.frameoptions.WhiteListedAllowFromStrategy;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;import java.util.Arrays;@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {@Overrideprotected void configure(HttpSecurity http) throws Exception {super.configure(http);//disable 默认策略。 这一句不能省。 http.headers().frameOptions().disable();//新增新的策略。 http.headers().addHeaderWriter(new XFrameOptionsHeaderWriter(new WhiteListedAllowFromStrategy(Arrays.asList("http://itaobops.aliexpress.com", "https://cpp.alibaba-inc.com","https://pre-cpp.alibaba-inc.com"))));}
}

上面是支持ALLOW-FROM uri的设置方式。

其他设置方式比较简单。 下面是支持SAMEORIGIN的设置方式:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {@Overrideprotected void configure(HttpSecurity http) throws Exception {super.configure(http);http.headers().frameOptions().sameOrigin();}
}

三、去除x-frame-options header配置:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {@Overrideprotected void configure(HttpSecurity http) throws Exception {super.configure(http);http.headers().frameOptions().disable();}
}

X-Frame-Options响应头配置详解相关推荐

  1. http常用请求头与响应头字段详解

    请求头 Accept: 例: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/ ...

  2. SpringBoot的配置详解application

    SpringBoot的配置文件application有两种文件格式,两种配置的内容是一致的,只是格式不一致. 1.application.properties 2.application.yml或者a ...

  3. varnish配置详解

       varnish配置详解 能用到缓存的服务器的原因是,应用到了程序的局部性. 空间局部性:一个程序最近访问了一个空间,那么他周边的空间也将被访问. 时间的局部性:一条指令一段时间内被执行,之后的一 ...

  4. HTTP协议的头信息详解

    HTTP协议的头信息详解 http://blog.csdn.net/guoguo1980/archive/2008/07/14/2649658.aspx HTTP协议的头信息详解 HTTP(Hyper ...

  5. VC项目配置详解(转)

    VC项目配置详解(转) http://fishboyzyf.blog.163.com/blog/static/6183821020118992835382/ VC项目配置详解 一.IDE基础配置 1. ...

  6. Nginx 反向代理工作原理简介与配置详解

    Nginx 反向代理工作原理简介与配置详解 测试环境 CentOS 6.8-x86_64 nginx-1.10.0 下载地址:http://nginx.org/en/download.html 安装 ...

  7. Tomcat 的 Server 文件配置详解

    转载自  Tomcat 的 Server 文件配置详解 前言 Tomcat隶属于Apache基金会,是开源的轻量级Web应用服务器,使用非常广泛.server.xml是Tomcat中最重要的配置文件, ...

  8. Nginx开启Gzip压缩配置详解

    Nginx开启Gzip压缩配置详解 最近生产上发生了一些问题,原先所有的静态资源文件都是经过gzip压缩的,然而这几天突然都没有压缩了,经过一顿排查,发现是Nginx的配置有问题,借此机会详细了解了N ...

  9. linux下DNS配置详解

    linux下DNS配置详解 DNS 是域名系统 (Domain Name Server) 的缩写,该系统用于命名组织到域层次结构中的计算机和网络服务.在Internet上域名与IP地址之间是一一对应的 ...

  10. Apache日志配置详解(rotatelogs LogFormat)

    logs/error_log CustomLog logs/access_log common --默认为以上部分 修改为如下: ErrorLog "|/usr/sbin/rotatelog ...

最新文章

  1. Kotlin的解析(下)
  2. BZOJ3992[SDOI2015]序列统计
  3. ICMP Internet控制报文协议(四)
  4. python换行输入数据_python将回车作为输入内容的实例
  5. 进入保护模式(三)内存的分页
  6. Myeclipse下使用Maven搭建spring boot项目(第二篇)
  7. 电动自动吞吐式IC卡RFID读写器EMV模块HX150进卡命令
  8. 数据库理论:计算机数据库技术在信息管理中的应用分析
  9. Win10为什么电脑没有本地组策略编辑器
  10. 软件工程--软件详细设计说明书(免费小说网站)
  11. nbiot和2g_nb-iot和4G谁才是物联网未来的趋势?不同行业应该如何选择?
  12. 贪吃蛇java游戏代码_java实现贪吃蛇游戏代码(附完整源码)
  13. Android程序员该如何进阶?,2021Android面经
  14. 从「雄狮」到「瑶光」,奇瑞历史突破背后的十字路口
  15. 在Ubuntu 18.04 Bionic Beaver上安装Wine
  16. python生成渐变颜色数组
  17. 使用v-show不起作用的原因有哪些-
  18. 百度竞价广告账户的设置
  19. Delete Nodes And Return Forest(C++删点成林)
  20. windows环境jenkins安装 自动编译 publish over ssh 远程发布.netcore webapi 服务化.netcore webapi

热门文章

  1. python数据分析教程百度云资源-【python数据分析+pdf】百度云下载 - 云盘精灵
  2. 分享几套Easypanel用户后台模板源码优化版
  3. (github附源码)毕设微信小程序二手书交易后台PHP微擎
  4. 堕落了!经典软件下载网站被查
  5. MySQL小数数据类型
  6. 风能设备物流的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  7. 所有浏览器主页都变成hao123,hao123劫持浏览器(亲测有效)
  8. CityEngine学习资料——split分割
  9. python实现程序自动运行的库_python tkiner实现自动打包程序
  10. 2017兰州高中计算机考试时间,兰州2017年中考考试时间安排