小编Tom*_*mer的帖子

使用 jwt 和私钥和公钥的 NestJs 身份验证

我正在尝试使用 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)

authentication jwt passport.js jwt-auth nestjs

4
推荐指数
1
解决办法
2515
查看次数

标签 统计

authentication ×1

jwt ×1

jwt-auth ×1

nestjs ×1

passport.js ×1