我想在外部服務器上進行身份驗證以獲取一些資料。當我嘗試進行身份驗證時,出現 Cors 錯誤。我也有一個 proxy.conf.json 但它似乎不起作用
代碼:
authUrl = "http://localhost:4200/auth"
tokenUrl = "http://localhost:4200/tokenUrl"
redirect_uri = "http://localhost:4200"
auth(clientId: string, secredId: string) {
const url = this.authUrl '?client_id=' clientId '&response_type=code&scope=' this.scope '&state=' this.state '&redirect_uri=' this.redirect_uri
this.http.get<void>(url, this.httpOptions).subscribe(() => {
const code = this.route.snapshot.paramMap.get('code');
// go on
});
配置:
{
"/auth": {
"target": "https://www.EXTERNAL_WWW_APP.com/oauth2/authorize",
"secure": false,
"changeOrigin": true,
"pathRewrite": {
"^/auth": ""
}
},
"/tokenUrl": {
"target": "https://www.EXTERNAL_WWW_APP.com/oauth2/token",
"secure": false,
"changeOrigin": true,
"pathRewrite": {
"^/tokenUrl": ""
}
}
錯誤:
Access to XMLHttpRequest at 'https://www.EXTERNAL_WWW_APP.com/login?return=34sdf34AasdIZU' (redirected from 'http://localhost:4200/auth?client_id=1&response_type=code&scope=read&state=aakjsdh&redirect_uri=http://localhost:4200') 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.
GET https://www.EXTERNAL_WWW_APP.com/login?return=c3f456tij47fztMJTZM net::ERR_FAILED
我究竟做錯了什么?
uj5u.com熱心網友回復:
您的問題出在后端,而不是出在 Angular 中。修復取決于您的后端解決方案。例如,在 .NET 6 Web 應用程式中,您需要在 Program.cs 中包含以下內容:
builder.Services.AddCors(options =>
{
var frontEndUrl = "your frontend url here";
var allowedOrigins = new[] { frontEndUrl };
options.AddDefaultPolicy(builder =>
{
builder.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod();
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365768.html
上一篇:自定義Angular編譯器錯誤
下一篇:Angular5版本升級
