nestjs-jwt - nestjs:Jwt 策略中的有效载荷未定义
问题描述
一切都很好,当我登录时,我得到了 jwt
但是当我想访问需要身份验证的资源时,在验证方法中,有效负载是未定义的
这是策略 jwt
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(
private userService:UserService
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey:"aaaaaaa",
});
}
async validate(payload:any) {
console.log(payload)
const user =await this.userService.findUserByEmail(payload.email)
console.log(user)
if(user){
const {password,salt,...result}=user
console.log(result)
return result;
}
else throw new UnauthorizedException()
}
}
这是模块
@Module({
exports:[UserService],
controllers: [UserController],
providers: [UserService,JwtStrategy],
imports : [TypeOrmModule.forFeature([UserEntity]),PassportModule.register(
{defaultStrategy:'jwt'}),
JwtModule.register({secret:'MedAKRAOU',signOptions:{expiresIn:3600000}})]
})
export class UserModule {}
解决方案
推荐阅读
- javascript - 在 Promise 中移除事件监听器
- nearprotocol - NEAR 上的智能合约有事件还是我需要轮询链来获取数据?
- spring - Spring security JWT 过滤器抛出 500 和 HTML 而不是 401 和 json
- python-3.x - python3:字节与字节数组,并转换为字符串和从字符串转换
- php - Laravel邮件,不能将变量传递给主题,但它可以在正文中使用
- javascript - pdf.js:循环搜索文本的页面
- python - Python - 尝试查找当前是否正在运行 sikuli 脚本
- python-3.x - TypeError:需要字符串参数,得到“字节”
- python - 在python中注释绘图时出现KeyError
- flutter - 如何在另一个 dart 文件中声明函数并到达主要变量