提供刷新令牌后重新初始化上一个失败的请求 [英] Re Initaite the last failed request after refresh token is provided

查看:83
本文介绍了提供刷新令牌后重新初始化上一个失败的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的angular 4应用程序,它一次又一次向受OAuth2保护的API发送多个请求,当请求失败并显示401状态代码时,我遇到了问题,我需要生成一个新的访问令牌并向API重新发起相同的请求,然后将响应返回给正在等待该方法的方法.1-所有请求都一次失败-因此,为了保留其他要继续的请求,我添加了一个循环,当令牌生成过程中,该循环保存进入服务器的请求2-为了发起请求,我在代码块中再次添加了 next.handle(request)行,但是没有用,然后我尝试调用 this.intercept(request,next)方法,即使它不起作用,之后我添加的最后一种方法是在错误块中重新处理请求,所以现在拦截方法的代码如下所示,但随后我意识到甚至没有进一步处理该请求即使我重试处理该请求.有几个教程只是建议 next.handle(request),但是在我看来,它不能按预期工作.
只有我能成功处理失败请求之后的请求,因为直到那时为止,都会生成新的访问令牌,但是失败的请求只会丢弃.

I have my angular 4 application which sends multiple request at the same time one after another to the API which is protected by OAuth2, I m facing issue when the request fails with 401 status code,I need to generate a new access token and re initiate the same request to the API and return the response to the method which is waiting for it. 1- All the request fails one after another - so in order to hold other request to proceed i have added a loop which holds the request coming to the server when the token generation is in progress 2- To initiate the request I added the line next.handle(request) again in the block but was not working, then i tried calling this.intercept(request,next) method even then it was not working, after that the last approach i added was to re handle the request in the error block so now the code of the intercept method looks as below, but then i realized that the request is not even processed further even if i retry to handle the request. There are several tutorial simply suggesting next.handle(request), but in my case it does not works as expected.
Only I was successful in handling the requests which comes after the failed request because till then the new access token is generated but the request which was failed simply drops.

请在链接中查看代码流!

我已经花了很多时间进行调试

I have already spent a lot of time on debugging

推荐答案

我能够通过在拦截方法中添加 Observable.fromPromise 然后调用一个返回promise和在获得刷新令牌之前,递归调用了method方法,但是在每次调用之间增加了延迟,这样我就不会最终递归调用几次.

I was able to achieve this with by adding Observable.fromPromise in the intercept method and then calling a method that returns promise and the method was recursively called until I get the refresh token, but adding a delay in between every call so that I don't end up calling recursively several times.

请为我的代码参考链接.如有任何问题,请发表评论.

Please refer this link for the my code. Please comment if facing any issues .

参考资料:https://www.illucit.com/cn/angular/angular-5-httpinterceptor-add-bearer-token-to-httpclient-requests/

这篇关于提供刷新令牌后重新初始化上一个失败的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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