Refused to display http localhost8080 xxx‘in a frame because it set ‘X-Frame-Options‘ to ‘DENY‘
Refused to display http:// localhost:8080 /xxx’in a frame because it set ‘X-Frame-Options’ to ‘DENY’
原因
Spring Security4默认是将’X-Frame-Options’ 设置为 ‘DENY’
X-Frame-Options HTTP 响应头是用来给浏览器指示允许一个页面可否在 frame , iframe 或者 object 中展现的标记。网站可以使用此功能,来确保自己网站的内容没有被嵌到别人的网站中去,也从而避免了点击劫持 (clickjacking) 的攻击。
解决办法一
直接在配置SecurityConfig在configure(HttpSecurity http)方法中加入以下代码
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//disable frameOptions检验
http.headers().frameOptions().disable();
}
}
解决办法二
在后端端口@Controller中获取response并设置
//拿到response输入设置X-Frame-Options头即可
response.setHeader("X-Frame-Options", "SAMEORIGIN");
解决办法三
这种办法我未进行验证,不过应该效果跟第一种那种,因为用的是前端的方法,所以可能需要在pom.xml导入一个thymeleaf-extras-springsecurity5的依赖
<security:headers>
<security:frame-options policy="SAMEORIGIN"/>
</security:headers>

浙公网安备 33010602011771号