java项目中数据权限实现思路

一、需求:

同样的页面,不同的账号登录进去,看到的数据不一样。

二、权限管理的方式 RBAC模型

角色与数据权限的关系:

比如管理员角色,他的数据权限是全部,那么拥有该角色的用户,所能看到的数据权限就是全部部门的数据;

比如部门领导的角色,如(朝阳区经信局的部门领导),能看到整个部门以及下属部门的数据;

比如某部门处理专员角色,仅能看到该部门的数据。

三、数据权限实现

3.1 定义数据权限注解

public @interface DataAuthority {

}

3.2 注解的使用

3.2.1 定义基础的类

@Data
public class BaseDeptParam {
    /**
     * 可以查看的部门数据
     */
    private List<Long> deptIds;

}

3.2.2 接口参数继承BaseDeptParam

@Data
public class RequestParam extends BaseDeptParam {

	private String id;

    private String name;

}

在需要数据权限的接口上,加上@DataAuthority的注解

    @DataAuthority
    @GetMapping(value = "/userList")
    public R<PageInfo<UserInfo>> userList(RequestParam param) {
        return taskStatisticsService.handlingList(param);
    }

3.3.3 在mybatis.xml中拼装部门的条件

select * from user b
where b.flag = 0 
  <if test="deptIds != null and deptIds.size()>0">
            AND b.dept_id IN
            <foreach collection="deptIds" open="(" close=")" item="item" index="index" separator=",">
                #{item}
            </foreach>
  </if>

3.2 注解实现类

@Aspect
@Component
public class DataAuthorityService {

    @Pointcut("@annotation(com.xx.xxx.xx.annotation.DataAuthority)")
    public void dataAuthorityPointCut() {
    }

    @Before("dataAuthorityPointCut()")
    public void before(JoinPoint joinPoint) {
        // 根据当前登录的用户id 查询用户的角色,再根据角色id,查询角色管理的部门ids,给deptIds赋值
        Long userId = getUserId();
        List<Long> roleIds = getRoleIdsByUserId();
        List<Long> deptIds = getDeptIdsByRoleIds();
        for (Object arg : args) {
            if (arg instanceof BaseDeptParam) {
                ((BaseDeptParam) arg).setDeptIds(deptIds);
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值