Commit bd77431b by levphon

共享数据拦截器

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