You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS中JwtStrategy的validate函数未被调用问题求助

NestJS中JwtStrategy的validate函数未被调用问题求助

各位大佬好!我最近在自己的NestJS+Angular项目里折腾JwtStrategy,遇到个头疼的问题——不管我怎么调整代码,策略里的validate()函数就是不触发!用户验证是要对接Microsoft Active Directory服务器的,先把我后端的Authentication模块代码贴出来,麻烦帮我看看哪里出问题了:

后端Authentication模块代码

import { Module } from '@nestjs/common';

import { AuthenticationService } from './authentication.service';
import { ConfigService } from '@nestjs/config';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';

import { MyProjectStrategy } from './genius.strategy';
import { AuthenticationController } from './authentication.controller';
import { UsersService } from 'src/users/users.service';

const jwtFactory = {
    useFactory: async (configService: ConfigService) => {
        return {
            privateKey: configService.get<string>('JWT_PRIVATE_KEY_EC512', ''),
            publicKey: configService.get<string>('JWT_PUBLIC_KEY_EC512', ''),
        };
    },
    inject: [ConfigService],
};

对了,要是需要更多信息排查的话,我还可以补充这些内容:

  • 我的MyProjectStrategy(也就是自定义的JwtStrategy)具体实现代码,包括构造函数和validate方法的写法
  • 控制器里使用JWT认证守卫的方式,比如有没有给需要认证的接口加上@UseGuards(JwtAuthGuard)装饰器
  • JWT的配置细节,比如算法是不是和我用的EC512匹配,密钥有没有从配置文件正确加载
  • 请求接口时,请求头里有没有正确携带Authorization: Bearer <token>格式的认证信息

备注:内容来源于stack exchange,提问作者Nelson Teixeira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 11:24:40