在nestjs应用程序中,为什么payload.sub在JwtStrategy的validate方法中未定义?

在nestjs应用程序中,为什么payload.sub在JwtStrategy的validate方法中未定义?,nestjs,jwt-auth,Nestjs,Jwt Auth,我正在开发一个nestjs应用程序,并使用JwtStrategy进行身份验证 我通过唱一些信息来生成访问令牌,包括有效负载。sub: token.service.ts async createAccessToken( payload: IJwtPayload, expires = this.expiresInDefault, ): Promise<LoginResponseVm> { ... other codes .... // sign

我正在开发一个
nestjs
应用程序,并使用
JwtStrategy
进行身份验证

我通过唱一些信息来生成
访问令牌
,包括
有效负载。sub

token.service.ts

async createAccessToken(
    payload: IJwtPayload,
    expires = this.expiresInDefault,
  ): Promise<LoginResponseVm> {

    ... other codes ....

    // sign
    // here payload is contain sub property and is filled by userId
    const signedPayload = sign(payload, this.jwtKey, options);
    const token: LoginResponse = {
      accessToken: signedPayload,
      expiresIn: expires,
    };

    return this.mapperService.map<LoginResponseVm>(
      token,
      LoginResponseVm.name,
      LoginResponse.name,
    );
  }
export interface IJwtPayload {
  sub: string;
  iat?: number;
  exp?: number;
  jti?: string;
}
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(
    private readonly tokenService: TokenService,
    configurationService: ConfigurationService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        ExtractJwt.fromAuthHeaderAsBearerToken(),
        ExtractJwt.fromUrlQueryParameter('access_token'),
      ]),
      secretOrKey: configurationService.JWT.Key,
      passReqToCallback: true,
    });
  }

  async validate(payload: IJwtPayload) {

    console.log(payload.sub); // <<--- Problem: payload.sub is undefined!
    const result = await this.tokenService.validatePayload(payload);
    if (!result) {
      throw new UnauthorizedException();
    }
    return result;
  }
}
但在运行项目时,使用生成的承载访问令牌填充请求的授权头,并在下面的验证方法上设置断点。有效负载有值,但
有效负载。sub
未定义为什么?

jwt战略.ts

async createAccessToken(
    payload: IJwtPayload,
    expires = this.expiresInDefault,
  ): Promise<LoginResponseVm> {

    ... other codes ....

    // sign
    // here payload is contain sub property and is filled by userId
    const signedPayload = sign(payload, this.jwtKey, options);
    const token: LoginResponse = {
      accessToken: signedPayload,
      expiresIn: expires,
    };

    return this.mapperService.map<LoginResponseVm>(
      token,
      LoginResponseVm.name,
      LoginResponse.name,
    );
  }
export interface IJwtPayload {
  sub: string;
  iat?: number;
  exp?: number;
  jti?: string;
}
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(
    private readonly tokenService: TokenService,
    configurationService: ConfigurationService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        ExtractJwt.fromAuthHeaderAsBearerToken(),
        ExtractJwt.fromUrlQueryParameter('access_token'),
      ]),
      secretOrKey: configurationService.JWT.Key,
      passReqToCallback: true,
    });
  }

  async validate(payload: IJwtPayload) {

    console.log(payload.sub); // <<--- Problem: payload.sub is undefined!
    const result = await this.tokenService.validatePayload(payload);
    if (!result) {
      throw new UnauthorizedException();
    }
    return result;
  }
}
@Injectable()
导出类JwtStrategy扩展了PassportStrategy(策略){
建造师(
专用只读令牌服务:令牌服务,
configurationService:configurationService,
) {
超级({
jwtFromRequest:ExtractJwt.fromExtractors([
从AuthHeaderAbarerToken()中提取JWT,
ExtractJwt.fromUrlQueryParameter(“访问令牌”),
]),
secretrokey:configurationService.JWT.Key,
passReqToCallback:正确,
});
}
异步验证(有效负载:IJwtPayload){

console.log(payload.sub);//我替换了此签名:

async validate(payload: IJwtPayload) {
用这个

async validate(req: Request, payload: IJwtPayload, done: Function) {

    const result = await this.tokenService.validatePayload(payload);
    if (!result) {
       return done(new UnauthorizedException(), false);
    }
    done(null, result);
}
我找到了上面的签名