Warm tip: This article is reproduced from serverfault.com, please click

javascript-拒绝在框架中显示...,因为它在 React 和 oAuth 中将“X-Frame-Options”设置为“相同来源”

(javascript - Refused to display...in a frame because it set 'X-Frame-Options' to 'same origin' in React & oAuth)

发布于 2020-08-28 15:54:00

当我使用 oAuth2 进行授权的应用程序在框架内进行授权时,它会引发以下错误:

拒绝在框架中显示...,因为它将“X-Frame-Options”设置为“同源”。

我们在 Reactjs 中使用 oAuth v2。问题是我们有一个基于 Web 的营销 SAAS 产品以及一个在 iframe 中运行的集成应用程序。而这个问题只发生在基于 iframe 的集成应用程序中。有没有办法克服它并以某种方式使其在 iframe 中工作?

Questioner
user12009061
Viewed
11
user9187519 2021-04-01 04:10:46

我们在 Slack 集成方面遇到了类似的问题。不幸的是,没有好的解决方案可以克服这个问题。你唯一能做的就是在 oAuth 处理期间打开新的空白页面。这就是我的意思。

...

     authorizeRedirect(options) {
        window.location = this.getAuthorizeUrl(options)
      },
    
      async authorizeNewWindow(options) {
        const authorizeUrl = this.getAuthorizeUrl(options)
    
        window.open(authorizeUrl, '_blank')
      },
    
      authorize({ type, ...options }) {
        if (type === 'newWindow') {
          return this.authorizeNewWindow(options)
        } else {
          this.authorizeRedirect(options)
        }
      },

假设你已经拥有用于集成的 Auth 服务,因此不会进一步介绍如何创建完整服务。但是你可以根据上述示例执行以下操作:

  • 你可以authorizeRedirect(options)像往常一样在同一页面的主平台中进行授权。
  • authorizeNewWindow(options)在新的空白页中使用单独打开授权页。这个可以用于 iframe。
  • authorize({ type, ...options })你可以在所需的组件或页面中使用的最后一个

当然,它不能提供良好的用户体验,但由于没有其他方法可以克服它,因此它比崩溃的 UI 更好。