1 X-Frame-Options HTTP

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

X-Frame-Options三个参数:

1、DENY
表示页面不允许在frame展示,即便是在相同域名页面嵌套也不允许。

2、SAMEORIGIN
表示页面可以相同域名页面frame展示

3、ALLOW-FROM uri
表示页面可以指定来源的frame展示

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

2 应用

2.1 Apache

需要把下面这行添加到 ‘site’ 的配置

Header always append X-Frame-Options SAMEORIGIN

2.2 nginx

需要添加到 ‘http’, ‘server’ 或者 ‘location’ 的配置项中,个人来讲喜欢配置在‘server’ 中
正常情况下都是使用SAMEORIGIN参数,允许同域嵌套

add_header X-Frame-Options SAMEORIGIN;

允许单个域名iframe嵌套
add_header X-Frame-Options ALLOW-FROM http://whsir.com/;

允许多个域名iframe嵌套,注意这里是用逗号分隔
add_header X-Frame-Options "ALLOW-FROM http://whsir.com/,https://cacti.org.cn/";

2.3 IIS

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

<system.webServer>

  ...

  <httpProtocol>

    <customHeaders&gt;

      <add name="X-Frame-Options" value="SAMEORIGIN" /&gt;

    </customHeaders&gt;

  </httpProtocol&gt;

  ...

</system.webServer&gt;

2.4 HAProxy

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

rspadd X-Frame-Options: SAMEORIGIN

2.5 Tomcat

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

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

2.6 EnableWebSecurity

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

spring boot支持EnableWebSecurity 这个anotation来设置不全安全策略。 具体如下
配置后如何确定X-Frame-Options是否生效呢?我这里以Google浏览器为例打开网站按F12键,选择Network,找到对应的Headers,如下图所示

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {
 
    @Override
    protected 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 {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.headers().frameOptions().sameOrigin();
 
    }
}

去除x-frame-options header配置:


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

在这里插入图片描述

原文地址:https://blog.csdn.net/qq_37705525/article/details/125359587

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_31972.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注