响应重定向(302)在角度中不起作用 [英] response redirection(302) not work in angular

查看:52
本文介绍了响应重定向(302)在角度中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过angular设计了SSO(单点登录)的登录页面,并通过spring boot框架分别开发了登录服务.

I Designed a login page for SSO(Single Sign-On) by angular and the services for login developed separately by spring boot framework.

在有角度的应用程序中,提交登录表单后,将通过HttpClient模块使用post-HttpMethod调用登录服务.在登录服务中,如果用户名和密码正确,则在登录服务的结果中,将响应状态设置为302,并在响应的标头中添加特定的URL.在这种情况下,我希望浏览器重定向到特定的URL,但这没有发生.

In the angular app, when submitted login form, login service call with post-HttpMethod by HttpClient module. In login service if username and password be correct then in the result of login service, set response status to 302 and add a specific URL to the header of the response. in this scenario I expect to browser redirect to specific URL, but this is not happening.

我在Firefox中跟踪请求和响应,与所附图片相同.

I trace request and response in Firefox as the same as the attached picture .

  1. 发送登录后请求,响应为302,标头位置是服务器响应中设置的特定URL
  2. 浏览器将选项请求发送到响应标头的位置URL,并获得200个结果
  3. 浏览器将获取请求发送到目标URL(标头位置是特定的URL),此时浏览器未打开目标URL,正义响应为200.


我更改了响应的Location标头,发现重定向没有完成(只是登录请求和选项请求完成了),下图是请求跟踪.


I change the Location header of response, and saw that the redirection is not doing(just login request and option request done), below picture is request trace.

推荐答案

我怀疑您的请求是XHR请求.如果是这样,浏览器将在处理程序中跟踪它们,但不会将整个页面导航到重定向位置.

I suspect your requests are XHR requests. If so, the browser follows them in the handler but won't navigate the full page to the redirect location.

如果您想模拟发送整页请求重定向时发生的情况,则可能需要更改后端,以使其在有效负载中返回带有原始重定向目标位置的200,然后重新加载页面到该位置(类似于 window.location = payload.redirectLocation ).

If you want to emulate what happens when a redirect is sent for a full page request, you may want to change your backend so that it returns a 200 with the target location of the original redirect in the payload, and then reload the page to that location (something like window.location = payload.redirectLocation).

您还可以摆脱重定向,根据身份验证成功让后端返回200或401,然后使用角度路由器导航.

You can also get rid of the redirect, let the backend return 200 or 401 depending on the authentication success, then navigate with the angular router.

这篇关于响应重定向(302)在角度中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