|
|
@@ -1,152 +0,0 @@
|
|
1
|
|
-package com.chinaitop.depot;
|
|
2
|
|
-
|
|
3
|
|
-import com.chinaitop.depot.utils.DataPolicyEngine;
|
|
4
|
|
-import com.chinaitop.depot.utils.RedisUtil;
|
|
5
|
|
-import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
6
|
|
-import net.sf.jsqlparser.statement.Statement;
|
|
7
|
|
-import net.sf.jsqlparser.statement.select.Select;
|
|
8
|
|
-import net.sf.jsqlparser.util.TablesNamesFinder;
|
|
9
|
|
-import org.apache.commons.lang.ObjectUtils;
|
|
10
|
|
-import org.apache.ibatis.executor.statement.StatementHandler;
|
|
11
|
|
-import org.apache.ibatis.mapping.BoundSql;
|
|
12
|
|
-import org.apache.ibatis.mapping.MappedStatement;
|
|
13
|
|
-import org.apache.ibatis.plugin.*;
|
|
14
|
|
-import org.apache.ibatis.reflection.DefaultReflectorFactory;
|
|
15
|
|
-import org.apache.ibatis.reflection.MetaObject;
|
|
16
|
|
-import org.apache.ibatis.reflection.ReflectorFactory;
|
|
17
|
|
-import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
|
|
18
|
|
-import org.apache.ibatis.reflection.factory.ObjectFactory;
|
|
19
|
|
-import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
|
|
20
|
|
-import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
|
|
21
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
22
|
|
-import org.springframework.stereotype.Component;
|
|
23
|
|
-
|
|
24
|
|
-import java.lang.reflect.Field;
|
|
25
|
|
-import java.sql.Connection;
|
|
26
|
|
-import java.util.HashSet;
|
|
27
|
|
-import java.util.List;
|
|
28
|
|
-import java.util.Properties;
|
|
29
|
|
-import java.util.Set;
|
|
30
|
|
-
|
|
31
|
|
-/**
|
|
32
|
|
- * Sql执行时间记录拦截器
|
|
33
|
|
- */
|
|
34
|
|
-@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class })})
|
|
35
|
|
-@Component
|
|
36
|
|
-public class DatePermissionInterceptor implements Interceptor {
|
|
37
|
|
-
|
|
38
|
|
- private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory();
|
|
39
|
|
- private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory();
|
|
40
|
|
- private static final ReflectorFactory DEFAULT_REFLECTOR_FACTORY = new DefaultReflectorFactory();
|
|
41
|
|
-
|
|
42
|
|
- @Autowired
|
|
43
|
|
- private RedisUtil redisUtil;
|
|
44
|
|
-
|
|
45
|
|
- @Override
|
|
46
|
|
- public Object intercept(Invocation invocation) throws Throwable {
|
|
47
|
|
-
|
|
48
|
|
- StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
|
|
49
|
|
- MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY,
|
|
50
|
|
- DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
|
|
51
|
|
-
|
|
52
|
|
- MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
|
|
53
|
|
-
|
|
54
|
|
- final BoundSql boundSql = statementHandler.getBoundSql();
|
|
55
|
|
- String bouString = boundSql.getSql();// 获取执行sql
|
|
56
|
|
-
|
|
57
|
|
- //sql是查询的才进行权限过滤
|
|
58
|
|
- if(mappedStatement.getSqlCommandType().toString().equals("select".toUpperCase())) {
|
|
59
|
|
-
|
|
60
|
|
- //通过本地线程获取库id
|
|
61
|
|
- Object threadLocalObj = DataPolicyEngine.get();
|
|
62
|
|
- String orgId = null;
|
|
63
|
|
- if(threadLocalObj != null) {
|
|
64
|
|
- orgId = ObjectUtils.toString(threadLocalObj);
|
|
65
|
|
- }
|
|
66
|
|
- if(orgId!=null) {
|
|
67
|
|
- //解析sql中的表名
|
|
68
|
|
- Statement statement = CCJSqlParserUtil.parse(bouString);
|
|
69
|
|
- Select selectStatement = (Select)statement;
|
|
70
|
|
- TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
|
|
71
|
|
- List<String> result = tablesNamesFinder.getTableList(selectStatement);
|
|
72
|
|
-
|
|
73
|
|
- Set<String> tableNames = new HashSet<String>();// 定义需要替换的table信息列表
|
|
74
|
|
- //获取业务表信息数据
|
|
75
|
|
- List<String> tableList = (List<String>)redisUtil.lGetAll("tableList").get(0);
|
|
76
|
|
- for(int i=0;i<result.size();i++) {
|
|
77
|
|
- for (int j = 0; j < tableList.size(); j++) {// 处理查看sql中是否有业务表
|
|
78
|
|
- if (result.get(i).equals(ObjectUtils.toString(tableList.get(j)))) {// 是否存在业务表
|
|
79
|
|
- tableNames.add(result.get(i));
|
|
80
|
|
- }
|
|
81
|
|
- }
|
|
82
|
|
- }
|
|
83
|
|
-
|
|
84
|
|
- if (tableNames != null && tableNames.size() > 0) {// sql中存在业务表进行sql拼接数据重新检索
|
|
85
|
|
- if(result.size() >= 2) {//sql中存在2张及以上表
|
|
86
|
|
- for (String table : tableNames) {// 进行业务表的数据权限替换
|
|
87
|
|
- table += " ";//表名后面加空格,防止两个表名一部分相似,导致替换出错
|
|
88
|
|
- bouString = bouString.replaceAll(table, "(SELECT * from " + table
|
|
89
|
|
- + " WHERE org_id = '"+orgId+"') ");
|
|
90
|
|
- }
|
|
91
|
|
- }else {//sql中是单表
|
|
92
|
|
- for (String table : tableNames) {// 进行业务表的数据权限替换
|
|
93
|
|
- if(bouString.contains(table+" ")) {//防止是单表查询,表后面没有空格的时候替换不了
|
|
94
|
|
- table += " ";//表名后面加空格,防止两个表名一部分相似,导致替换出错
|
|
95
|
|
- }
|
|
96
|
|
- bouString = bouString.replaceAll(table, "(SELECT * from " + table
|
|
97
|
|
- + " WHERE org_id = '"+orgId+"') "+table+" ");
|
|
98
|
|
- }
|
|
99
|
|
- }
|
|
100
|
|
-
|
|
101
|
|
- /*bouString = bouString.replaceAll(table, "(SELECT b.* from " + table
|
|
102
|
|
- + " b,user_business u WHERE b.org_id = u.org_id AND u .user_id ='"+userId+"' AND u.business_type = '"+table+"') ");*/
|
|
103
|
|
-
|
|
104
|
|
- }
|
|
105
|
|
-
|
|
106
|
|
- //metaStatementHandler.setValue("delegate.boundSql.sql", bouString);
|
|
107
|
|
-
|
|
108
|
|
- Field field = getField(boundSql, "sql");
|
|
109
|
|
- if (field != null) {
|
|
110
|
|
- try {
|
|
111
|
|
- field.setAccessible(true);
|
|
112
|
|
- field.set(boundSql, bouString);
|
|
113
|
|
- } catch (IllegalArgumentException e) {
|
|
114
|
|
- // TODO Auto-generated catch block
|
|
115
|
|
- e.printStackTrace();
|
|
116
|
|
- } catch (IllegalAccessException e) {
|
|
117
|
|
- // TODO Auto-generated catch block
|
|
118
|
|
- e.printStackTrace();
|
|
119
|
|
- }
|
|
120
|
|
- }
|
|
121
|
|
- }
|
|
122
|
|
-
|
|
123
|
|
- }
|
|
124
|
|
-
|
|
125
|
|
- return invocation.proceed();
|
|
126
|
|
-
|
|
127
|
|
-
|
|
128
|
|
- }
|
|
129
|
|
-
|
|
130
|
|
- private static Field getField(Object obj, String fieldName) {
|
|
131
|
|
- Field field = null;
|
|
132
|
|
- for (Class<?> clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
|
|
133
|
|
- try {
|
|
134
|
|
- field = clazz.getDeclaredField(fieldName);
|
|
135
|
|
- break;
|
|
136
|
|
- } catch (NoSuchFieldException e) {
|
|
137
|
|
- // 这里不用做处理,子类没有该字段可能对应的父类有,都没有就返回null。
|
|
138
|
|
- }
|
|
139
|
|
- }
|
|
140
|
|
- return field;
|
|
141
|
|
- }
|
|
142
|
|
-
|
|
143
|
|
- @Override
|
|
144
|
|
- public Object plugin(Object target) {
|
|
145
|
|
- return Plugin.wrap(target, this);
|
|
146
|
|
- }
|
|
147
|
|
-
|
|
148
|
|
- @Override
|
|
149
|
|
- public void setProperties(Properties properties) {
|
|
150
|
|
- }
|
|
151
|
|
-
|
|
152
|
|
-}
|