【sentinel】授权规则详解及源码分析

Sentinel 的授权规则主要用于定义资源的访问权限。它可以基于调用者的来源、调用方式、请求参数等进行规则配置,以实现对资源访问的控制。

授权规则主要包括以下几种类型:

  1. 简单授权规则(SimpleRule):基于调用来源的控制。
  2. 链路规则(AuthorityRule):基于调用链路的控制。
  3. 参数规则(ParamFlowRule):基于请求参数的控制。
  4. quota规则(QPSFlowRule):基于QPS的控制。
  5. 系统规则(SystemRule):系统保护规则,如系统负载、响应时间等。

授权规则的配置方式如下:




List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("some-resource");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(10);
rules.add(rule);
 
AuthorityRule authorityRule = new AuthorityRule();
authorityRule.setResource("some-resource");
authorityRule.setStrategy(new DefaultAuthorityStrategy());
 
ParamFlowRule paramRule = new ParamFlowRule();
paramRule.setResource("some-resource");
paramRule.setParamIdx(0);
paramRule.setGrade(RuleConstant.PARAM_FLOW_GRADE_QPS);
paramRule.setCount(5);
 
// 加载规则
PriorityQueue<FlowRule> flowRules = new PriorityQueue<>(rules);
PriorityQueue<AuthorityRule> authorityRules = new PriorityQueue<>(Collections.singleton(authorityRule));
PriorityQueue<ParamFlowRule> paramRules = new PriorityQueue<>(Collections.singleton(paramRule));
 
// 应用规则
FlowRuleManager.loadRules(flowRules);
AuthorityRuleManager.loadRules(authorityRules);
ParamFlowRuleManager.loadRules(paramRules);

以上代码展示了如何配置不同类型的授权规则,并加载到对应的规则管理器中。

在源码层面,授权规则的加载和管理通常在AuthorityRuleManagerFlowRuleManagerParamFlowRuleManager等类中实现。这些类会维护一个规则集合,并提供加载、更新和检查规则的方法。规则的检查过程通常涉及到对调用者的来源、目标资源、请求参数等的匹配和权限判断。

具体到源码层面,授权规则的加载和检查过程涉及到多个类和方法,例如AuthorityRuleManagerloadRulescheck方法,FlowRuleManagerloadRulescheckFlow方法,以及ParamFlowRuleManagerloadRulescheck方法等。这些方法通常会在系统启动时加载规则配置,并在资源调用时被相应的拦截器调用以确定是否允许资源的访问。

最后修改于:2024年09月04日 10:04

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日