gpt4 book ai didi

authentication - NestJS jwt-passport 认证

转载 作者:行者123 更新时间:2023-12-02 02:49:17 30 4
gpt4 key购买 nike

我想实现一个分布式身份验证库以在多个项目中使用它。该库应实现 JWT 身份验证方法。代码如下:
jwt.strategy.ts

import {ExtractJwt, Strategy} from 'passport-jwt';
import {PassportStrategy} from '@nestjs/passport';
import {Injectable} from '@nestjs/common';
import {JwtPayload, User} from './interfaces';
import {ConfigService} from "./config.service";

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor(private readonly configService: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: configService.get('secretOrPrivateKey'),
});
}

async validate(payload: JwtPayload): Promise<User> {
return {
uuid: payload.uuid,
email: payload.email,
}
}
}
jwt.auth.module.ts:
import {Module, DynamicModule} from '@nestjs/common';
import {JwtModule} from '@nestjs/jwt';
import {JwtStrategy} from './jwt.strategy';
import {PassportModule} from '@nestjs/passport';
import {ConfigService} from "./config.service";
import {JwtOptions} from "./interfaces/jwt.options";

@Module({
})

export class JwtAuthModule {
static forRoot(jwtOptions): DynamicModule {
return {
module: JwtAuthModule,
imports: [
// JwtModule.register(jwtOptions),
// PassportModule.register({defaultStrategy: 'jwt'}),
],
providers: [
JwtStrategy,
{
provide: ConfigService,
useValue: new ConfigService(jwtOptions),
}
],
exports: [ConfigService, JwtStrategy]
};
}
}
我已经将它导入到我的 app.module.ts 中:
import { Module, NestModule, HttpModule } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { environment } from './environments';
import { AuthModule } from './auth/auth.module';
import { PermissionModule } from './permission/permission.module';
import {JwtAuthModule} from '@pe/nest-kit';
import {JwtModule} from '@nestjs/jwt';
import {PassportModule} from '@nestjs/passport';

@Module({
imports: [
JwtModule.register(environment.jwtOptions),
PassportModule.register({defaultStrategy: 'jwt'}),
JwtAuthModule.forRoot(environment.jwtOptions),
HttpModule,
AuthModule,
PermissionModule,
MongooseModule.forRoot(environment.mongodb),
],
})
export class ApplicationModule implements NestModule {
configure() {
}
}
但是,每次我尝试打开项目 url 时,都会出现错误:

[Nest] 27645 - 24.10.2018, 15:23:26 [ExceptionsHandler] Unknownauthentication strategy "jwt" +4119ms Error: Unknown authenticationstrategy "jwt"at attempt (/home/user/workspace/permissions/node_modules/passport/lib/middleware/authenticate.js:187:37)at authenticate (/home/user/workspace/permissions/node_modules/passport/lib/middleware/authenticate.js:363:7)at Promise (/home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:83:3)at new Promise ()at /home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:75:83at MixinAuthGuard. (/home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:47:36)at Generator.next ()at /home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:19:71at new Promise ()at __awaiter (/home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:15:12)at MixinAuthGuard.canActivate (/home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:40:20)at GuardsConsumer.tryActivate (/home/user/workspace/permissions/node_modules/@nestjs/core/guards/guards-consumer.js:13:34)at canActivateFn (/home/user/workspace/permissions/node_modules/@nestjs/core/router/router-execution-context.js:97:59)at /home/user/workspace/permissions/node_modules/@nestjs/core/router/router-execution-context.js:47:37at /home/user/workspace/permissions/node_modules/@nestjs/core/router/router-proxy.js:8:23at Layer.handle [as handle_request] (/home/user/workspace/permissions/node_modules/express/lib/router/layer.js:95:5)


我究竟做错了什么?

最佳答案

你确定你已经添加了所有需要的包吗?尝试通过认证文档https://docs.nestjs.com/techniques/authentication它很好地解释了如何处理 JWT。

关于authentication - NestJS jwt-passport 认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52969368/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com