如何使用AngularJS获取重定向响应的标题 [英] How to get headers of the redirect response using AngularJS
问题描述
我试图为内部应用程序实现 OAuth2
身份验证方法。这个过程看起来非常简单:用户按下登录按钮
OAuth2服务器
代码
的页面 OAuth2服务器
即可获得 access_token
access_token
被接收用户正被重定向回初始页面,并在此期间设置了一个特殊的头文件 Header [Access-Token]
重定向身份验证在这种情况下工作正常,我唯一拥有的是我无法读取标题[Access-Token]
在重定向回到初始页面之后。
使用拦截器
我可以拦截从应用程序发送的请求(例如REST API请求)但不是页面加载过程中的那些。
有没有使用Angular解决这个问题的解决方案?
我希望这会对您有所帮助。
- 您可以将用户重定向到originalURL / + access_token ,因为响应将间接发送到服务器上的端点
- 然后,如果您不想公开access_token,可以在cookie / session中设置它的值只需将它从网址中移除即可。
- 警报:如果该令牌包含/或#在路由中使用/和#。因此,如果您的网址包含此类字符,它会将您重定向到默认路线。
I'm trying to implement OAuth2
authentication methods for an internal application. The process looks quite straightforward:
- User presses "Login" button
- User is being redirected to the page which contains
code
received fromOAuth2 server
- Request is being sent to
OAuth2 server
to obtainaccess_token
- As soon as
access_token
is received user is being redirected back to initial page and a special headerHeader["Access-Token"]
is being set during this redirection
Authentication works fine in this case, the only prolem I'm having is that I can't read Header["Access-Token"]
after the redirection back to initial page.
Using interceptors
I'm able to intercept requests which are being sent from the application (e.g. REST API requests) but not the ones during the page load.
Is there any solution using Angular for this problem?
I hope it will be helpful.
- you can redirect user to "originalURL/"+access_token from server as the response will be indirectly sent to endpoint on server
- Then if you don't want to expose the access_token you can set it's value in cookies/session and just remove it from url.
- Alert: if that token contains "/" or "#" please replace it with some keyword while redirection from server because Angular use "/" and "#" in routing. So if your url includes such characters it will redirect you to default route.
这篇关于如何使用AngularJS获取重定向响应的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!