JWTInterceptor
import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; @Injectable() export class JWTInterceptor implements HttpInterceptor { constructor() { } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { req = req.clone({ setHeaders: {Authorization: 'Token Token_admin_id'} }); return next.handle(req); } }
ResponseInterceptor
import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; @Injectable() export class ResponseInterceptor implements HttpInterceptor { constructor() { } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(req).map(event => { if (event instanceof HttpResponse) { switch (event.status) { case 401: // expired jwt go to login break; case 404: // not found break; case 403: // forbidden break; case 500: // service inner error break; default: console.log('data return successfully!'); } } return event; }); } }
引入:
import {HttpClientModule, HTTP_INTERCEPTORS} from '@angular/common/http';
providers: [ {provide: HTTP_INTERCEPTORS, useClass: JWTInterceptor, multi: true}, {provide: HTTP_INTERCEPTORS, useClass: ResponseInterceptor, multi: true}],