简体   繁体   English

Angular Service Worker 错误拦截 http 重定向 (302)

[英]Angular Service worker incorrectly intercepting http redirects (302)

In my project, i am using Angular and Spring boot.在我的项目中,我使用 Angular 和 Spring 引导。 My service worker is generating by Angular when i build my project.当我构建我的项目时,我的服务人员是由 Angular 生成的。

I use external service (auth2) and my back-end redirecting to login page when session times out.当 session 超时时,我使用外部服务(auth2)和我的后端重定向到登录页面。 This works perfectly fine without the service worker enabled, however with the service worker this fails.这在没有启用服务工作者的情况下工作得很好,但是使用服务工作者会失败。

To my observation, the mode of the Request is changed from navigate to cors.据我观察,请求的模式从导航更改为 cors。

Without SW:没有软件:

Sec-Fetch-Dest: document Sec-Fetch-Mode: navigate Sec-Fetch-Dest:文档 Sec-Fetch-Mode:导航

With SW:带软件:

Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Dest:空 Sec-Fetch-Mode:cors

When active service worker, i have this exception:当活跃的服务人员时,我有这个例外:

Access to fetch at 'https://authentication-url.com/...' (redirected from 'https://my-w ebsite.com/file-request.json') from origin 'https://my-website.com' 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.从源“https://my- website.com' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”header。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。

I tried adding ngsw-bypass=true query param to the redirect, but that did not help.我尝试将 ngsw-bypass=true 查询参数添加到重定向,但这没有帮助。

Can you help me?你能帮助我吗? I search on web but i found nothing on this problem with Angular service worker.我在 web 上进行搜索,但在 Angular 服务人员的这个问题上一无所获。

In the back-end try add the 'Access-Control-Allow-Origin' to the cors default headers.在后端尝试将'Access-Control-Allow-Origin'添加到 cors 默认标头。 That means that the server isn't given access to your app.这意味着服务器无权访问您的应用程序。 I had the same problem with angular and django and i added 'Access-Control-Allow-Origin' to the cors defaul headers.我对 angular 和 django 有同样的问题,我在 cors 默认头文件中添加了'Access-Control-Allow-Origin'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM