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. opencv3计算机视觉+Python(四)

    使用分水岭和GrabCut算法进行物体分割 用GrabCut算法进行图像分割 在OpenCV中,实现了grabcut分割算法,该算法可以方便的分割出前景图像,操作简单,而且分割的效果很好.算法的原理参 ...

  2. key points & (QA) about RPKI

    @1: Q: What does ROA look like?Since ROA means which ASes are allowed for originating routes to some ...

  3. GCE 创建一个Linux VM

    sudo yum install wget 安装Java sudo wget --no-check-certificate --no-cookies --header "Cookie: or ...

  4. SQLServer数据库基本操作,导入Excel数据

    打开SQLServer客户端,连上服务端 先建立数据库,点击新建查询 基本操作如下 创建表 create table mytest ( id int primary key identity(1,1) ...

  5. windows下docker toolbox无法下载boot2docker.iso

    GitHub连不上导致自动更新失败.(网络形势严峻!) 通过别的途径手动下载了指定的最新的boot2docker.iso文件.(比方说迅雷!比方说迅雷!比方说迅雷!) https://github.c ...

  6. samtools+bcftools 进行SNP calling

    两个软件的作用:1.samtools mpileup 主要是用于收集BAM文件中的信息,这个位点上有多少条read匹配,匹配read的碱基是什么,并将这些信息存储在BCF文件中.2.bcftools ...

  7. 20145231第二周Java学习笔记

    20145231 <Java程序设计>第2周学习总结 教材学习内容总结 本周的学习采用的依然是先看课本,再看视频,然后实践敲代码,最后根据学习笔记总结完成博客. 第三章:基础语法 知识点比 ...

  8. CentOS 6.5 下的截图方法

    1.利用命令模式 捕获整个屏幕 : $ gnome-screenshot 截完屏之后我们可以设置自定义图片存储位置,如图: 捕获当前终端Terminal : $ gnome-screenshot -w ...

  9. Eclipse常用插件安装_PropertiesEditor

    properties文件在项目中多用做i18n国际化支持的配置文件,在properties文件中出现的中文信息都要转换为Unicode文本,一般的做法都是使用JDK自带的native2ascii工具进 ...

  10. Eclipse与Tomcat的集成(无插件)

    1.下载Eclipse(https://www.eclipse.org/downloads/)和Tomcat(http://tomcat.apache.org/),具体的安装略: 2.打开Eclips ...