我有两个Web应用程序:一个是Web应用程序(web-app),另一个是报表Web。我想在<iframe>
中嵌入报表Web,但是浏览器显示以下错误:
X-Frame-Options: DENY
是否有任何帮助?
我有两个Web应用程序:一个是Web应用程序(web-app),另一个是报表Web。我想在<iframe>
中嵌入报表Web,但是浏览器显示以下错误:
X-Frame-Options: DENY
是否有任何帮助?
X-Frame-Options的值可以为DENY(默认),SAMEORIGIN和ALLOW-FROM uri。根据Spring Security文档,您可以告诉Spring添加自定义头来覆盖默认行为:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(new WhiteListedAllowFromStrategy(Arrays.asList("www.yourhostname.com"))))
...
}
春天将添加X-Frame-Options: ALLOW-FROM ... 或者
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
对于X-Frame-Options:SAMEORIGIN,或者可以通过完全禁用来关闭它
http.headers().frameOptions().disable()
编辑(2020年6月)- X-Frame选项已过时:
"frame-ancestors指令取代了X-Frame-Options头文件。如果一个资源同时具有这两个策略,应该执行frame-ancestors策略并忽略X-Frame-Options策略。"
https://www.w3.org/TR/CSP2/#frame-ancestors-and-frame-options
所以请考虑使用content-security-policy
:
<headers>
<content-security-policy policy-directives="frame-ancestors 'self'"/>
</headers>
<http>
<!-- ... -->
<headers>
<frame-options policy="SAMEORIGIN" />
</headers>
</http>
Java配置:
@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// ...
.headers().frameOptions().sameOrigin();
}
}
禁用配置
您也可以直接禁用它,但要意识到安全风险。
http.headers().frameOptions().disable();
背景信息
在Spring Security 3.2.0中引入了安全 Headers,但默认情况下被禁用:
http://spring.io/blog/2013/08/23/spring-security-3-2-0-rc1-highlights-security-headers/
在Spring Security 4.x中,默认启用这些头部(对于IFrames:X-Frame-Options: DENY):
"Spring Security 4.x已更改Java配置和XML配置以要求明确禁用默认设置。"
http://docs.spring.io/spring-security/site/migrate/current/3-to-4/html5/migrate-3-to-4-jc.html#m3to4-header
HttpSecurity http
对象的配置。 - flavio.donzeconfigure()
配置没有被调用? - flavio.donze.and()
的副本。 - Erica Kane你可以使用
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
在您的安全应用程序上下文XML中的<http>
配置内部