加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular5.x拦截器 给get post请求添加参数user_token

发布时间:2020-12-17 07:03:37 所属栏目:安全 来源:网络整理
导读:export class Interceptor implements HttpInterceptor { public myAppListService; constructor(private message: NzMessageService,private injector: Injector) { } public intercept(req: HttpRequestany,next: HttpHandler): ObservableHttpEventany {
export class Interceptor implements HttpInterceptor {
public myAppListService;
constructor(private message: NzMessageService,private injector: Injector) {
}

public intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {
this.myAppListService = this.injector.get(MyAppListService);
this.myAppListService.selectedUserToken = getQueryString(‘user_token‘) || sessionStorage.getItem(‘user_token‘);
?
if (this.myAppListService.selectedUserToken !== ‘null‘) {
let newParams;
let newBody;
if (req.method === ‘GET‘) {
let userToken = this.myAppListService.selectedUserToken;
if (userToken) {
newParams = req.params.set(‘user_token‘,userToken);
const comReq = req.clone({
params: newParams,
body: newBody
});
return next.handle(comReq).do((res) => {
this.handleResponse(res);
});
}
} else if (req.method === ‘POST‘ && req.headers.get(‘content-type‘).indexOf(‘application/x-www-form-urlencoded;‘) > -1) {
// if (req.body.indexOf(‘user_token=‘)) {
let userToken = this.myAppListService.selectedUserToken;
if (userToken) {
if (req.body.match(/(^|&)user_token=([^&]*)(&|$)/)) {
newBody = req.body.replace(req.body.match(/(^|&)user_token=([^&]*)(&|$)/)[0],‘‘);
newBody = newBody + ‘&user_token=‘ + userToken;
} else {
newBody = ‘&user_token=‘ + userToken;
}
const comReq = req.clone({
params: newParams,
body: newBody
});
return next.handle(comReq).do((res) => {
this.handleResponse(res);
});
}
// }
}

}
const comReq = req.clone();
return next.handle(comReq).do((res) => {
this.handleResponse(res);
});
}
public handleResponse(res) {
if (res instanceof HttpResponse) {
const data = res.body;
if (data.status && data.status === 2) {
this.message.error(data.data);
location.href = ‘/index.php?r=Login/Ulogin‘;
}
if (data.status && data.status === 1 && data.data === ‘应用不存在,或者您不是应用管理者‘) {
location.href = ‘/index.php?r=Login/Ulogin‘;
return;
}
}
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读