如何在 Spring 安全性中禁用 'X-Frame-Options' 响应 header?
How to disable 'X-Frame-Options' response header in Spring Security?
我的 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 安全措施,一切都非常顺利。如何在 spring 安全 xml 文件中禁用它? <http>
标签之间应该写什么
默认情况下 X-Frame-Options
设置为拒绝,以防止 clickjacking 攻击。要覆盖它,您可以将以下内容添加到您的 spring 安全配置
<http>
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
</http>
以下是政策的可用选项
- DENY - 是默认值。有了这个,页面就不能显示在框架中,不管网站试图这样做。
- SAMEORIGIN - 我假设这就是您要查找的内容,以便页面将(并且可以)显示在与页面本身相同来源的框架中
- ALLOW-FROM - 允许您指定起点,页面可以在框架中显示。
有关详细信息,请查看 here。
和 here 检查如何使用 XML 或 Java 配置来配置 headers。
请注意,您可能还需要根据需要指定适当的 strategy
。
如果您使用 Java 配置而不是 XML 配置,请将其放入您的 WebSecurityConfigurerAdapter.configure(HttpSecurity http)
方法中:
http.headers().frameOptions().disable();
您很可能不想完全停用此 Header,而是使用 SAMEORIGIN
。如果您正在使用 Java 配置 (Spring Boot
) 并希望允许 X-Frame-Options: SAMEORIGIN
,那么您需要使用以下内容。
对于较旧的 Spring 安全版本:
http
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
对于较新的版本,例如 Spring Security 4.0.2:
http
.headers()
.frameOptions()
.sameOrigin();
如果使用 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>
如果您正在使用 Spring 引导,禁用 Spring 安全默认值 header 的最简单方法是使用 security.headers.*
属性。特别是,如果您想禁用 X-Frame-Options
默认值 header,只需将以下内容添加到您的 application.properties
:
security.headers.frame=false
您还可以使用 security.headers.cache
、security.headers.content-type
、security.headers.hsts
和 security.headers.xss
属性。有关详细信息,请查看 SecurityProperties
.
如果您正在使用 Spring 安全性的 Java 配置,默认情况下会添加所有默认安全性 headers。可以使用下面的 Java 配置禁用它们:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.headers().disable()
...;
}
}
您应该配置多个 HttpSecurity 实例。
这是我的代码,其中只有 /public/** 请求没有 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/**");
}
}
}
我的 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 安全措施,一切都非常顺利。如何在 spring 安全 xml 文件中禁用它? <http>
标签之间应该写什么
默认情况下 X-Frame-Options
设置为拒绝,以防止 clickjacking 攻击。要覆盖它,您可以将以下内容添加到您的 spring 安全配置
<http>
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
</http>
以下是政策的可用选项
- DENY - 是默认值。有了这个,页面就不能显示在框架中,不管网站试图这样做。
- SAMEORIGIN - 我假设这就是您要查找的内容,以便页面将(并且可以)显示在与页面本身相同来源的框架中
- ALLOW-FROM - 允许您指定起点,页面可以在框架中显示。
有关详细信息,请查看 here。
和 here 检查如何使用 XML 或 Java 配置来配置 headers。
请注意,您可能还需要根据需要指定适当的 strategy
。
如果您使用 Java 配置而不是 XML 配置,请将其放入您的 WebSecurityConfigurerAdapter.configure(HttpSecurity http)
方法中:
http.headers().frameOptions().disable();
您很可能不想完全停用此 Header,而是使用 SAMEORIGIN
。如果您正在使用 Java 配置 (Spring Boot
) 并希望允许 X-Frame-Options: SAMEORIGIN
,那么您需要使用以下内容。
对于较旧的 Spring 安全版本:
http
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
对于较新的版本,例如 Spring Security 4.0.2:
http
.headers()
.frameOptions()
.sameOrigin();
如果使用 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>
如果您正在使用 Spring 引导,禁用 Spring 安全默认值 header 的最简单方法是使用 security.headers.*
属性。特别是,如果您想禁用 X-Frame-Options
默认值 header,只需将以下内容添加到您的 application.properties
:
security.headers.frame=false
您还可以使用 security.headers.cache
、security.headers.content-type
、security.headers.hsts
和 security.headers.xss
属性。有关详细信息,请查看 SecurityProperties
.
如果您正在使用 Spring 安全性的 Java 配置,默认情况下会添加所有默认安全性 headers。可以使用下面的 Java 配置禁用它们:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.headers().disable()
...;
}
}
您应该配置多个 HttpSecurity 实例。
这是我的代码,其中只有 /public/** 请求没有 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/**");
}
}
}