基于RESTful API 怎么设计用户权限控制 sails-permissions

一 注册与登陆(jwt)

  1. 安装依赖包
 npm install passport passport-jwt passport-local @nestjs/passport @nestjs/jwt -S
  1. 创建 auth 模块
    // 在 domain 目录下创建 auth 相关文件
    nest g service auth domain
    nest g module auth domain
  1. 编写 jwt.strategy.ts 文件(固定写法)
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { jwtConstants } from './constants';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: jwtConstants.secret,
    });
  }

  // JWT验证 - Step 4: 被守卫调用
  async validate(payload: any) {
    console.log(`JWT验证 - Step 4: 被守卫调用`);
    return { userId: payload.sub, username: payload.username, realName: payload.realName, role: payload.role };
  }
}

几个坑点

  1. export/import 造成得 module/service 报错问题
  2. jwt 登陆策略得问题: UseGuard(AuthGuard('local'))/UseGuard(AuthGuard('jwt'))

待解决

  1. 引入异常过滤器后,前置数据校验失败

results matching ""

    No results matching ""