Commit bd77431b by levphon

共享数据拦截器

1 parent 54b4049e
......@@ -9,7 +9,8 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>glsx-ne-shield-auth-client</artifactId>
<artifactId>ne-shield-auth-client</artifactId>
<version>1.1.0-SNAPSHOT</version>
<dependencies>
<!-- feign -->
......
......@@ -3,6 +3,7 @@ package cn.com.glsx.auth.interceptor;
import cn.com.glsx.admin.common.constant.UserConstants;
import cn.com.glsx.auth.utils.OriginSqlHolder;
import cn.com.glsx.auth.utils.ShieldContextHolder;
import com.glsx.plat.common.annotation.DataOnlyShared;
import com.glsx.plat.common.annotation.DataPerm;
import com.glsx.plat.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
......@@ -75,17 +76,28 @@ public class DataPermissionInterceptor implements Interceptor {
return invocation.proceed();
}
//没自定义注解直接执行
DataPerm dataAuth = getDataPerm(mappedStatement);
if (dataAuth == null) {
//超级管理员/全部数据权限 不控制数据权限
if (ShieldContextHolder.isSuperAdmin() || UserConstants.RolePermitCastType.all.getCode().equals(ShieldContextHolder.getRolePermissionType())) {
return invocation.proceed();
}
//超级管理员/全部数据权限 不控制数据权限
if (ShieldContextHolder.isSuperAdmin() || UserConstants.RolePermitCastType.all.getCode().equals(ShieldContextHolder.getRolePermissionType())) {
Method method = getMapperExecutingMethod(mappedStatement);
if (method == null) {
throw new Exception("Mybatis拦截器执行异常");
}
//如果方法有共享数据注解,直接跳过
if (method.isAnnotationPresent(DataOnlyShared.class)) {
return invocation.proceed();
}
//没自定义注解直接执行
if (!method.isAnnotationPresent(DataPerm.class)) {
return invocation.proceed();
}
DataPerm dataAuth = method.getAnnotation(DataPerm.class);
//拼装sql
String originSql = boundSql.getSql(); //获取到当前需要被执行的SQL
......@@ -126,25 +138,23 @@ public class DataPermissionInterceptor implements Interceptor {
}
/**
* 获取方法上的数据权限注解
* 获取当前mapper执行方法
*
* @param mappedStatement
* @return
* @throws ClassNotFoundException
*/
private DataPerm getDataPerm(MappedStatement mappedStatement) throws ClassNotFoundException {
DataPerm dataPerm = null;
private Method getMapperExecutingMethod(MappedStatement mappedStatement) throws ClassNotFoundException {
String id = mappedStatement.getId();
String className = id.substring(0, id.lastIndexOf("."));
String methodName = id.substring(id.lastIndexOf(".") + 1);
final Method[] method = Class.forName(className).getDeclaredMethods();
for (Method me : method) {
if (me.getName().equals(methodName) && me.isAnnotationPresent(DataPerm.class)) {
dataPerm = me.getAnnotation(DataPerm.class);
break;
if (me.getName().equals(methodName)) {
return me;
}
}
return dataPerm;
return null;
}
/**
......
......@@ -4,6 +4,7 @@ import cn.com.glsx.admin.common.constant.UserConstants;
import cn.com.glsx.auth.utils.OriginSqlHolder;
import cn.com.glsx.auth.utils.ShieldContextHolder;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.glsx.plat.common.annotation.DataOnlyShared;
import com.glsx.plat.common.annotation.DataPermShare;
import com.glsx.plat.common.utils.StringUtils;
import com.google.common.collect.Lists;
......@@ -82,17 +83,29 @@ public class DataPermitLinkInterceptor implements Interceptor {
return invocation.proceed();
}
//没自定义注解直接按通过
DataPermShare dataAuth = getAnnotation(mappedStatement);
if (dataAuth == null) {
//超级管理员/全部数据权限 不控制数据权限
if (ShieldContextHolder.isSuperAdmin() || UserConstants.RolePermitCastType.all.getCode().equals(ShieldContextHolder.getRolePermissionType())) {
return invocation.proceed();
}
//超级管理员/全部数据权限 不控制数据权限
if (ShieldContextHolder.isSuperAdmin() || UserConstants.RolePermitCastType.all.getCode().equals(ShieldContextHolder.getRolePermissionType())) {
Method method = getMapperExecutingMethod(mappedStatement);
if (method == null) {
throw new Exception("Mybatis拦截器执行异常");
}
//如果方法有共享数据注解,直接跳过
if (method.isAnnotationPresent(DataOnlyShared.class)) {
return invocation.proceed();
}
//没自定义注解直接执行
if (!method.isAnnotationPresent(DataPermShare.class)) {
return invocation.proceed();
}
//没自定义注解直接按通过
DataPermShare dataAuth = method.getAnnotation(DataPermShare.class);
//拼装sql
String originSql = OriginSqlHolder.getSql(); //获取到当前需要被执行的原始SQL
String permitSql = boundSql.getSql(); //获取到当前需要被执行的权限SQL
......@@ -322,25 +335,23 @@ public class DataPermitLinkInterceptor implements Interceptor {
}
/**
* 获取方法上的数据权限注解
* 获取当前mapper执行方法
*
* @param mappedStatement
* @return
* @throws ClassNotFoundException
*/
private DataPermShare getAnnotation(MappedStatement mappedStatement) throws ClassNotFoundException {
DataPermShare dataAuth = null;
private Method getMapperExecutingMethod(MappedStatement mappedStatement) throws ClassNotFoundException {
String id = mappedStatement.getId();
String className = id.substring(0, id.lastIndexOf("."));
String methodName = id.substring(id.lastIndexOf(".") + 1);
final Method[] method = Class.forName(className).getDeclaredMethods();
for (Method me : method) {
if (me.getName().equals(methodName) && me.isAnnotationPresent(DataPermShare.class)) {
dataAuth = me.getAnnotation(DataPermShare.class);
break;
if (me.getName().equals(methodName)) {
return me;
}
}
return dataAuth;
return null;
}
private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
......
package cn.com.glsx.auth.listener;
import cn.com.glsx.auth.interceptor.DataOnlySharedInterceptor;
import cn.com.glsx.auth.interceptor.DataPermissionInterceptor;
import cn.com.glsx.auth.interceptor.DataPermitLinkInterceptor;
import lombok.extern.slf4j.Slf4j;
......@@ -31,6 +32,7 @@ public class DataCommandLineRunner implements CommandLineRunner {
sqlSessionFactory.getConfiguration().addInterceptor(new DataPermitLinkInterceptor());
sqlSessionFactory.getConfiguration().addInterceptor(new DataPermissionInterceptor());
sqlSessionFactory.getConfiguration().addInterceptor(new DataOnlySharedInterceptor());
List<Interceptor> interceptorList = sqlSessionFactory.getConfiguration().getInterceptors();
interceptorList.forEach(interceptor -> {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!