2

所以,我很困惑。我慢慢掌握了 NestJS,但它的passport工作方式让我感到困惑。

我按照教程进行操作,一切正常。

我创建了一个 JWT 策略:

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor(private prisma: PrismaService) {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            secretOrKey: 'topSecret51',
        });
    }

    async validate(payload: JwtPayload): Promise<User | null> {
        const { email } = payload;
        const user = await this.prisma.user.findOne({
            where: { email }
        });

        if (!user) {
            throw new UnauthorizedException('Athorisation not provided')
        }
        return user;
    }
}

并定义了模块:

@Module({
  imports: [
    PassportModule.register({
      defaultStrategy: 'jwt',
    }),
    JwtModule.register({
      secret: 'topSecret51',
      signOptions: {
        expiresIn: 3600,
      },
    })
  ],
  providers: [UserService, PrismaService, JwtStrategy],
  controllers: [UserController],
  exports: [JwtStrategy, PassportModule],
})
export class UserModule {}

,一个有效的令牌被发行了。我不明白的是如何passport访问JwtStrategy. 护照如何知道我的文件夹结构中有一个文件包含一个JwtStrategy

1.) 它不是由PassportModuleor注入的依赖项JWTModule 2.) 它不作为参数传递给任何方法

是否有一些幕后魔术可以查看所有提供者并确定它们中的任何一个是否是提供给的参数的子类PassportStrategy

4

1 回答 1

4

NestJS 的护照模块有很多神奇之处。我会尽我所能解释这一切是如何运作的,尽管有时它甚至超出了我的范围。

首先要注意的是,每个PassportStrategy 都必须扩展抽象 mixin PassportStrategy。这个 mixinStrategy从普通的护照包中获取一个(passport-jwt在这种情况下)。此策略有一个默认名称(jwt此处),但您可以传递第二个参数来提供您自己的名称。

Nest 做了一些非常酷的魔法来将值从CustomStrategy构造函数的super()调用传递到护照正常注册。然后它将CustomStrategy#validate其应用于护照的验证回调,这就是该validate方法必须匹配护照的预期verify回调的原因。

现在我们已经用护照完成了一些注册,但是要正确调用这些,我们需要查看AuthGuardmixin。通常情况下,我们可以向 mixin 传递一个策略,该策略需要匹配一个护照策略的名称(如jwt本例所示),或者可以在PassportModule'defaultStrategy选项中设置。jwt从这里开始,守卫通过名称( 、googlelocal等)调用策略并传递 Nest 创建的 http 上下文request中的response、 和next。在调用结束时,request.user由 的返回值设置passports.authenticate

于 2020-06-17T20:15:28.733 回答