springboot用户创建的业务数据只能是同一组织能看的见

文章介绍了一个业务场景,其中A部门用户创建的业务单据只能被同部门用户查看。通过在数据库中添加`create_dept`字段记录数据来源,并使用SpringAop进行切面编程,实现在查询时动态过滤数据,确保数据安全。自定义`BranchScope`注解用于标记需进行组织过滤的方法,MyBatis配置中结合注解动态生成SQL,限制查询结果。
摘要由CSDN通过智能技术生成

业务场景需求说明:

    在项目权限框架体系下。A部门用户 :A张三,创建一条业务单据 :业务单据A数据,

    此时呢,业务需求是:【业务单据A数据】只能是 【A部门】下的所有用户能看的见,其他组织无权查看  此条数据

1.数据库设计:

   创建:create_dept 字段,记录数据的来源  组织

 

 2. 使用SpringAop 技术,将 组织数据过滤 与 业务逻辑隔离。。。

package com.AAA.framework.aspectj;

import com.AAA.common.annotation.BranchScope;
import com.AAA.common.core.domain.BaseEntity;
import com.AAA.common.core.domain.entity.SysUser;
import com.AAA.common.core.domain.model.LoginUser;
import com.AAA.common.utils.SecurityUtils;
import com.AAA.common.utils.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 组织数据过滤处理
 *
 * @author wangwei
 */
@Aspect
@Component
public class BranchScopeAspect {

  /** 数据权限过滤关键字 */
  public static final String DATA_SCOPE = "branchScope";

  // 配置织入点
  @Pointcut("@annotation(com.ruoyi.common.annotation.BranchScope)")
  public void branchScopePointCut() {}

  @Before("branchScopePointCut()")
  public void doBefore(JoinPoint point) throws Throwable {
    handleDataScope(point);
  }

  protected void handleDataScope(final JoinPoint joinPoint) {
    // 获得注解
    BranchScope controllerBranchScope = getAnnotationLog(joinPoint);
    if (controllerBranchScope == null) {
      return;
    }
    // 获取当前的用户
    LoginUser loginUser = SecurityUtils.getLoginUser();
    if (StringUtils.isNotNull(loginUser)) {
      SysUser currentUser = loginUser.getUser();
      // 如果是超级管理员,则不过滤数据 && !currentUser.isAdmin()
      if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()) {
        branchScopeFilter(joinPoint, currentUser);
      }
    }
  }

  /**
   * 数据范围过滤
   *
   * @param joinPoint 切点
   * @param user 用户
   */
  public static void branchScopeFilter(JoinPoint joinPoint, SysUser user) {
    Object params = joinPoint.getArgs()[0];
    if (StringUtils.isNotNull(params) && params instanceof BaseEntity) {
      BaseEntity baseEntity = (BaseEntity) params;
      baseEntity
          .getParams()
              .put(
                      DATA_SCOPE,
                      " AND create_dept IN (SELECT dept_id FROM sys_dept WHERE dept_id = '"
                              + user.getDeptId()
                              + "') ");
    }
  }

  /** 是否存在注解,如果存在就获取 */
  private BranchScope getAnnotationLog(JoinPoint joinPoint) {
    Signature signature = joinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    Method method = methodSignature.getMethod();
    if (method != null) {
      return method.getAnnotation(BranchScope.class);
    }
    return null;
  }
}

核心关键点说明:  这里处理  过滤组织数据的  思路是,在查询数据的时候,通过  组织id:dept_id ,查询所有业务单据中  有没有   当前登录组织  创建的单据

完整sql语句:

      SELECT count(0) FROM table_aaa   WHERE create_dept IN (SELECT dept_id FROM sys_dept WHERE dept_id = '103')

3. 详细业务使用:

     自定义注解:

   

package com.AAA.common.annotation;

import java.lang.annotation.*;

/** 组织过滤注解 */

/***
 * wangwei
 * 2023-05-19 09:08:00
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BranchScope {}

3.mybatis中配置

    ${params.branchScope}

    <select id="selectJcryList" parameterType="Jcry" resultMap="JcryResult">
        <include refid="selectJcryVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="pnumber != null  and pnumber != ''"> and pnumber = #{pnumber}</if>
            <if test="deleted != null  and deleted != ''"> and deleted = #{deleted}</if>
            ${params.branchScope}
        </where>
    </select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大锤4391

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值