在我的 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'). …
我正在使用 Docker 和 docker-compose 开发一个 Node 应用程序,它们都有很棒的方法来从.env文件导入多个环境变量。
只是想知道是否有更新 Heroku 部署的环境变量而不是键入它们的快捷方式。
--env-file我注意到官方文档上没有选项: https ://devcenter.heroku.com/articles/config-vars