我按照几篇文章在 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()
和getToken()
方法,jwt.interceptor.ts
哪些包含