insp4j 轻量级权限框架 1.2.0 发布

简介

  • insp4j为inspector的缩写,中文含义检查员。
  • 基于Spring-EL、AOP,更加灵活易用的权限控制框架,支持权限分组,可用于一套系统多种权限隔离校验,使用简单、易于扩展,支持Servlet、Reactive。
  • 参考了SpringSecurity、Expression的设计,定制扩展灵活。

需求背景

一个大的项目里会存在各种子业务系统,比如xx营销平台,提供给B端商户使用,其中包含的子业务系统有:
商城系统(多门店)、仓库系统(多仓库)、运营系统(多部门),系统交给不同的人员进行维护管理,这里统称操作员。
每个操作员能操作的功能,操作的门店/仓库/部门都是不一样的,甚至是不同的用户体系,不同的粗细粒度,
一般我们每个子业务系统会给不同的开发小组开发,如果没有设计统一的权限处理方案,各个小组会各自实现自己所负责的业务系统权限,很难管理。

insp4j就是抽象了一个group,把每个不同的子系统的权限设计区分隔离,不同的group,不同的用户、操作权限、数据权限,这些信息统一封装在InspAuthentication,需要业务系统来构造。

insp4j没有实现用户认证、授权,更没涉及到数据库层面上的数据范围过滤,只对业务系统构造的InspAuthentication、@Insp注解上定义的权限标识基于AOP实现拦截校验,是轻量级的权限控制实现。

快速开始

  • 导包
<!-- Servlet ->
<dependency>
    <groupId>cn.is4j.insp</groupId>
    <artifactId>insp4j-web-spring-boot-starter</artifactId>
    <version>${latest.version}</version>
</dependency>
<!-- Reactive ->
<dependency>
    <groupId>cn.is4j.insp</groupId>
    <artifactId>insp4j-reactive-spring-boot-starter</artifactId>
    <version>${latest.version}</version>
</dependency>
  • 配置
/** * 实现接口cn.is4j.insp.web.service.InspWebAuthenticationService返回认证用户的权限 并交由spring ioc管理 */
@Component
public class InspWebAuthenticationServiceImpl implements InspWebAuthenticationService {

    @Autowired
    private DeptService deptService;
    @Autowired
    private MerchantService merchantService;

    /** * 建议走缓存 每次拦截都会进入该方法 这里只是演示 */
    @Override
    public InspAuthentication loadAuthentication(HttpServletRequest httpServletRequest, InspMetadataSource metadataSource) {
        // groupName可以用来做用户/权限隔离
        // 如系统用户在sys_user表,权限在sys_authorities表,一般用户(商户)在biz_merchant表,权限在biz_merchant_authorities表
        if("system".equals(metadataSource.getGroupName())){
            String userId = SecurityUtil.getUserId();
            List<String> funcAuthorities = SecurityUtil.getUser().getAuthorities();
            List<String> dataAuthorities = deptService.listDeptId();
            return new InspAuthentication(userId, funcAuthorities, dataAuthorities);
        }
        if("merchant".equals(metadataSource.getGroupName())){
            String userId = merchantService.getIdByToken(httpServletRequest.getHeader("token"));
            List<String> funcAuthorities = merchantService.listFuncAuthorities(userId);
            List<String> dataAuthorities = merchantService.listDataAuthorities(userId);
            return new InspAuthentication(userId, funcAuthorities, dataAuthorities);
        }
        throw new RuntimeException("error groupName");
    }
}
  • 使用
@RestController
@RequestMapping("/dept")
public class DeptController {

    //操作权限
    @Insp("hasFunc('dept:list')")
    @GetMapping("/list")
    public R<?> list() {
        return ok();
    }

    // 操作权限加数据权限 有没有操作该id的权限
    // 支持Spring-EL表达式
    @Insp(value = "hasFuncData('dept:update',#id)")
    @GetMapping("/updateById")
    public R<?> updateById(@RequestParam Long id) {
        return ok();
    }
    // 权限分组 业务系统/运营系统 可能用户账号体系不一样,权限体系也是分开设计的,就需要用到groupName来实现分组
    // 一个分组可以单独一套用户/权限
    @Insp(value = "hasFuncData('dept:delete',#id)", groupName = "system")
    @GetMapping("/deleteById")
    public R<?> deleteById(@RequestParam Long id) {
        return ok();
    }

}

v1.2.0

  • feat: 添加CHANGELOG.md文件
  • refactor: insp4j-web项目更名为insp4j-web-spring-boot-starter、insp4j-reactive项目更名为insp4j-reactive-spring-boot-starter

v1.1.3

  • feat: 新增角色权限 hasRole hasAnyRole

v1.1.2

  • fix: 解决InspAuthenticationService依赖的bean无法被正常代理问题(代理失效)

v1.1.1

  • update: 自定义异常处理传递metadataSource

v1.1.0

  • update: 优化InspContext ThreadLocal实现,支持父子线程获取上下文
  • refactor: 重构el表达式,支持el参数传递到InspAuthenticationService,更加灵活控制权限
  • update: 优化ExpressionRoot校验逻辑
  • feat: 支持自定义异常处理bean
  • style: 代码优化

v1.0.1

  • style: 包名规范
  • style: pom scm

v1.0.0

  • feat: 框架设计,初始提交
  • feat: aop支持注解拦截权限校验

猜你喜欢

转载自www.oschina.net/news/123418/insp4j-1-2-0-released