Spring Security:Refused to display 'http://**' in a frame because it set 'X-Frame-Options' to 'deny'

在整合Spring Security时,页面的iframe出现这个错误:

Refused to display 'http://**' in a frame because it set 'X-Frame-Options' to 'deny'

解决:

在继承WebSecurityConfigurerAdapter的子类的覆盖方法configure(HttpSecurity)里面添加:

http.headers().frameOptions().sameOrigin()

frameOptions()会返回一个HeadersConfigurer对象,看它的类注释:

 * <p>
 * Adds the Security HTTP headers to the response. Security HTTP headers is activated by
 * default when using {@link WebSecurityConfigurerAdapter}'s default constructor.
 * </p>
 *
 * <p>
 * The
default headers include are:
 * </p>
 *
 * <pre>
 * Cache-Control: no-cache, no-store, max-age=0, must-revalidate
 * Pragma: no-cache
 * Expires: 0
 * X-Content-Type-Options: nosniff
 * Strict-Transport-Security: max-age=31536000 ; includeSubDomains
 * X-Frame-Options:
DENY
 * X-XSS-Protection: 1; mode=block
 * </pre>

从中可以得知默认的iframe加载是DENY,导致了页面上出现错误。

 

sameOrigin()的注释是这样子的:

/**

* <p>

* Specify to allow any request that comes from the same origin to frame this

* application. For example, if the application was hosted on example.com, then

* example.com could frame the application, but evil.com could not frame the

* application.

* </p>

*

* @return

*/

 

从注释中我们知道sameOrigin()方法表示允许同源请求加载iframe。

 

也可以添加以下内容来实现:

http.headers().frameOptions().disable()

注释:

/**
* Prevents the header from being added to the response.
*
* @return the {@link HeadersConfigurer} for additional configuration.
*/

这样就相当于把默认要添加到响应头信息中的内容全阻止、禁用掉了。

X-Frame-Options是一个HTTP响应头,用于指示浏览器是否允许将页面嵌入到frame、iframe或object中展示。当一个页面设置了X-Frame-Optionsdeny时,浏览器会拒绝在frame中展示该页面,即使是在相同域名的页面中也不允许。\[2\] 这个设置可以用来防止点击劫持攻击,即将一个网站的内容嵌入到另一个恶意网站中,以欺骗用户进行操作。\[3\] 如果你遇到了"Refused to display xxx in a frame because it set X-Frame-Options to deny"的错误,说明该页面设置了X-Frame-Optionsdeny,所以无法在frame中展示。这是一种安全措施,以保护网站的内容不被恶意利用。\[1\] 在开发中,你可以通过设置响应头的X-Frame-Options值来解决这个问题。例如,可以将X-Frame-Options设置为SAMEORIGIN,这样页面就可以在同域名页面的frame中嵌套展示。\[2\] #### 引用[.reference_title] - *1* [Refused to display 'xxx' in a frame because it set 'X-Frame-Options' to 'deny'.](https://blog.csdn.net/Absorbed66c/article/details/100914969)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [解决 Refused to display in a frame because it set 'X-Frame-Options' to 'deny'.问题](https://blog.csdn.net/lizy928/article/details/82535089)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值