小编Rob*_*ley的帖子

NestJS JWT 策略需要一个秘密或密钥

在我的 NestJS 节点应用程序中,我已经使用 Passport 设置了 JWT 身份验证(根据https://docs.nestjs.com/techniques/authentication)但是我试图将 JWT 密钥保留在使用内置检索的环境文件中 -在配置服务中。

export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly configService: ConfigService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('JWT_KEY'),
      signOptions: { expiresIn: '60s' }
    });
  }
Run Code Online (Sandbox Code Playgroud)

该模块注册如下:

JwtModule.registerAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => {
        return {
          secret: configService.get<string>('JWT_KEY')
        };
      },
      inject: [ConfigService]
    })
Run Code Online (Sandbox Code Playgroud)

启动应用程序时出现以下错误:

api: [Nest] 16244   - 03/27/2020, 10:52:00   [ExceptionHandler] JwtStrategy requires a secret or key +1ms
Run Code Online (Sandbox Code Playgroud)

看起来 JWTStrategy 类在 ConfigService 准备好提供 JWT Key 之前正在实例化,并且在调用configService.get<string>('JWT_KEY'). …

authentication node.js jwt passport.js nestjs

8
推荐指数
1
解决办法
3849
查看次数

标签 统计

authentication ×1

jwt ×1

nestjs ×1

node.js ×1

passport.js ×1