同步操作将从 天爱有情/tianai-ram 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
该权限框架是一个灵活度极高的权限系统,
ram一个权限包含如下几个元素
元素名称 | 描述 |
---|---|
效果(Effect) | 授权效果包括两种:允许(Allow)和拒绝(Deny) |
操作(Action) | 操作是指对具体资源的操作。 |
资源(Resource) | 资源是指被授权的具体对象。 |
条件(Condition) | 条件是指授权生效的条件。 |
效果(Effect)
取值为:允许(Allow)或拒绝(Deny)。
样例:"Effect": "Allow"
。
操作(Action)
操作支持多值,取值为:云服务所定义的API操作名称。
格式:<ram-code>:<action-name>
。
ram-code
:RAM代码action-name
:相关的API操作接口名称。样例:"Action": ["oss:ListBuckets", "ecs:Describe*", "rds:Describe*"]
。
资源(Resource)
资源是指被授权的具体对象。
样例:"Resource": ["bj:project:*:*:1001", "bj:images:*:*:0002", "bj:file:*:*:mybucket", "bj:oss:*:*:mybucket/*"]
。
条件(Condition)
逻辑说明
CurrentUserStatementProvider
接口,用户查询当前用户的权限描述信息例子:
@Component
public class DemoCurrentUserPermissionProvider implements CurrentUserStatementProvider {
@Override
public List<Statement> getCurrentUserStatementList(MethodInvocation invocation) {
Statement statement = new Statement();
// 拒绝 zb:project:* 下的资源访问
statement.setEffect(Effect.Deny);
statement.setActionList(Arrays.asList("*"));
statement.setResourceList(Arrays.asList("zb:project:*"));
statement.setConditionList(null);
// 允许 zb:qiyeguanli:qiyexingxi:** zb:zhuanjiaku:zhuanjiachouqu:* 下的资源访问 且条件通过(Condition 条件是自定义的校验逻辑)
Statement statement2 = new Statement();
statement2.setEffect(Effect.Allow);
statement2.setActionList(Arrays.asList("post", "put"));
statement2.setResourceList(Arrays.asList("zb:qiyeguanli:qiyexingxi:**","zb:zhuanjiaku:zhuanjiachouqu:*"));
Condition condition = new Condition();
condition.setOperationType("ip");
condition.setKey("ip");
condition.setValueList(Arrays.asList("127.0.0.1"));
// 拒绝 zb:zhuanjiaku:zhuanjiachouqu:1, zb:zhuanjiaku:zhuanjiachouqu:aa, zb:zhuanjiaku:zhuanjiachouqu:22 这三个资源的 post操作访问
Statement statement3 = new Statement();
statement3.setEffect(Effect.Deny);
statement3.setActionList(Arrays.asList("post"));
statement3.setResourceList(Arrays.asList("zb:zhuanjiaku:zhuanjiachouqu:1", "zb:zhuanjiaku:zhuanjiachouqu:aa", "zb:zhuanjiaku:zhuanjiachouqu:22"));
statement3.setConditionList(null);
List<Statement> statementList = new ArrayList<>();
statementList.add(statement2);
statementList.add(statement);
statementList.add(statement3);
// 排序
DefaultStatementComparator.INSTANCE.sort(statementList);
return statementList;
}
}
例子:
@Action(value = "zb:", action = "11", description = "招标项目:")
@RestController
public class DemoController {
@Autowired
private PermissionHandler permissionHandler;
@Autowired
private ActionHolder actionHolder;
@GetMapping("/test/{id}")
@Action(resource = "zhuanjiaku:zhuanjiachouqu:{#id}", action = "put", description = "专家抽取:详情")
public String test(@PathVariable String id) {
return "success";
}
@PostMapping("/test2/{aa}")
@Action(resource = "qiyeguanli:qiyexingxi:{#aa}:{#d.id}", action = "put", hitThrowError = false, description = "企业管理:企业信息:详情")
public String test2(@PathVariable String aa, @RequestBody Body d) {
return "success2";
}
@GetMapping("/get")
public Collection<Action> test3() {
return actionHolder.actionList();
}
@Data
public static class Body {
private String id;
private String name;
}
}
@Action注解属性详解
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Action {
@AliasFor("resource")
String value() default "";
/** 资源名称. */
@AliasFor("value")
String resource() default "";
/** 描述. */
String description() default "";
/** 是否跳过校验. */
boolean skip() default false;
/** action. */
String action() default "exec";
/** 是否开启自动校验, 如果为true会使用springAOP进行自动校验. */
boolean autoMatch() default true;
/** 如果开启自动校验 并且校验过是否抛出异常. */
boolean hitThrowError() default true;
}
RamHitException
异常,可以自定义异常拦截器拦截该异常进行权限校验处理,如果不想抛出异常可以 在 @Action中将属性 hitThrowError
设置为falseRamContext
类RamContext
为扩展类,可以在代码中通过RamContext
获取当前校验的一些数据,比如当前校验是否通过 ,当前的Action数据等等,也就是说,如果想自定义校验,而不用自带的Aop校验,可以使用RamContext
获取一些权限信息,也可以设置一些数据给Aop拦截器
ActionHolder
类ActionHolder
用于读取和解析Action, 默认启动的时候会扫描所有带有 @Action
注解的方法,可以自定义添加或删除
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。