estJS:添加验证选项AuthGuard与智威汤逊
estJS:添加验证选项AuthGuard与智威汤逊
我想利用AuthGuard装饰,以及护照JWT战略,documentation以下。
在文档中一切都很正常。但是我现在想保护与包含在JWT一个范围的路由。因此,这里是我的应用程序生成的基本JWT有效载荷:
{
"user": {
"id": "c4f-118-4216-8b48-ddb825de8",
estJS:添加验证选项AuthGuard与智威汤逊
我想利用AuthGuard
装饰,以及护照JWT战略,documentation以下。
在文档中一切都很正常。但是我现在想保护与包含在JWT一个范围的路由。因此,这里是我的应用程序生成的基本JWT有效载荷:
{
"user": {
"id": "20189c4f-118-4216-8b48-ddb825de8",
"username": "[email protected]"
},
"scope": [
"manage_server"
],
"iat": 154766258,
"exp": 154771258,
"iss": "15f246d-8810-44f9-a908-801872ded159",
"sub": "20189c4f-118-4216-8b48-ddb825de8",
"jti": "078047bc-fc1f-4c5-8abe-7284f7bcc44"
}
这里是由AuthGuard
装饰被把守的基本路线的保护:
@Get('protected')
@UseGuards(AuthGuard('jwt'))
async protected(): Promise<string> {
return 'Hello Protected World';
}
我想补充的选择和限制路线,以具有manager_server
范围到他们的智威汤逊的人访问。所以读完AuthGuard
码的一点点后,我认为我能写的东西,如:
@Get('protected')
@UseGuards(AuthGuard('jwt', {
scope: 'manage_server'
}))
async protected(): Promise<string> {
return 'Hello Protected World';
}
不过,我不能在文档中看到,我可以利用这个选项。
我认为增加一个选项参数的validate
的JWTStrategy
功能可以使的伎俩,但事实并非如此。这里是我的validate
功能(包含在jwt.
文件):
async validate(payload: JwtPayload, done: ((err: any, value: any) => void)) {
ct user = await this.authService.validateUser(payload);
if (!user) {
return done(new UnauthorizedException(), false);
}
done(null, user);
}
非常感谢您的帮助,不要犹豫,问我要更多的信息的评论,如果你需要这样。
回答如下:当你看AuthGuard的code,这似乎是功能是唯一可能的定制。
我想,不是写自己AuthGuard
支持范围检查,它是清洁剂有ScopesGuard
(或RolesGuard
)有自己的decorater像@Scopes('manage_server')
代替。对于这一点,你可以遵循RolesGuard
,也只检查在请求中docs属性下的智威汤逊有效载荷的属性user
例子。
基本步骤
创建@Scopes()
装饰:
export ct Scopes = (...scopes: string[]) => ReflectMetadata('scopes', scopes);
创建ScopesGuard
:
@Injectable()
export class ScopesGuard implements CanActivate {
ctructor(private readonly reflector: Reflector) {}
canActivate(context: ExecutionContext): boolean {
ct scopes = this.reflector.get<string[]>('scopes', context.getHandler());
if (!scopes) {
return true;
}
ct request = context.switchToHttp().getRequest();
ct user = request.user;
ct hasScope = () => user.scopes.some((scope) => scopes.includes(scope));
return user && user.scopes && hasScope();
}
}
使用ScopesGuard作为所有路线的全局后卫(没有给出范围时返回true):
@Module({
providers: [
{
provide: APP_GUARD,
useClass: ScopesGuard,
},
],
})
export class ApplicationModule {}
然后用它在端点上:
@Get('protected')
@UseGuards(AuthGuard('jwt'))
@Scopes('manage_server')
async protected(): Promise<string> {
return 'Hello Protected World';
}
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2024-04-14 22:00:26
下一篇:基质金属蛋白酶MMP
推荐阅读
留言与评论(共有 18 条评论) |
本站网友 重庆妈妈网 | 22分钟前 发表 |
以具有manager_server范围到他们的智威汤逊的人访问 | |
本站网友 药品销售公司 | 25分钟前 发表 |
{ scope | |
本站网友 vs2005下载 | 22分钟前 发表 |
[ { provide | |
本站网友 八个月宝宝食谱 | 0秒前 发表 |
Promise<string> { return 'Hello Protected World'; } 不过 | |
本站网友 中国城市竞争力蓝皮书 | 10分钟前 发表 |
也只检查在请求中docs属性下的智威汤逊有效载荷的属性user例子 | |
本站网友 李林娟 | 1分钟前 发表 |
我认为增加一个选项参数的validate的JWTStrategy功能可以使的伎俩 | |
本站网友 新会楼市 | 27分钟前 发表 |
"iat" | |
本站网友 中工网 | 7分钟前 发表 |
Promise<string> { return 'Hello Protected World'; } | |
本站网友 颈椎病治疗 | 20分钟前 发表 |
ScopesGuard | |
本站网友 股票与债券的区别 | 10分钟前 发表 |
{ "id" | |
本站网友 美的总部大楼 | 14分钟前 发表 |
value | |
本站网友 龙港四小 | 29分钟前 发表 |
因此 | |
本站网友 上海老年大学 | 22分钟前 发表 |
false); } done(null | |
本站网友 黄宝强 | 22分钟前 发表 |
any | |
本站网友 好看的韩国电影 | 26分钟前 发表 |
Promise<string> { return 'Hello Protected World'; } 不过 | |
本站网友 首地大峡谷保利影院 | 2分钟前 发表 |
"iss" | |
本站网友 格力空调售后 | 6分钟前 发表 |
我认为我能写的东西 |