首頁  >  文章  >  後端開發  >  重定向後 Golang 與 Gin 中的 CORS 錯誤

重定向後 Golang 與 Gin 中的 CORS 錯誤

WBOY
WBOY轉載
2024-02-09 20:21:33408瀏覽

重定向后 Golang 与 Gin 中的 CORS 错误

php小編小新在此為大家介紹重定向後Golang與Gin中的CORS錯誤。 CORS(跨來源資源共享)是一種用於在不同領域之間進行安全資料傳輸的機制,然而,在使用Golang和Gin框架時,遇到CORS錯誤是常見的問題。本文將詳細解釋CORS錯誤的原因和解決方法,幫助開發者更好地理解和處理這個問題。無論您是初學者還是有經驗的開發者,本文都能為您提供有用的指導和解決方案。讓我們一起來探索Golang和Gin中的CORS錯誤吧!

問題內容

我正在嘗試在用 go 和 gin 編寫的網頁伺服器中實作 google oauth2。我新增了兩個新端點,名稱為 /google/sign-in 和 /google/callback。第一個接收請求並重定向到 google auth url,第二個在使用者選擇有效的 google 帳戶、驗證令牌並為我的內部驗證建立 jwt 後呼叫。

一切都很好,但事實並非如此,因為當我呼叫第一個 api 路由時,我收到了 cors 錯誤:

access to xmlhttprequest at 'https://accounts.google.com/o/oauth2/auth?access_type=online&client_id=xxxxxxxxxxxxx-337ka657nqlo84q6697vv2efsc2vqvm0.apps.googleusercontent.com&redirect_uri=http%3a%2f%2flocalhost%3a3000%2fgoogle%2fcallback&response_type=code&scope=https%3a%2f%2fwww.googleapis.com%2fauth%2fuserinfo.email+https%3a%2f%2fwww.googleapis.com%2fauth%2fuserinfo.profile&state=7e5f86fe352b4563c7d1bd62408285dcbc44e3e26a4f142bbae915279008ece6' (redirected from 'http://localhost:3000/google/sign-in') from origin 'http://localhost:4200' has been blocked by cors policy: response to preflight request doesn't pass access control check: no 'access-control-allow-origin' header is present on the requested resource.

這是我的 golang 程式碼:

r := gin.default()

r.use(cors.new(cors.config{
    alloworigins: []string{"*"},
    allowmethods: []string{"get", "post", "put", "delete", "patch", "options"},
    allowheaders: []string{"origin", "authorization", "content-type", "content-length", "accept-encoding", "x-csrf-token", "baggage", "sentry-trace", "x-user-lang"},
}))

r.post("/google/sign-in", authcontroller.redirecttogoogleauthpage)
r.get("/google/callback", authcontroller.googlesignin)

身份驗證控制器

func (a AuthController) RedirectToGoogleAuthPage(c *gin.Context) {
  googleAuthConfig := utils.GetGoogleAuthConfig()
  state := utils.GenerateRandomKey()
  url := googleAuthConfig.AuthCodeURL(state, oauth2.AccessTypeOnline)
  session := sessions.Default(c)
  session.Set(state, state)
  err := session.Save()
  if err != nil {
      c.JSON(http.StatusInternalServerError, a.Errors.InternalError(err.Error()))
      return
  }
  c.Header("X-Auth-State", state)
  c.Redirect(http.StatusTemporaryRedirect, url)
}

在 googleauthconfig 中,回呼 url 為 http://localhost:3000/google/callback,它已加入 google cloud oauth 憑證中。

我知道我錯過了回調請求中的 access-control-allow-origin,但如何添加該標頭?

解決方法

根據問題中的信息,您正在訪問http://localhost:4200頁面,並向http://localhost:3000 /google/sign-in發送AJAX請求,該請求將重定向到https://accounts.google.com/o/oauth2/auth。這行不通。您需要將頁面重新導向至 https://accounts.google.com/o/oauth2/auth

有兩種選擇可以解決此問題:

  • 修改客戶端程式碼以將AJAX 請求替換為表單請求(使用076c157479602e1f1ab08a1598864165 元素)。在這種情況下,RedirectToGoogleAuthPage 中的 c.JSON 應替換為其他內容。

  • 或修改RedirectToGoogleAuthPage 以使用包含要重定向到的目標URL 的JSON 內容進行回應,並修改客戶端程式碼以將頁面重定向到目標URL(使用window .location = targetURL)。

看起來第二個選擇需要對程式碼進行較少的更改。

以上是重定向後 Golang 與 Gin 中的 CORS 錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除