RBAC权限模型(Role-Based Access Control)

前面主要介绍了元数据管理和业务数据的处理,通常一个系统都会有多个用户,不同用户具有不同的权限,本文主要介绍基于RBAC动态权限管理在crudapi中的实现。

概要

RBAC简介

RBAC权限模型(Role-Based Access Control)即:基于角色的权限控制。模型中有几个关键的术语:

用户:系统接口及访问的操作者

权限:能够访问某接口或者做某操作的授权资格

角色:具有一类相同操作权限的用户的总称

用户角色权限关系

一个用户有一个或多个角色

一个角色包含多个用户

一个角色有多种权限

一个权限属于多个角色

Spring security

Spring Security是Spring项目组中用来提供安全认证服务的框架,可以很方便的实现动态权限管理。

表单配置

系统内置5个表单,这些表单和权限相关,和具体业务无关

资源resource



其中url是ANT格式表达式,用于配置url来确定是否拥有某个资源的权限。

用户user



用户表记录登录用户信息

角色role



角色

用户角色行userRoleLine



用户和角色的中间表,参考之前表关系管理,利用两个一对多建立多对多关系,

角色资源行roleResourceLine



角色和资源的中间表,同样的利用两个一对多建立多对多关系

表关系

原表 目标表 关系
user userRoleLine 一对多
userRoleLine role 多对一
role roleResourceLine 一对多
roleResourceLine resource 多对一

权限控制原理

根据登录用户首选获取角色列表,每个角色对应多个资源,最终用户的权限为多个角色对应的资源叠加。如果拥有某个资源权限就返回数据,否则提示无权限。

默认如果没有匹配任何资源,表示该资源无需特别权限,只需要登录用户即可。

验证



