微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NestJS 更新后 AuthGuard 损坏

如何解决NestJS 更新后 AuthGuard 损坏

我有一个使用 jwt 实现身份验证的 nestJS(版本 6)项目。以下配置工作正常:

package.json
... 
“@nest-modules/mailer”: “^1.1.3",“@nestjs/common”: “^6.6.7",“@nestjs/core”: “^6.6.7",“@nestjs/jwt”: “^6.1.1",“@nestjs/passport”: “^6.1.0",“@nestjs/platform-express”: “^6.6.7",...

jwt strategy file

@Injectable()
export class JwtBearerStrategy extends PassportStrategy(Strategy) {
  constructor(
    private readonly sessionService: SessionService,private readonly userRepository: UsersRepository
  ) {
    super({
      jwtFromrequest: ExtractJwt.fromAuthHeaderAsBearerToken(),secretorKey: SESSION_SECRET_KEY
    });
  }

  public async validate(session: Session) {
    const user = await this.userRepository.findByUsername(session.user.username);
    if (!user) {
      throw new UnauthorizedException();
    }
    delete user.password;
    return user;
  }
}

End points are protected as follows
@Controller('/session')
export class SessionController {
  constructor(
    private readonly sessionService: SessionService,private readonly userRepository: UsersRepository
  ) {}

  @Get()
  @UseGuards(AuthGuard())
  public async getCurrentUser(@CurrentUser() currentUser: User) {
    return currentUser;
  }

但是,由于我更新了 nestJs 核心和通用模块,因此无法提取身份验证。我的 @CurrentUser 装饰器在请求中找不到用户对象。嵌套模块已更新为以下内容

...
"@nestjs/common": "^7.6.15","@nestjs/core": "^7.6.15","@nestjs/jwt": "^7.2.0","@nestjs/microservices": "^7.6.15","@nestjs/passport": "^7.1.5","@nestjs/platform-express": "^7.6.15",...

现在我的 @UseGuards 装饰器抛出了这个自定义错误

[CurrentUser Decorator]: No user found on request
  No user objet found on request
   * Please ensure Passport Module is configured correctly
   * Ensure that the controller,or method is using @UseGuards(AuthGuard())
  ** see docs/auth.md for more informaion **

AuthGuard 在第 7 版中的工作方式似乎略有不同。有没有其他人遇到过这个问题并且能够帮助我?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?