typescript - NestJS JwtStrategy 使用 configService 传递密钥
问题描述
我有来自文档示例(https://docs.nestjs.com/techniques/authentication)的 JwtStrategy 类:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(
private readonly authService: AuthService,
private readonly configService: ConfigService,
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: this.configService.getSecretKey,
});
}
// ...
}
当我this
在调用 super() 之前尝试访问时,出现错误。但我仍然想使用 configService 来获取密钥。
我知道我可以使用 env var 来做到这一点,但在我看来,服务方法是更清晰的解决方案。
我如何使用 configService 或者从中获取价值并传递给 super() 调用?谢谢。
解决方案
只需删除this.
,请参见此处:
secretOrKey: configService.getSecretKey
configService
由于已作为参数传递,因此它将起作用。
推荐阅读
- vba - 如何从Word中提取嵌入的文件
- java - 关于docker-java的withCmd命令
- reactjs - 使用 useReducer 和其他 reducer 清除表单
- matlab - FFT(快速傅立叶变换)的频率分辨率(或增量)
- python - MaxPooling2D 给出 ValueError
- list - Haskell 编译器在简单列表代码上的错误
- cuda - CUDA cudaMemcpyAsync 使用单个流来托管
- python - 通过python具有多个组的ngroup
- java - 在 Android Studio 中读取二进制数据
- python - /account/delete/testuser1 处的 AttributeError:“str”对象没有属性“field”