Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

如何在nestjs的守卫里面获得header的值

我请求的token放在header里面,我想在nestjs的守卫代码里获得header里面的token,但是不知道怎么在守卫获得header对象
代码如下,有人知道吗?

import {
  CanActivate,
  ExecutionContext,
  HttpException,
  HttpStatus,
  Injectable,
} from '@nestjs/common';
import { Observable } from 'rxjs';
const whiteList = ['/admin/login'];
@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest();
    console.log(request.user);
    const url = request.route.path;
    //   console.log(request);
 //白名单的直接返回
 if (whiteList.includes(url)) {
      return true;
    }
    // if (!header.token) {
 //   //throw new HttpException('用户未登录', HttpStatus.BAD_REQUEST);
 // }
 return true;
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

解决了,麻烦大家了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...