【问题标题】:How to disable 'X-Frame-Options' response header in Spring Security?如何在 Spring Security 中禁用“X-Frame-Options”响应标头?
【发布时间】:2015-04-23 04:53:21
【问题描述】:

我的jsp上有CKeditor,每当我上传东西时,都会弹出以下错误:

 Refused to display 'http://localhost:8080/xxx/xxx/upload-image?CKEditor=text&CKEditorFuncNum=1&langCode=ru' in a frame because it set 'X-Frame-Options' to 'DENY'.

我已经尝试删除 Spring Security,一切都像一个魅力。如何在 spring security xml 文件中禁用它? <http>标签之间应该写什么

【问题讨论】:

  • 以下答案均未解决是否可以在控制器方法级别应用 SAMEORIGIN 或 ALLOW - 有人知道吗?

标签: java spring spring-security spring-boot x-frame-options


【解决方案1】:

默认情况下X-Frame-Options 设置为拒绝,以防止clickjacking 攻击。要覆盖它,您可以将以下内容添加到您的 spring 安全配置中

<http>    
    <headers>
        <frame-options policy="SAMEORIGIN"/>
    </headers>
</http>

这里有可用的政策选项

  • DENY - 是默认值。有了这个,页面就不能显示在一个框架中,不管网站试图这样做。
  • SAMEORIGIN - 我假设这就是您要查找的内容,以便页面将(并且可以)显示在与页面本身相同来源的框架中
  • ALLOW-FROM - 允许您指定页面可以在框架中显示的来源。

欲了解更多信息,请查看here。

和here 检查如何使用 XML 或 Java 配置来配置标头。

请注意,您可能还需要根据需要指定适当的strategy。

【讨论】:

  • http 和 headers 标签的命名空间是什么?
  • 是否可以将此作为控制器方法级别应用?
  • 如果需要在WebSecurityConfigurerAdapter的configure方法中配置,写如下代码:http.headers().frameOptions().sameOrigin();
  • @vtor 我使用的是 spring 3.1,这不受支持,您可能会建议任何解决方法吗?
  • @Spring docs.spring.io/spring-security/site/docs/current/reference/html/… 支持。您能分享一下您尝试过但没有奏效的方法吗?
【解决方案2】:

如果您使用的是 Java 配置而不是 XML 配置,请将其放入您的 WebSecurityConfigurerAdapter.configure(HttpSecurity http) 方法中:

http.headers().frameOptions().disable();

【讨论】:

  • 使用 disable() 是一个选项,但如果它在同一台服务器上,请使用 http.headers().frameOptions().sameOrigin();
  • 如果我混合 :-)
【解决方案3】:

您很可能不想完全停用此标头,而是使用SAMEORIGIN。如果您正在使用 Java 配置 (Spring Boot) 并希望允许 X-Frame-Options:SAMEORIGIN,那么您需要使用以下内容。


对于较旧的 Spring Security 版本:

http
   .headers()
       .addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))

对于像Spring Security 4.0.2这样的新版本:

http
   .headers()
      .frameOptions()
         .sameOrigin();

【讨论】:

  • Spring 3.2.12如何配置这个?
  • 从 3.X 迁移到 4.X 并遇到了这个问题,因为它只是基于第一个示例进行附加。谢谢。
  • 谢谢。 http.headers().frameOptions().sameorigin();为我工作。
【解决方案4】:

如果使用 XML 配置,您可以使用

<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:security="http://www.springframework.org/schema/security"> 
<security:http>
    <security:headers>
         <security:frame-options disabled="true"></security:frame-options>
    </security:headers>
</security:http>
</beans>

【讨论】:

    【解决方案5】:

    如果您使用 Spring Security 的 Java 配置,则默认添加所有默认安全标头。可以使用下面的 Java 配置禁用它们:

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

    【讨论】:

      【解决方案6】:

      如果您使用的是 Spring Boot,禁用 Spring Security 默认标头的最简单方法是使用 security.headers.* 属性。特别是,如果您想禁用X-Frame-Options 默认标头,只需将以下内容添加到您的application.properties:

      security.headers.frame=false
      

      您还可以使用security.headers.cache、security.headers.content-type、security.headers.hsts 和security.headers.xss 属性。欲了解更多信息,请查看SecurityProperties。

      【讨论】:

      • 在 Spring Boot 2.x 中不推荐使用此方法。 “安全自动配置不再可定制。请提供您自己的 WebSecurityConfigurer bean。”
      【解决方案7】:

      您应该配置多个 HttpSecurity 实例。

      这是我的代码,其中只有 /public/** 请求没有 X-Frame-Options标题。

      @Configuration
      public class SecurityConfig {
      
      /**
       * Public part - Embeddable Web Plugin
       */
      
      @Configuration
      @Order(1)
      public static class EmbeddableWebPluginSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
          protected void configure(HttpSecurity http) throws Exception {
              // Disable X-Frame-Option Header
              http.antMatcher("/public/**").headers().frameOptions().disable();
          }
      }
      
      /**
       * Private part - Web App Paths
       */
      
      @Configuration
      @EnableOAuth2Sso
      public static class SSOWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
          @Override
          protected void configure(HttpSecurity http) throws Exception {
      
              http
                      .csrf().disable()
                      .antMatcher("/**")
                      .authorizeRequests()
                      .antMatchers("/public/**", "/", "/login**", "/webjars/**", "/error**", "/static/**", "/robots", "/robot", "/robot.txt", "/robots.txt")
                      .permitAll()
                      .anyRequest()
                      .authenticated()
                      .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                      .logoutSuccessUrl("/bye");
          }
      
          /**
           * Public API endpoints
           */
      
          @Override
          public void configure(WebSecurity web) throws Exception {
              web.ignoring().antMatchers("/api/**");
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-10-19
        • 1970-01-01
        • 2015-04-15
        • 2017-03-09
        • 2015-01-17
        • 1970-01-01
        • 2012-08-07
        • 2018-09-04
        • 2013-11-16
        相关资源
        最近更新 更多