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}],

@angular/cli项目构建--interceptor的更多相关文章

  1. @angular/cli项目构建--组件

    环境:nodeJS,git,angular/cli npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm instal ...

  2. @angular/cli项目构建--modal

    环境准备: cnpm install ngx-bootstrap-modal --save-dev impoerts: [BootstrapModalModule.forRoot({container ...

  3. @angular/cli项目构建--Dynamic.Form

    导入所需模块: ReactiveFormsModule DynamicFormComponent.html <div [formGroup]="form"> <l ...

  4. @angular/cli项目构建--animations

    使用方法一(文件形式定义): animations.ts import { animate, AnimationEntryMetadata, state, style, transition, tri ...

  5. @angular/cli项目构建--路由3

    路由定位: modifyUser(user) { this.router.navigate(['/auction/users', user.id]); } 路由定义: {path: 'users/:i ...

  6. @angular/cli项目构建--httpClient

    app.module.ts update imports: [ HttpClientModule] product.component.ts import {Component, OnInit} fr ...

  7. @angular/cli项目构建--路由2

    app.module.ts update const routes: Routes = [ {path: '', redirectTo: '/home', pathMatch: 'full'}, {p ...

  8. @angular/cli项目构建--路由1

    app.module.ts import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angu ...

  9. @angular/cli项目构建--Dynamic.Form(2)

    form-item-control.service.ts update @Injectable() export class FormItemControlService { constructor( ...

随机推荐

  1. Python2 socket 多线程并发 ThreadingTCPServer Demo

    # -*- coding:utf-8 -*- from SocketServer import TCPServer, StreamRequestHandler import traceback cla ...

  2. mysql数据库中表记录的玩法

    一.增加表记录(相当于插入表记录) 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); 语法二: INS ...

  3. sed Demo

    @1:sed basic usage: 和AWK一样, sed也是逐行对文本进行处理. sed的主要功能如下: @1:对每行中的匹配项进行处理(修改/删除) @2:格式化文本的处理 @3:(行的增删改 ...

  4. python的PIL模块安装

    一.Centos安装PIL #尤其重要,否则会报错 yum install python-devel yum install libjpeg libjpeg-devel zlib zlib-devel ...

  5. Django 项目补充知识(JSONP,前端瀑布流布局,组合搜索,多级评论)

    一.JSONP 1浏览器同源策略 通过Ajax,如果在当前域名去访问其他域名时,浏览器会出现同源策略,从而阻止请求的返回 由于浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一 ...

  6. Loadrunder之脚本篇——参数类型

    Internal data Date/Time,Group Name,Iteration Number,Load Generator Name,Ramdom Number,Table,Unique N ...

  7. [Papers]Finding Advertising Keywords on Web Pages

    参考资料: Finding Advertising Keywords on Web Pages ,Wen-tau Yih,Joshua Goodman, Vitor R. Carvalho

  8. 0530JavaScript基础2

    常用内置对象 所谓内置对象就是ECMAScript提供出来的一些对象,我们知道对象都是有相应的属性和方法 数组Array(部分相当于列表) 1.数组的创建方式 var colors = ['red', ...

  9. openpyxl之excel操作

    一.读取excel中内容 1.导入模块 : from openpyxl import load_workbook 2.打开excel : workbook = load_workbook(" ...

  10. PHP类的变量与成员,及其继承、访问与重写要注意的问题

    PHP的类及其实例: <?php ?>  后期静态绑定:为了避免子类重写静态属性后,使用继承来的方法仍然方法父类的静态属性,PHP5.3增加了一个新的语法,后期静态绑定,使用static关 ...