Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular jwt拦截器逻辑如何_Angular_Logic - Fatal编程技术网

Angular jwt拦截器逻辑如何

Angular jwt拦截器逻辑如何,angular,logic,Angular,Logic,我很困惑: 看这个,我将解释如何理解jwt拦截器对令牌槽的刷新 1请求有一个令牌,它被发送到服务器 2服务器响应400个错误请求 拦截器处理第一次尝试中所做的HTTP响应:向服务器调用刷新令牌 4刷新令牌是旧的,所以服务器,比如说400 5现在会发生什么?。服务器是否会发送另一个刷新服务?我怎样才能摆脱口臭 // src/app/auth/jwt.interceptor.ts // ... import 'rxjs/add/operator/do'; export class JwtInte

我很困惑: 看这个,我将解释如何理解jwt拦截器对令牌槽的刷新

1请求有一个令牌,它被发送到服务器

2服务器响应400个错误请求

拦截器处理第一次尝试中所做的HTTP响应:向服务器调用刷新令牌

4刷新令牌是旧的,所以服务器,比如说400

5现在会发生什么?。服务器是否会发送另一个刷新服务?我怎样才能摆脱口臭

// src/app/auth/jwt.interceptor.ts

// ...
import 'rxjs/add/operator/do';

export class JwtInterceptor implements HttpInterceptor {

  constructor(public auth: AuthService) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(req).do((event: HttpEvent<any>) => {
      if (event instanceof HttpResponse) {
        // do stuff with response if you want
      }
    }, (err: any) => {
      if (err instanceof HttpErrorResponse {
        if (err.status === 401) {
          // redirect to the login route
          // or show a modal
        }
      }
    });
  }
}

如果您的令牌是旧的,那么在将响应发送到客户端之前,您应该在服务器上终止会话。如果您使用的是express会话,则只需调用destroy,然后发送401即可。在您的拦截器中,您还可以在执行身份验证重定向之前销毁客户端上会话的任何方面