[英]Angular Detect 401 Unauthorized Access using JWT
我關注了幾篇文章,以在Angular上實現JWT身份驗證,除了檢測到401未經授權之外,其他所有功能似乎都可以正常工作。
我想將用戶重定向到登錄頁面,但無法檢測到401錯誤。
我有這個課:
export class JwtInterceptor implements HttpInterceptor {
constructor(public authService: AuthService) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
console.log('HTPP REQUEST');
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this.authService.collectFailedRequest(request);
console.log('Login boy');
}
}
});
}
}
但不確定在哪里使用它。 我有auth.service.ts
,其中包含login()
和jwt.interceptor.ts
getToken()
方法,以及jwt.interceptor.ts
,其中包含
providers: [PagerService, AuthService,
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}, {
provide: HTTP_INTERCEPTORS,
useClass: JwtInterceptor,
multi: true
}
]
我必須在module.app.ts
添加多個攔截器。 不要忘記在攔截器的export class...
之前添加@Injectable()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.