Typescript HttpInterceptor上的递归太多
在我的应用程序中,身份验证基于JWT令牌。为了使它们失效,我在JWT中放置了一个随机的安全字符串,并在数据库中存储了与拥有令牌的用户关联的完全相同的字符串。这两者必须匹配才能使JWT有效。 当用户注销时,API生成另一个随机字符串并替换数据库中的旧字符串,使得JWT对该用户无效,因为de JWT中的字符串与数据库中的字符串不匹配 为了实现这一点,我需要一个拦截器,它在向API发出每个请求之前发出一个静默请求,以检查随机字符串是否相等。 基于这个问题,我的拦截器工作正常,但行为怪异,发出无限请求并抛出“太多递归”错误 我做错了什么 拦截器Typescript HttpInterceptor上的递归太多,typescript,angular5,interceptor,Typescript,Angular5,Interceptor,在我的应用程序中,身份验证基于JWT令牌。为了使它们失效,我在JWT中放置了一个随机的安全字符串,并在数据库中存储了与拥有令牌的用户关联的完全相同的字符串。这两者必须匹配才能使JWT有效。 当用户注销时,API生成另一个随机字符串并替换数据库中的旧字符串,使得JWT对该用户无效,因为de JWT中的字符串与数据库中的字符串不匹配 为了实现这一点,我需要一个拦截器,它在向API发出每个请求之前发出一个静默请求,以检查随机字符串是否相等。 基于这个问题,我的拦截器工作正常,但行为怪异,发出无限请求并
import { TokenService } from './token.service';
import { Router } from '@angular/router';
import { HttpInterceptor,
HttpRequest,
HttpHandler,
HttpEvent,
HttpClient,
HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { SharedService } from "./shared-service.service";
import { CookieService } from 'ngx-cookie-service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private sharedService : SharedService,
private httpClient : HttpClient,
private router : Router,
private cookieService : CookieService,
private tokenService : TokenService) {}
token: string;
cd: number;
isExpired: boolean = false;
revog_token: string;
intercept(req: HttpRequest<any>,
next: HttpHandler): Observable<HttpEvent<any>> {
// Check if token exists on cookies
if(this.tokenService.CheckToken()){
if (req.url.match(/api\//)) { // Request to API
console.log("API Request");
this.CheckRevogToken(); // <-- THIS METHOD TRIGGERS THE "TOO MUCH RECURSION" ERROR
}
const cloned = req.clone({ headers: req.headers.set("Authorization","Bearer "+ this.tokenService.GetToken()) });
return next.handle(cloned);
}
else{
return next.handle(req).catch((error, caught) => {
// If status code is 401 ==> Not authorized
if(error.status == 401){
alert("A sua sessão expirou! Será redirecionado para a página de autenticação");
}else{
console.log("Ocorreu um erro");
return Observable.throw(error);
}}) as any;
}
}
CheckRevogToken(){
this.revog_token = this.tokenService.GetRevogFromDecodedToken();
var headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this.httpClient.post("http://localhost:53448/api/check_revog_token", {cd: this.cd, revog_token: this.revog_token}, {headers:headers})
.subscribe(
res => {
console.log(res);
},
err => {
console.log("err: " +err);
}
)};
}
从“/token.service”导入{TokenService};
从'@angular/Router'导入{Router};
导入{HttpInterceptor,
HttpRequest,
HttpHandler,
HttpEvent,
HttpClient,
HttpHeaders}来自“@angular/common/http”;
从“@angular/core”导入{Injectable}”;
从“rxjs/Observable”导入{Observable};
从“/shared service.service”导入{SharedService};
从“ngx cookie服务”导入{CookieService};
@可注射()
导出类AuthInterceptor实现HttpInterceptor{
构造函数(私有sharedService:sharedService,
私有httpClient:httpClient,
专用路由器:路由器,
私人厨师服务:厨师服务,
专用令牌服务:令牌服务){}
令牌:字符串;
cd:编号;
isExpired:boolean=false;
revog_令牌:字符串;
拦截(req:HttpRequest您已经在代码中创建了无限循环:
您可以从:http://localhost:53448/api/...
请求
您的HttpInterceptor
捕获它。因为url匹配(/api\//)
格式,所以调用此.CheckRevogToken()
方法
在CheckRevogToken()
中,您创建了一个听者,并将其发布到以下url:“http://localhost:53448/api/check_revog_token“
HttpInterceptor
再次捕获您的post请求。因为url再次匹配(/api\//)
,所以重复步骤2
为什么要返回this.httpClient.post()的可观察值
即使您不使用它?请尝试删除之前的return
语句。这是因为CheckRevogToken
在您调用此url时也在调用http侦听器:http://localhost:53448/api/check_revog_token
这也将在下次调用拦截器等。因此,您必须在ch之前阻止它eck令牌method@D.Simon我正在进行更改,该行处于“备用”状态,以便稍后使用。但是,谢谢anyway@JulienMetral可能看了几个星期都没注意到。非常感谢!哦,现在我知道发生了什么!也许如果我更改/api/check\u revog\u token的端点,问题就解决了…非常感谢!