添加客户资源,ANT url为/api/business/customer/**,操作为*,表示GET,PATCH,DELETE,POST都需要授权。

如果操作为DELETE,表示值控制DELETE操作,其它操作不限制。



通过UI访问客户时候提示没有权限,和期望的效果一致



添加角色“客户管理员”,该角色拥有客户访问权限



给“超级管理员”添加“客户管理员”角色,这样“超级管理员”就拥有了客户访问权限



因为用户重新分配了角色,需要需要注销重新登录,登录之后又可以正常访问客户资源了。

核心源码

@Slf4j
@Component
public class DynamicSecurityMetadataSource implements FilterInvocationSecurityMetadataSource { private static Map<String, ConfigAttribute> configAttributeMap = null; @Autowired
private DynamicSecurityService dynamicSecurityService; @PostConstruct
public void loadDataSource() {
configAttributeMap = dynamicSecurityService.loadDataSource();
} public void clearDataSource() {
log.info("DynamicSecurityMetadataSource clearDataSource");
configAttributeMap.clear();
configAttributeMap = null;
} @Override
public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
if (configAttributeMap == null) {
this.loadDataSource();
}
List<ConfigAttribute> configAttributes = new ArrayList<>(); FilterInvocation fi = (FilterInvocation) o; String method = fi.getRequest().getMethod();
log.info("getAttributes method = " + method); //获取当前访问的路径
String url = fi.getRequestUrl();
String path = URLUtil.getPath(url) + "_"+ method; log.info("getAttributes url = " + url);
log.info("getAttributes path = " + path); PathMatcher pathMatcher = new AntPathMatcher();
Iterator<String> iterator = configAttributeMap.keySet().iterator();
//获取访问该路径所需资源
while (iterator.hasNext()) {
String pattern = iterator.next();
if (pathMatcher.match(pattern, path)) {
log.info("match success = " + pattern + ", " + path);
configAttributes.add(configAttributeMap.get(pattern));
}
}
// 未设置操作请求权限,返回空集合
return configAttributes;
} @Override
public Collection<ConfigAttribute> getAllConfigAttributes() {
return null;
} @Override
public boolean supports(Class<?> aClass) {
return true;
} }

继承FilterInvocationSecurityMetadataSource,实现getAttributes接口,通过http url加http method进行匹配

@Slf4j
@Component
public class DynamicAccessDecisionManager implements AccessDecisionManager { @Override
public void decide(Authentication authentication, Object object,
Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
// 当接口未被配置资源时直接放行
if (CollUtil.isEmpty(configAttributes)) {
log.info("empty configAttributes decide passed!");
return;
} FilterInvocation fi = (FilterInvocation) object; String method = fi.getRequest().getMethod();
log.info("decide method = " + method); List<String> needAuthorityList = new ArrayList<String>(); Iterator<ConfigAttribute> iterator = configAttributes.iterator();
while (iterator.hasNext()) {
ConfigAttribute configAttribute = iterator.next();
//将访问所需资源或用户拥有资源进行比对
String needAuthority = configAttribute.getAttribute();
needAuthorityList.add(needAuthority);
for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
if (needAuthority.trim().equals(grantedAuthority.getAuthority())) {
return;
}
}
}
throw new AccessDeniedException("对不起,您没有资源:" + String.join(",", needAuthorityList) +"的访问权限!");
} @Override
public boolean supports(ConfigAttribute configAttribute) {
return true;
} @Override
public boolean supports(Class<?> aClass) {
return true;
}
}

继承AccessDecisionManager,实现decide接口,将访问所需资源或用户拥有资源进行比对,如果拥有权限则放行,否则提示无权限。

小结

本文介绍了RBAC在crudapi中的实现原理,首先引入Spring security框架,然后利用配置生成用户,角色,资源等表单,通过配置实现基本的CRUD功能,最终实现了动态权限精细化管理。因为用户,角色等表与业务无关,所以会作为系统内置表单。

附demo演示

本系统属于产品级的零代码平台,不同于自动代码生成器,不需要生成Controller、Service、Repository、Entity等业务代码,程序运行起来就可以使用,真正0代码,可以覆盖基本的和业务无关的CRUD RESTful API。

官网地址:https://crudapi.cn

测试地址:https://demo.crudapi.cn/crudapi/login

基于角色访问控制RBAC权限模型的动态资源访问权限管理实现的更多相关文章

  1. Kubernetes角色访问控制RBAC和权限规则(Role+ClusterRole)

    文章转载自:https://blog.csdn.net/BigData_Mining/article/details/88849696 基于角色的访问控制(Role-Based Access Cont ...

  2. 基于角色访问控制的OA系统的设计与实现

    摘要:随着电子政务的快速发展和全面普及,办公自动化(OA)系统的安全性显得越来越重要.对基于Web 的B/S 结构的OA 系统结构和安全需求进行了分析,为了增强用户身份鉴别和授权控制的安全性,分析了基 ...

  3. Kubernetes RBAC授权普通用户对命名空间访问权限

    Kubernetes RBAC授权普通用户对命名空间访问权限 官方文档:https://www.cnblogs.com/xiangsikai/p/11413970.html kind: Role ap ...

  4. Chapter6_访问权限控制_类的访问权限

    在Java中,访问权限修饰词也可以用于确定库中的哪些类对于该库的使用者是可用的,类既不可以是private也不可以是protected的,对于类的访问权限,只有两种选择:包访问权限或者public.下 ...

  5. SpringMVC之八:基于SpringMVC拦截器和注解实现controller中访问权限控制

    SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...

  6. 基于SpringMVC拦截器和注解实现controller中访问权限控制

    SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...

  7. SpringMVC-开启静态资源访问权限

    1.配置 <mvc:resources mapping="/js/**" location="/js/"/> mapping:代表js目录下的所有文 ...

  8. SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建

    SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...

  9. RBAC (基于角色的访问控制)

    基于角色的访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而 ...

随机推荐

  1. 【转】Dockerfile

    1. 关于docker build  docker build可以基于Dockerfile和context打包出一个镜像,其中context是一系列在PATH或URL中指定的位置中的文件(contex ...

  2. K8S(08)交付实战-交付jenkins到k8s集群

    k8s交付实战-交付jenkins到k8s集群 目录 k8s交付实战-交付jenkins到k8s集群 1 准备jenkins镜像 1.1 下载官方镜像 1.2 修改官方镜像 1.2.1 创建目录 1. ...

  3. 【原创】Linux虚拟化KVM-Qemu分析(九)之virtio设备

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...

  4. 1009E Intercity Travelling 【数学期望】

    题目:戳这里 题意:从0走到n,难度分别为a1~an,可以在任何地方休息,每次休息难度将重置为a1开始.求总难度的数学期望. 解题思路: 跟这题很像,利用期望的可加性,我们分析每个位置的状态,不管怎么 ...

  5. 微服务架构Day04-SpringBoot之web开发

    引入项目 把html页面放在模板引擎文件夹templates下,这样能使用模板引擎的功能. 登录页面国际化 国际化:编写国际化配置文件 1.编写国际化配置文件,抽取页面需要显示的国际化消息 2.Spr ...

  6. 易漏的PEP8语法规范

    import后定义函数的话, 两个空白行. 函数参数内'='左右不加空格. 在写函数说明时, 三引号得是三个双引号.

  7. js optional chaining operator

    js optional chaining operator js 可选链 可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效. ?. 操作符的功能类似于 ...

  8. Github access token

    Github access token https://github.com/settings/tokens https://docs.github.com/en/free-pro-team@late ...

  9. Fetch & POST

    Fetch & POST fetch( `http://10.1.5.202/deploy/http/send/viewtree`, { method: "POST", m ...

  10. TweenMax & GSAP & SVG Animation

    TweenMax & GSAP & SVG Animation svg animation https://greensock.com/tweenmax GSAP https://ww ...