我正在尝试使用 nestJS 了解 jwt 和身份验证。我创建了两个单独的微服务,其中一个是身份验证服务,成功登录后,客户端获得 jwt 令牌,使用此令牌他可以访问另一个微服务。
这是 auth 服务的 JwtStrategy 和 AuthModule 的代码:
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: 'secretKey'
});
}
async validate(payload: any) {
return payload;
}
}
Run Code Online (Sandbox Code Playgroud)
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { UsersModule } from '../users/users.module';
import { PassportModule …Run Code Online (Sandbox Code Playgroud)