bound 提供可选的 UC 鉴权能力,使用 nestjs 的 守卫 来实现,一般用于判断客户端是否有权限调用某个接口。
整个鉴权流程如下:
- 客户端调用 uc 的相关方法,如 getAuthHeader 获取 Authorization,将 Authorization 和 sdp-app-id 放入请求头中,发送请求。
- 服务端接收请求信息,调用 uc 的校验接口,判断是否有请求权限。
- 鉴权成功则放行,继续走真正的请求;鉴权失败则返回失败原因。
具体实现,请看 src/guard/auth.guard.ts 。
使用
详细使用方法,请参考 绑定守卫。
以下是模块级别和方法级别的使用示例:
// 模块级别守卫
import { Controller, UseGuards } from '@nestjs/common'
import { AuthGuard } from 'path/to/auth.guard'
@Controller('user')
@UseGuards(AuthGuard)
export class UserController {}
// 方法级别守卫
import { Controller, UseGuards, Get } from '@nestjs/common'
import { AuthGuard } from 'path/to/auth.guard'
@Controller('user')
export class UserController {
@UseGuards(AuthGuaard)
@Get('/')
async get() {}
}