How to disable 'X-Frame-Options' response header in Spring Security?
Asked Answered
W

9

118

I have CKeditor on my JSP and whenever I upload something, the following error pops out:

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

I have tried removing Spring Security and everything works like a charm.

  • How can I disable this in Spring Security XML file?
  • What should I write between <http> tags?
Wend answered 21/2, 2015 at 14:54 Comment(1)
None of the answers below yet address whether it's possible to apply SAMEORIGIN or ALLOW at the controller method level - anyone know?Postglacial
O
126

By default X-Frame-Options is set to denied, to prevent clickjacking attacks. To override this, you can add the following into your spring security config

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

Here are available options for policy

  • DENY - is a default value. With this the page cannot be displayed in a frame, regardless of the site attempting to do so.
  • SAMEORIGIN - I assume this is what you are looking for, so that the page will be (and can be) displayed in a frame on the same origin as the page itself
  • ALLOW-FROM - Allows you to specify an origin, where the page can be displayed in a frame.

For more information take a look here.

And here to check how you can configure the headers using either XML or Java configs.

Note, that you might need also to specify appropriate strategy, based on needs.

Omeara answered 21/2, 2015 at 17:57 Comment(6)
What is the namespace for this http and headers tags?Precautious
Is it possible to apply this as the controller method level?Janaye
If you need to configure it within WebSecurityConfigurerAdapter's configure method, write the following code: http.headers().frameOptions().sameOrigin();Chiekochien
@Omeara I use spring 3.1 and this is not supported, any workaround you might suggest?Boyish
@Boyish docs.spring.io/spring-security/site/docs/current/reference/html/… it is supported. Could you please share what you have tried and didn't work?Omeara
When I have applied xrfameoptions in spring security XML, it is set on all response headers except for login page. I wonder what went wrong?!Gallonage
F
140

If you're using Java configs instead of XML configs, put this in your WebSecurityConfigurerAdapter.configure(HttpSecurity http) method:

http.headers().frameOptions().disable();
Fourposter answered 10/4, 2015 at 19:53 Comment(3)
Using disable() is an option but if it's on the same server, use http.headers().frameOptions().sameOrigin();Arriola
what if i mix :-)Noxious
.headers(headers -> headers .frameOptions(FrameOptionsConfig::disable))Interrupter
O
126

By default X-Frame-Options is set to denied, to prevent clickjacking attacks. To override this, you can add the following into your spring security config

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

Here are available options for policy

  • DENY - is a default value. With this the page cannot be displayed in a frame, regardless of the site attempting to do so.
  • SAMEORIGIN - I assume this is what you are looking for, so that the page will be (and can be) displayed in a frame on the same origin as the page itself
  • ALLOW-FROM - Allows you to specify an origin, where the page can be displayed in a frame.

For more information take a look here.

And here to check how you can configure the headers using either XML or Java configs.

Note, that you might need also to specify appropriate strategy, based on needs.

Omeara answered 21/2, 2015 at 17:57 Comment(6)
What is the namespace for this http and headers tags?Precautious
Is it possible to apply this as the controller method level?Janaye
If you need to configure it within WebSecurityConfigurerAdapter's configure method, write the following code: http.headers().frameOptions().sameOrigin();Chiekochien
@Omeara I use spring 3.1 and this is not supported, any workaround you might suggest?Boyish
@Boyish docs.spring.io/spring-security/site/docs/current/reference/html/… it is supported. Could you please share what you have tried and didn't work?Omeara
When I have applied xrfameoptions in spring security XML, it is set on all response headers except for login page. I wonder what went wrong?!Gallonage
T
73

Most likely you don't want to deactivate this Header completely, but use SAMEORIGIN. If you are using the Java Configs (Spring Boot) and would like to allow the X-Frame-Options: SAMEORIGIN, then you would need to use the following.


For older Spring Security versions:

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

For newer versions like Spring Security 4.0.2:

http
   .headers()
      .frameOptions()
         .sameOrigin();
Transformation answered 4/8, 2015 at 14:2 Comment(3)
How to configure this in Spring 3.2.12?Precautious
Migrating from 3.X to 4.X and ran into this as it was just appending based on the first example. thx.Gratulation
Thanks. http.headers().frameOptions().sameorigin(); worked for me.Thermonuclear
D
22

If using XML configuration you can use

<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>
Derwood answered 14/4, 2016 at 17:15 Comment(0)
U
14

If you are using Spring Security's Java configuration, all of the default security headers are added by default. They can be disabled using the Java configuration below:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
   WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .headers().disable()
      ...;
  }
}
Unvoiced answered 30/9, 2016 at 10:32 Comment(0)
S
9

If you're using Spring Boot, the simplest way to disable the Spring Security default headers is to use security.headers.* properties. In particular, if you want to disable the X-Frame-Options default header, just add the following to your application.properties:

security.headers.frame=false

There is also security.headers.cache, security.headers.content-type, security.headers.hsts and security.headers.xss properties that you can use. For more information, take a look at SecurityProperties.

Salter answered 1/7, 2016 at 18:56 Comment(1)
In Spring Boot 2.x this method is deprecated. "The security auto-configuration is no longer customizable. Provide your own WebSecurityConfigurer bean instead."Cauterize
E
2

You should configure multiple HttpSecurity instances.

Here is my code where only /public/** requests are without X-Frame-Options header.

@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/**");
    }
  }
}
Eccrine answered 11/3, 2021 at 10:36 Comment(1)
how do you prevent overriding existing rules when calling antMatcher(String)?Rebellious
F
0

.csrf().disable() its to dangerous.

test:

.headers().frameOptions().sameOrigin()
Frederickafredericks answered 7/10, 2022 at 14:41 Comment(0)
Z
0

with lambda_dsl (spring security 6.2 and prepare for 7.x)

http.headers(header.frameOptions(frameOptions -> Customizer.withDefaults());)

http.headers(header->{header.frameOptions(FrameOptionsConfig::sameOrigin);})

http.headers(header->{header.frameOptions(FrameOptionsConfig::disable);})
Zehe answered 15/2 at 9:24 Comment(1)
Perhaps you could edit your answer and provide some explanation as to how these lambdas solve the issue described in the posted question?Idola

© 2022 - 2024 — McMap. All rights reserved.