nestjs的jwt授权认证
概述
最近在用nestjs重新做RBAC构架。
本次完整实践nestjs的注册登陆,到jwt授权认证,到guard守卫拦截验证,到strategy的JWT策略。
和Spring Boot的Shiro对比下来,还是nestjs的写着舒服。
实践
auth.controller.ts
注册登陆的controller实现
1 | import { Body, Controller, Post } from '@nestjs/common'; |
user.service.ts
注册登陆及JWT的service实现
1 | import { HttpException, HttpStatus, Injectable, NotFoundException } from '@nestjs/common'; |
jwt-auth.guard.ts
守卫实现,拦截accessToken和refreshToken,过期进行续签。
1 | import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common'; |
jwt.strategy.ts
JWT解析后进行校验
1 |
|