基于角色访问控制RBAC权限模型的动态资源访问权限管理实现

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

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

概要

RBAC简介

RBAC权限模型(Role-Based Access Control)即:基于角色的权限控制。模型中有几个关键的术语:
用户:系统接口及访问的操做者
权限:可以访问某接口或者作某操做的受权资格
角色:具备一类相同操做权限的用户的总称 api

用户角色权限关系

一个用户有一个或多个角色
一个角色包含多个用户
一个角色有多种权限
一个权限属于多个角色 安全

Spring security

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

表单配置

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

资源resource

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

用户user

user
用户表记录登陆用户信息this

角色role

role
角色url

用户角色行userRoleLine

userRoleLine
用户和角色的中间表,参考以前表关系管理,利用两个一对多创建多对多关系,3d

角色资源行roleResourceLine

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

表关系

relation

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

权限控制原理

根据登陆用户首选获取角色列表,每一个角色对应多个资源,最终用户的权限为多个角色对应的资源叠加。若是拥有某个资源权限就返回数据,不然提示无权限。
默认若是没有匹配任何资源,表示该资源无需特别权限,只须要登陆用户便可。

验证

customerResource
添加客户资源,ANT url为/api/business/customer/*,操做为,表示GET,PATCH,DELETE,POST都须要受权。
若是操做为DELETE,表示值控制DELETE操做,其它操做不限制。

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

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

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

customerOK
由于用户从新分配了角色,须要须要注销从新登陆,登陆以后又能够正常访问客户资源了。

核心源码

@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

相关文章
相关标签/搜索