我遇到 CORS 錯(cuò)誤問題。我確實(shí)請(qǐng)求 Google oAuth2,但收到 CORS 錯(cuò)誤:我想獲得 google 身份驗(yàn)證并生成 JWT 令牌。當(dāng)我在不使用客戶端的情況下執(zhí)行此操作時(shí),一切都很好。當(dāng)我發(fā)送角度請(qǐng)求時(shí),這是 CORS 的問題。我允許所有類型的 CORS。為什么我會(huì)收到此錯(cuò)誤?Access to XMLHttpRequest at 'https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=1020159669873-d9r35ssmnejud852bam87d8gqtcj5qf1.apps.googleusercontent.com&scope=openid%20profile%20email&state=8nizHP1X2z9sA8m0vqM4Lzd6VT24R15eSw5flteTywM%3D&redirect_uri=http://localhost:8080/oauth2/callback/google' (redirected from 'http://localhost:8080/oauth2/authorization/google')from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.Cross-Origin Read Blocking (CORB) blocked cross-origin response https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=1020159669873-d9r35ssmnejud852bam87d8gqtcj5qf1.apps.googleusercontent.com&scope=openid%20profile%20email&state=8nizHP1X2z9sA8m0vqM4Lzd6VT24R15eSw5flteTywM%3D&redirect_uri=http://localhost:8080/oauth2/callback/google with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.我的角度請(qǐng)求: googleLogin(): Observable<LoginResponse> { return this.http.get<LoginResponse> (environment.baseUrl + '/oauth2/authorization/google') .pipe(tap(response => { localStorage.setItem('access_token', response.accessToken); })); }//...public onGoogleLogin(): void { this.authService.googleLogin().subscribe(); }//...跨域資源配置: @Override public void addCorsMappings(CorsRegistry registry) { registry .addMapping("/**") .allowedOrigins("*") .allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE") .maxAge(MAX_AGE_SECS); }
1 回答

aluckdog
TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊
您無(wú)法在此示例中獲取令牌,因?yàn)槟枰M(jìn)行實(shí)際的重定向。有幾種方法可以規(guī)避此要求,RFC https://www.rfc-editor.org/rfc/rfc6749#section-1.2中有詳細(xì)說(shuō)明
在彈出窗口中啟動(dòng)授權(quán)流程,并通過瀏覽器中提供的 API 將服務(wù)器收到的令牌
postMessage()
從彈出窗口傳回 Web 應(yīng)用程序。保存狀態(tài),無(wú)論狀態(tài)是什么,重定向到將啟動(dòng)授權(quán)流程的服務(wù)器,并在將令牌交換為授權(quán)后,使用令牌作為查詢字符串參數(shù)重定向回 Web 應(yīng)用程序。然后使用它并恢復(fù)狀態(tài)。
添加回答
舉報(bào)
0/150
提交
取消