如何在Spring Security中禁用“X-Frame-Options”响应头?

时间:2015-02-21 14:54:43

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

我的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>标签之间写什么

7 个答案:

答案 0 :(得分:94)

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

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

以下是政策的可用选项

  • DENY - 是默认值。使用此页面无论站点是否尝试这样做,页面都无法显示在框架中。
  • SAMEORIGIN - 我认为这正是您所寻找的,因此该页面将(并且可以)显示在与页面本身相同的原始框架中
  • 允许来自 - 允许您指定原点,页面可以在框架中显示。

有关详细信息,请查看here

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

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

答案 1 :(得分:68)

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

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

答案 2 :(得分:45)

您很可能不想完全停用此标头,但请使用SAMEORIGIN。如果您正在使用Java Configs(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();

答案 3 :(得分:15)

如果使用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>

答案 4 :(得分:9)

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

security.headers.frame=false

您还可以使用security.headers.cachesecurity.headers.content-typesecurity.headers.hstssecurity.headers.xss属性。有关详细信息,请查看SecurityProperties

答案 5 :(得分:6)

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

String tokenPOS = token.get(PartOfSpeechAnnotation.class);

答案 6 :(得分:0)

您应该配置多个 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/**");
    }
  }
}