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>
posted @   鸭梨的药丸哥  阅读(64)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示