如何使用AngularJS获取重定向响应的标题 [英] How to get headers of the redirect response using AngularJS

查看:300
本文介绍了如何使用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:

    1. User presses "Login" button
    2. User is being redirected to the page which contains code received from OAuth2 server
    3. Request is being sent to OAuth2 server to obtain access_token
    4. As soon as access_token is received user is being redirected back to initial page and a special header Header["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屋!

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