Hyperf:自定义注解、切面实现权限校验
2022/9/11 4:23:10
本文主要是介绍Hyperf:自定义注解、切面实现权限校验,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
概要
权限控制
权限控制:无论是基于角色,还是根据实际需求开发的基于部门、岗位、员工的系统访问权限控制,最终都是当用户登录系统时或登录后访问某项功能或菜单时基于设定的规则去查验是否能够正常访问。
实现权限控制的方式有很多种。最常见的是通过一个中间件类拦截请求,获取到请求的Path,然后从提前放入缓存或实时查询的权限规则中查找是否存在这个Path,如果存在则放行,反之则无权限访问。
今天介绍的是一种通过自定义注解、切面实现的权限校验的方法。
原理介绍
项目在启动时会扫描项目文件,把相关文件加载入缓存中,例如配置文件、各种注解:@Inject、@Listener、@Controller等等。
具体实现
自定义注解
创建一个注解类,这个要继承AbstractAnnotation这个抽象类,它实现了一下方法(先不用关心):
红色部分:是收集注解的方法。绿色部分是绑定参数用的。
自定义的注解类:
- @Annotation 表示这是一个注解类;
- @Target 表示注解作用域 ,有如下参数:
- METHOD 注解允许定义在类方法上
- PROPERTY 注解允许定义在类属性上
- CLASS 注解允许定义在类上
- ALL 注解允许定义在任何地方
注解使用
/** * @Controller(prefix="/index") */ class IndexController { /** * @GetMapping("show") * @PreAuthorization(value="index:show") */ public function show() { return "show"; } }
当项目启动时@PreAuthorization注解信息就会注入,value值就是权限标识,后续校验时使用。
当访问 http://localhost/index/show 时通过切面去做权限校验。
自定义切面
/** * @Aspect */ #[Aspect] class PreAuthorizationAspect extends AbstractAspect { protected ContainerInterface $container; protected RequestInterface $request; protected Logger $logger; // 要切入的注解,具体切入的还是使用了这些注解的类,仅可切入类注解和类方法注解 public $annotations = [ PreAuthorization::class ]; public function __construct(ContainerInterface $container, RequestInterface $request, Logger $logger) { $this->container = $container; $this->request = $request; $this->logger = $logger; } public function process(ProceedingJoinPoint $proceedingJoinPoint) { // 切面切入后,执行对应的方法会由此来负责 try { $authorization = $this->getAuthorizationAnnotation($proceedingJoinPoint->className, $proceedingJoinPoint->methodName); if (!$this->checkPermission($authorization->value)) { throw new BusinessException(ErrorCode::AUTH_INVALID, ErrorCode::getMessage(ErrorCode::AUTH_INVALID)); } } catch (AnnotationException | NotFoundExceptionInterface | ContainerExceptionInterface $e) { $this->logger->info("PreAuthorizationAspect 执行过程异常:%s", [ 'code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ]); } try { return $proceedingJoinPoint->process(); } catch (Exception $e) { $this->logger->info("PreAuthorizationAspect 执行过程异常:%s", [ 'code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ]); } } /** * desc: 获取注解类 * @param string $className * @param string $method * @return PreAuthorization * @throws AnnotationException */ protected function getAuthorizationAnnotation(string $className, string $method): PreAuthorization { $annotation = AnnotationCollector::getClassMethodAnnotation($className, $method)[PreAuthorization::class] ?? null; if (!$annotation instanceof PreAuthorization) { throw new AnnotationException("Annotation PreAuthorization couldn't be collected successfully."); } return $annotation; } /** * desc: 校验操作权限 * @param string $annotationValue * @return bool * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function checkPermission(string $annotationValue): bool { $token = UserAuth::instance()->getToken(); $cache = di()->get(Redis::class)->get(CacheConstant::USER_AUTH_KEY.$token); $cache = json_decode($cache, true); // 判断是不是超级管理员 if (SysUser::isSuperAdmin($cache["userId"])) { return true; } // 判断注解中的value值是否在其中 $hasPerms = $cache["perms"]; if (in_array($annotationValue, $hasPerms)) { return true; } return false; } }
这篇关于Hyperf:自定义注解、切面实现权限校验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行