我已经在nestJs中实现了jwt身份验证。但是,每当我尝试使用以下授权标头进行身份验证时:
Bearer <token> or JWT <token>
我收到401。这些是我的身份验证文件
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: `${process.env.SECRET}`,
});
}
async validate(payload: Credentials) {
const user: Account = await this.authService.validateAccount(payload);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
canActivate(context: ExecutionContext) {
return super.canActivate(context);
}
handleRequest(err, user, info) {
if (err || !user) {
throw err || new UnauthorizedException();
}
return user;
}
}
这是我的身份验证模块
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secretOrPrivateKey: `${process.env.SECRET}`,
}),
AccountModule,
],
providers: [AuthService, JwtStrategy],
controllers: [AuthController],
exports: [PassportModule, AuthService],
})
export class AuthModule {
}
答案 0 :(得分:0)
validate
仅在传递有效的jwt令牌时被调用。当令牌使用其他机密签名或过期时,将永远不会调用validate
。确保您具有有效的令牌。您可以使用jwt debugger来检查令牌。
答案 1 :(得分:0)
我陷入了同样的问题。这里是我的代码(有效)供您比较:
src / auth / auth.module.ts
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { AuthService } from './auth.service';
import { JwtStrategy } from './jwt.strategy';
import { UserModule } from 'src/user/user.module';
import { PassportModule } from '@nestjs/passport';
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secretOrPrivateKey: 'secretKey',
signOptions: {
expiresIn: '1d',
},
}),
UserModule,
],
providers: [AuthService, JwtStrategy],
exports: [PassportModule, AuthService],
})
export class AuthModule {}
src / auth / auth.service.ts
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { JwtPayload } from './interfaces/jwt-payload.interface';
import { UserService } from 'src/user/user.service';
@Injectable()
export class AuthService {
constructor(
private readonly jwtService: JwtService,
private readonly userService: UserService,
) {}
makeToken(payload: JwtPayload) {
const { email } = payload;
return this.jwtService.sign({ email });
}
checkToken(token: string) {
return this.jwtService.verify(token);
}
async validateUser(payload: JwtPayload) {
return await this.userService.read(payload.email);
}
}
src / auth / jwt.strategy.ts
import { Strategy, ExtractJwt, VerifiedCallback } from 'passport-jwt';
import { AuthService } from './auth.service';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtPayload } from './interfaces/jwt-payload.interface';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'secretKey',
});
}
async validate(payload: JwtPayload, done: VerifiedCallback) {
const user = await this.authService.validateUser(payload);
if (!user) {
done(new UnauthorizedException(), false);
}
return done(null, user);
}
}
src / auth / interfaces / jwt-payload.interface.ts
export interface JwtPayload {
email: string;
}
src / account / account.module.ts
import { Module } from '@nestjs/common';
import { AccountController } from './account.controller';
import { PassportModule } from '@nestjs/passport';
import { AuthModule } from 'src/auth/auth.module';
@Module({
imports: [AuthModule],
controllers: [AccountController],
})
export class AccountModule {}
src / account / account.controller.ts
import { Controller, UseGuards, Post } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Controller('account')
export class AccountController {
@Post('skills')
@UseGuards(AuthGuard())
updateSkills() {
return console.log('something');
}
}
P.S。:我没有执行JwtAuthGuard。
我希望它能对您有所帮助:)
答案 2 :(得分:0)
您可以使用护照和NestJS查看最低工作示例
https://github.com/leosuncin/nest-auth-example
您的代码的主要区别:
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }), // I don't do this because I explicity call every stratategy
JwtModule.register({
secretOrPrivateKey: 'secretKey',
signOptions: {
expiresIn: '1d',
},
}),
UserModule,
],
providers: [AuthService, JwtStrategy],
exports: [PassportModule, AuthService], // I don't do this
})
我不使用任何JwtAuthGuard
,而只使用默认值。
答案 3 :(得分:0)
我遇到了类似的问题,nestjs password jwt模块在我的本地计算机上运行正常,但在实时服务器上却无法运行。经过半天的研究,我发现我的令牌头没有到达实时服务器,原因是我在实时服务器上使用了nginx(proxy_pass),并且我的头字段名称为“ access_token”,因此出于某种原因,nginx删除了它
制作一个简单的全局中间件,并检查您是否在标头中接收到令牌。
希望能帮助某人。
答案 4 :(得分:0)
我遇到了这个问题,我解决了。
只需删除 ${procces.env.JWT_SECRET}
并执行其他操作,例如异步注册,我不知道为什么,但它有效。