1、实现思路,通过ThreadLocal获取是否开启数据权限,开启则通过mybatis-plus的DataPermissionHandler实现数据权限
controller上添加的注解DataScope.java
package com.pojo.common.datascope.annotation;
import com.pojo.common.core.config.HkhjDataPermissionHandler;
import com.pojo.common.core.utils.DataScopeContext;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 数据权限过滤注解
*
* @author zsj
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataScope {
/**
* 部门数据权限查询列名
*/
String deptId() default DataScopeContext.DEPT_ID;
/**
* 用户数据权限查询列名
*/
String userSelf() default DataScopeContext.USER_SELF;
}
2、aop切面DataScopeAspect.java
package com.pojo.common.datascope.aspect;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.pojo.common.core.config.HkhjDataPermissionHandler;
import com.pojo.common.core.utils.DataScopeContext;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.pojo.common.datascope.annotation.DataScope;
import com.pojo.common.security.service.TokenService;
import com.pojo.system.api.domain.SysUser;
import com.pojo.system.api.model.LoginUser;
/**
* 数据过滤处理
*
* @author ruoyi
*/
@Aspect
@Component
public class DataScopeAspect {
@Autowired
private TokenService tokenService;
// 配置织入点
@Pointcut("@annotation(com.pojo.common.datascope.annotation.DataScope)")
public void dataScopePointCut() {
}
@Before("dataScopePointCut()")
public void doBefore(JoinPoint point) throws Throwable {
handleDataScope(point);
}
protected void handleDataScope(final JoinPoint joinPoint) {
// 获得注解
DataScope controllerDataScope = getAnnotationLog(joinPoint);
Map<String, String> map = Maps.newConcurrentMap();
if (controllerDataScope == null) {
map.put(DataScopeContext.OPEN_DATA_SCOPE, "false");
DataScopeContext.setContextMap(map);
return;
}
// 获取当前的用户
LoginUser loginUser = tokenService.getLoginUser();
if (Objects.nonNull(loginUser)) {
SysUser currentUser = loginUser.getSysUser();
Map<String, String> dataScopeFilter = dataScopeFilter(currentUser);
map.put(DataScopeContext.OPEN_DATA_SCOPE, "true");
map.put(DataScopeContext.DEPT_ID, controllerDataScope.deptId());
map.put(DataScopeContext.USER_SELF, controllerDataScope.userSelf());
map.put(DataScopeContext.USER_NAME, currentUser.getUserName());
map.put(DataScopeContext.DATA_SCOPE_DEPT_ID, dataScopeFilter.getOrDefault(DataScopeContext.DATA_SCOPE_DEPT_ID, "-1"));
map.put(DataScopeContext.DATA_SCOPE, dataScopeFilter.get(DataScopeContext.DATA_SCOPE));
DataScopeContext.setContextMap(map);
}
}
/**
* 数据范围过滤
*
* @param user 用户
*/
public static Map<String, String> dataScopeFilter(SysUser user) {
Set<String> dataScope = Sets.newHashSet();
Map<String, String> result