#angular #oauth-2.0 #cors
Вопрос:
Я хочу пройти аутентификацию на внешнем сервере, чтобы получить некоторые данные. Когда я пытаюсь пройти аутентификацию, я получаю ошибку Cors. У меня также есть прокси.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 'amp;response_type=codeamp;scope=' this.scope 'amp;state=' this.state 'amp;redirect_uri=' this.redirect_uri this.http.getlt;voidgt;(url, this.httpOptions).subscribe(() =gt; { 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=1amp;response_type=codeamp;scope=readamp;state=aakjsdhamp;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
Что я делаю не так?
Ответ №1:
Ваша проблема в бэкэнде, а не в угловом. Исправление зависит от вашего внутреннего решения. Например, в веб-приложении .NET 6 вам понадобится что-то подобное в вашей программе.cs:
builder.Services.AddCors(options =gt; { var frontEndUrl = "your frontend url here"; var allowedOrigins = new[] { frontEndUrl }; options.AddDefaultPolicy(builder =gt; { builder.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod(); }); });
Комментарии:
1. Итак, если у меня нет прав изменять его на внешнем сервере, могу ли я забыть? авторизация работает с моим бэкендом django