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>
标签之间应该写什么?
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'.
<http>
标签之间应该写什么?如果您使用Java配置而不是XML配置,请将以下内容放入WebSecurityConfigurerAdapter.configure(HttpSecurity http)
方法中:
http.headers().frameOptions().disable();
http.headers().frameOptions().sameOrigin();
。 - Ian Newland默认情况下X-Frame-Options
被设置为denied,以防止点击劫持攻击。要覆盖此设置,您可以将以下内容添加到您的Spring安全配置中
<http>
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
</http>
以下是策略可用选项:
如需更多信息,请查看这里。
同时,你可以通过XML或Java配置来设置头文件,这里有详细的配置方法。
请注意,您可能还需要根据需求指定适当的strategy
。
http
和 headers
标签的命名空间是什么? - Pasupathi Rajamanickamhttp.headers().frameOptions().sameOrigin();
。 - joninx很可能您不想完全停用此标题,而是使用SAMEORIGIN
。 如果您正在使用Java配置(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();
如果使用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 Security的Java配置,则默认情况下会添加所有默认的安全标头。以下Java配置可以用于禁用它们:
如果您正在使用Spring Security的Java配置,则默认情况下会添加所有默认的安全标头。以下Java配置可以用于禁用它们:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.headers().disable()
...;
}
}
如果您正在使用Spring Boot,禁用Spring Security默认标头的最简单方法是使用security.headers.*
属性。特别是,如果您想禁用X-Frame-Options
默认标头,只需将以下内容添加到您的application.properties
:
security.headers.frame=false
还有security.headers.cache
、security.headers.content-type
、security.headers.hsts
和security.headers.xss
属性可供使用。更多信息请参考SecurityProperties
。
您应该配置多个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/**");
}
}
}
antMatcher(String)
时,如何防止覆盖现有规则? - Joaquín L. Robles.csrf().disable()
太危险了。
测试:
.headers().frameOptions().sameOrigin()