Configuring and Disabling X-Frame-Options Response Header in Spring Security

Nov 28, 2025 · Programming · 28 views · 7.8

Keywords: Spring Security | X-Frame-Options | CKEditor | Clickjacking Protection | XML Configuration | Java Configuration

Abstract: This technical article provides a comprehensive analysis of the X-Frame-Options response header mechanism in Spring Security. Through examining the frame refusal issues encountered during CKEditor file uploads, it systematically explains how to adjust X-Frame-Options policies in both XML and Java configurations, including complete disablement, SAMEORIGIN, and ALLOW-FROM options. The article integrates Spring Security official documentation to deeply analyze security implications and applicable scenarios, offering developers complete technical solutions.

Problem Background and Error Analysis

When using CKEditor for file uploads, developers frequently encounter the following error message:

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

This error indicates that the browser refused to display the specified URL within a frame because the server's response header contained X-Frame-Options: DENY. When Spring Security configuration is removed, functionality returns to normal, confirming that the issue originates from Spring Security's default security settings.

X-Frame-Options Security Mechanism Analysis

The X-Frame-Options is a crucial security field in HTTP response headers, primarily designed to prevent clickjacking attacks. Clickjacking is a malicious technique where attackers overlay transparent iframes on legitimate web pages to trick users into performing sensitive operations unknowingly.

Spring Security enables this protection mechanism by default, with DENY as the default value, meaning the page cannot be displayed in any frame. While this conservative security strategy effectively prevents attacks, it causes compatibility issues in legitimate frame usage scenarios.

XML Configuration Solution

For applications using Spring Security XML configuration, X-Frame-Options policy can be adjusted by adding header configuration within the <http> tags:

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

This configuration changes the X-Frame-Options value to SAMEORIGIN, allowing the page to be displayed in frames that share the same origin as the page itself. For CKEditor upload functionality, this is typically the most appropriate solution as it permits normal operation of iframes within the same domain.

Policy Options Detailed Explanation

X-Frame-Options supports three main policy options:

In CKEditor upload scenarios, SAMEORIGIN is the optimal choice as it maintains basic security protection while allowing proper functioning of frames within the same application.

Java Configuration Alternative

For Spring applications using Java configuration, particularly Spring Boot projects, equivalent functionality can be achieved by overriding the configure method in WebSecurityConfigurerAdapter:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.headers().frameOptions().sameOrigin();
}

This approach provides equivalent functionality to XML configuration but offers better type safety and IDE support. For newer versions of Spring Security, this is the recommended practice.

Complete Disablement Option

In specific circumstances where complete disablement of X-Frame-Options protection is necessary, the following configuration can be used:

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

Or the corresponding XML configuration:

<http>
    <headers>
        <frame-options disabled="true"/>
    </headers>
</http>

However, it's important to note that completely disabling this protection significantly reduces application security and should be used cautiously.

Security Considerations and Best Practices

When adjusting X-Frame-Options configuration, security and functionality requirements must be balanced:

Spring Security's header protection mechanism is a comprehensive security framework that includes various security headers beyond X-Frame-Options, such as cache control, content type options, and HSTS. Proper configuration of these headers is crucial for building secure web applications.

Version Compatibility Notes

Different versions of Spring Security may have variations in configuration syntax:

Developers should select the appropriate configuration method based on their actual Spring Security version and refer to official documentation for the latest configuration guidelines.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.