如何检查Nestjs Guard中的角色
我有一个提供JWT令牌的外部服务。在Nestjs中,我首先有JWTGuard类:
@Injectable()
export class JwtGuard extends AuthGuard('JWT_STRATEGY') {
constructor() {
super();
}
getRequest(context: ExecutionContext) {
console.log('JwtGuard');
const ctx = GqlExecutionContext.create(context);
return ctx.getContext().req;
}
}
然后是护照策略:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'JWT_STRATEGY') {
constructor(private configService: ConfigService) {
super({
secretOrKeyProvider: passportJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: configService.get<string>('ADFS_KEYS_URL'),
}),
ignoreExpiration: false,
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
audience: configService.get<string>('ADFS_AUDIENCE'),
issuer: configService.get<string>('ADFS_ISSUER'),
algorithms: ['RS256'],
});
}
validate(payload: unknown): unknown {
console.log('jwt strategy');
console.log(payload);
return payload;
}
}
JWTGuard似乎首先运行,然后是策略。但是,如果我想做额外的警卫和检查,请说角色。一个人在哪里做?我需要在护照策略之后的另一个后卫吗?我有两个角色“用户”和“ admin”。
I have an external service providing a JWT token. In Nestjs i first have JwtGuard class:
@Injectable()
export class JwtGuard extends AuthGuard('JWT_STRATEGY') {
constructor() {
super();
}
getRequest(context: ExecutionContext) {
console.log('JwtGuard');
const ctx = GqlExecutionContext.create(context);
return ctx.getContext().req;
}
}
and then a passport strategy:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'JWT_STRATEGY') {
constructor(private configService: ConfigService) {
super({
secretOrKeyProvider: passportJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: configService.get<string>('ADFS_KEYS_URL'),
}),
ignoreExpiration: false,
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
audience: configService.get<string>('ADFS_AUDIENCE'),
issuer: configService.get<string>('ADFS_ISSUER'),
algorithms: ['RS256'],
});
}
validate(payload: unknown): unknown {
console.log('jwt strategy');
console.log(payload);
return payload;
}
}
It seems that JwtGuard is running first, then the strategy. But if i want to do additional guards and checks, say for roles. Where does one do that? Do i need another guard that runs after the passport strategy? I have two roles "User" and "Admin".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在appModule中定义全局守卫(称为
rolesguard
)如下:然后在
RolesGuard
中我们有以下内容:然后创建您自己的装饰器,您可以装饰您的装饰器API如果您需要该API来根据警卫保护您的应用程序。
然后,您可以在API中使用Decorator:
因此,在API中,如果您没有
family
,则在警卫中,您将不会有sircessuriperroles
, 如果 block 将返回true。更多信息: https://docs.nestjs.com/security/authorization
First of all, define a global guard (called
RolesGuard
) in the AppModule as following:Then within
RolesGuard
we have the following:Then create your own decorator and you can decorate your APIs if you need that API to protect your app based on your guard.
Then you can use your decorator in your API like this:
So, in your API, if you won't have
FamilyRoles
, in the guard you won't haverequiredRoles
, and theif
block will return true.More info: https://docs.nestjs.com/security/authorization