1

Starting with Spring 3.2 I can simply add this to my security xml:

 <security:headers>
    <security:frame-options
            policy="SAMEORIGIN" />
</security:headers>

But this is not supported in Spring version 3.1, any workaround for this without having to upgrade the version?

This is documentation for version 3.1:

http://docs.spring.io/spring-security/site/docs/3.1.3.RELEASE/reference/springsecurity.html

1
  • You can set custom header in Controller response.setHeader("X-Frame-Options", "SAMEORIGIN"); Commented Aug 22, 2017 at 12:07

1 Answer 1

3

I believe XFrameOptionsHeaderWriter implements logic behind this configuration. It was introduced in Spring 3.2, nothing similar exist prior to that version.

If you want to implement this yourself, you can use a simple filter:

public class XFrameOptionsHeaderFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        response.setHeader("X-Frame-Options", "SAMEORIGIN");
        filterChain.doFilter(request, response);
    }

}

You need to create a bean for this class in your application context:

<bean id="xFrameOptionsHeaderFilter" class="your.package.XFrameOptionsHeaderFilter"/>

And then register the filter in your web.xml:

<filter>  
    <filter-name>xFrameOptionsHeaderFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>xFrameOptionsHeaderFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Sign up to request clarification or add additional context in comments.

2 Comments

tnx very nice answer but I cant seem to get it working, I see the bean is loaded but method is not called when I make a request. any ideas what might be missing?
Maybe you declared the bean in the wrong place? You should put it in your app-context, the one that is used in contextConfigLocation, not your servlet context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.