gaodandan vor 7 Jahren
Ursprung
Commit
d6a14e212a

+ 9 - 0
pom.xml

@@ -83,6 +83,15 @@
83 83
 			<version>3.9</version>
84 84
 		</dependency>
85 85
 		
86
+		<dependency>
87
+			<groupId>org.springframework.boot</groupId>
88
+			<artifactId>spring-boot-starter-data-redis</artifactId>
89
+		</dependency>
90
+		<dependency>  
91
+	        <groupId>org.springframework.session</groupId>  
92
+	        <artifactId>spring-session-data-redis</artifactId>  
93
+		</dependency>
94
+		
86 95
 		<!-- 阿里巴巴json -->
87 96
 		<dependency>
88 97
 		    <groupId>com.alibaba</groupId>

+ 154 - 0
src/main/java/com/chinaitop/depot/DatePermissionInterceptor.java

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

+ 44 - 0
src/main/java/com/chinaitop/depot/utils/CustomFilter.java

@@ -0,0 +1,44 @@
1
+package com.chinaitop.depot.utils;
2
+
3
+import java.io.IOException;
4
+
5
+import javax.servlet.Filter;
6
+import javax.servlet.FilterChain;
7
+import javax.servlet.FilterConfig;
8
+import javax.servlet.ServletException;
9
+import javax.servlet.ServletRequest;
10
+import javax.servlet.ServletResponse;
11
+import javax.servlet.http.HttpServletRequest;
12
+import javax.servlet.http.HttpSession;
13
+
14
+import org.apache.commons.lang.ObjectUtils;
15
+
16
+public class CustomFilter implements Filter {
17
+
18
+	@Override
19
+    public void init(FilterConfig filterConfig) throws ServletException {
20
+ 
21
+    }
22
+	
23
+	@Override
24
+	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
25
+			throws IOException, ServletException {
26
+		HttpServletRequest request = (HttpServletRequest) arg0;
27
+		HttpSession session = request.getSession();
28
+		String orgId = ObjectUtils.toString(request.getParameter("pOrgId"));
29
+		if (orgId.isEmpty() && session != null){
30
+			orgId = ObjectUtils.toString(session.getAttribute("depotId"),"");
31
+		}
32
+		if (!orgId.isEmpty()) {
33
+			DataPolicyEngine.set(orgId);
34
+		}
35
+		arg2.doFilter(arg0, arg1);
36
+		DataPolicyEngine.remove();
37
+	}
38
+	
39
+	@Override
40
+    public void destroy() {
41
+ 
42
+    }
43
+
44
+}

+ 61 - 0
src/main/java/com/chinaitop/depot/utils/DataPolicyEngine.java

@@ -0,0 +1,61 @@
1
+package com.chinaitop.depot.utils;
2
+
3
+import org.apache.commons.logging.Log;
4
+import org.apache.commons.logging.LogFactory;
5
+
6
+
7
+/**
8
+ * 
9
+ * @author hf
10
+ * 
11
+ * @description 数据策略(数据权限控制使用)
12
+ * 
13
+ * 
14
+ */
15
+public class DataPolicyEngine {
16
+	//log信息输出对象
17
+	protected static final Log log = LogFactory.getLog(DataPolicyEngine.class); 
18
+
19
+	//本地线程,用于存储线程公共对象
20
+	private static ThreadLocal threadLocalSession = new ThreadLocal();
21
+	
22
+	
23
+	/**
24
+	 * @description 获取本地执行线程
25
+	 *
26
+	 * @return ThreadLocal 
27
+	 * 
28
+	 */
29
+	public static ThreadLocal getThreadLocalSession() {
30
+		return threadLocalSession;
31
+	}
32
+	
33
+	/**
34
+	 * @description 在线程内设置存储对象
35
+	 * 
36
+	 */
37
+	public static void set(Object obj) {
38
+		threadLocalSession.set(obj);
39
+
40
+	}
41
+	
42
+	/**
43
+	 * @description 获取在线程内设置存储的对象
44
+	 * 
45
+	 */
46
+	public static Object get() {
47
+		return threadLocalSession.get();
48
+
49
+	}
50
+
51
+	/**
52
+	 * @description 移除在线程内设置存储的对象
53
+	 * 
54
+	 */
55
+	public static void remove() {
56
+		threadLocalSession.remove();
57
+
58
+	}
59
+	
60
+	
61
+}

+ 66 - 0
src/main/java/com/chinaitop/depot/utils/RedisConfig.java

@@ -0,0 +1,66 @@
1
+package com.chinaitop.depot.utils;
2
+
3
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
4
+import com.fasterxml.jackson.annotation.PropertyAccessor;
5
+import com.fasterxml.jackson.databind.ObjectMapper;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.context.annotation.Bean;
8
+import org.springframework.context.annotation.Configuration;
9
+import org.springframework.data.redis.connection.RedisConnectionFactory;
10
+import org.springframework.data.redis.core.HashOperations;
11
+import org.springframework.data.redis.core.ListOperations;
12
+import org.springframework.data.redis.core.RedisTemplate;
13
+import org.springframework.data.redis.core.SetOperations;
14
+import org.springframework.data.redis.core.ValueOperations;
15
+import org.springframework.data.redis.core.ZSetOperations;
16
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
17
+import org.springframework.data.redis.serializer.StringRedisSerializer;
18
+
19
+@Configuration
20
+public class RedisConfig {
21
+    @Autowired
22
+    private RedisConnectionFactory factory;
23
+
24
+    @Bean
25
+    public RedisTemplate<String, Object> redisTemplate() {
26
+        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
27
+        ObjectMapper om = new ObjectMapper();
28
+        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
29
+        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
30
+        jackson2JsonRedisSerializer.setObjectMapper(om);
31
+        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
32
+        template.setConnectionFactory(factory);
33
+        template.setKeySerializer(new StringRedisSerializer());
34
+        template.setValueSerializer(jackson2JsonRedisSerializer);
35
+        template.setHashKeySerializer(jackson2JsonRedisSerializer);
36
+        template.setHashValueSerializer(jackson2JsonRedisSerializer);
37
+        template.setDefaultSerializer(new StringRedisSerializer());
38
+        template.afterPropertiesSet();
39
+        return template;
40
+    }
41
+
42
+    @Bean
43
+    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
44
+        return redisTemplate.opsForHash();
45
+    }
46
+
47
+    @Bean
48
+    public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
49
+        return redisTemplate.opsForValue();
50
+    }
51
+
52
+    @Bean
53
+    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
54
+        return redisTemplate.opsForList();
55
+    }
56
+
57
+    @Bean
58
+    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
59
+        return redisTemplate.opsForSet();
60
+    }
61
+
62
+    @Bean
63
+    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
64
+        return redisTemplate.opsForZSet();
65
+    }
66
+}

+ 547 - 0
src/main/java/com/chinaitop/depot/utils/RedisUtil.java

@@ -0,0 +1,547 @@
1
+package com.chinaitop.depot.utils;
2
+
3
+import java.util.List;
4
+import java.util.Map;
5
+import java.util.Set;
6
+import java.util.concurrent.TimeUnit;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.data.redis.core.HashOperations;
9
+import org.springframework.data.redis.core.ListOperations;
10
+import org.springframework.data.redis.core.RedisTemplate;
11
+import org.springframework.data.redis.core.SetOperations;
12
+import org.springframework.data.redis.core.ValueOperations;
13
+import org.springframework.stereotype.Component;
14
+import org.springframework.util.CollectionUtils;
15
+
16
+@Component
17
+public class RedisUtil {
18
+
19
+    @Autowired
20
+    private RedisTemplate<String, Object> redisTemplate;
21
+    @Autowired
22
+    private ValueOperations<String, String> valueOperations;
23
+    @Autowired
24
+    private HashOperations<String, String, Object> hashOperations;
25
+    @Autowired
26
+    private ListOperations<String, Object> listOperations;
27
+    @Autowired
28
+    private SetOperations<String, Object> setOperations;
29
+
30
+    //=============================common============================
31
+    /**
32
+     * 指定缓存失效时间
33
+     * @param key 键
34
+     * @param time 时间(秒)
35
+     * @return
36
+     */
37
+    public boolean expire(String key,long time){
38
+        try {
39
+            if(time>0){
40
+                redisTemplate.expire(key, time, TimeUnit.SECONDS);
41
+            }
42
+            return true;
43
+        } catch (Exception e) {
44
+            e.printStackTrace();
45
+            return false;
46
+        }
47
+    }
48
+
49
+    /**
50
+     * 根据key 获取过期时间
51
+     * @param key 键 不能为null
52
+     * @return 时间(秒) 返回0代表为永久有效
53
+     */
54
+    public long getExpire(String key){
55
+        return redisTemplate.getExpire(key,TimeUnit.SECONDS);
56
+    }
57
+
58
+    /**
59
+     * 判断key是否存在
60
+     * @param key 键
61
+     * @return true 存在 false不存在
62
+     */
63
+    public boolean hasKey(String key){
64
+        try {
65
+            return redisTemplate.hasKey(key);
66
+        } catch (Exception e) {
67
+            e.printStackTrace();
68
+            return false;
69
+        }
70
+    }
71
+
72
+    /**
73
+     * 删除缓存
74
+     * @param key 可以传一个值 或多个
75
+     */
76
+    @SuppressWarnings("unchecked")
77
+    public void del(String ... key){
78
+        if(key!=null&&key.length>0){
79
+            if(key.length==1){
80
+                redisTemplate.delete(key[0]);
81
+            }else{
82
+                redisTemplate.delete(CollectionUtils.arrayToList(key));
83
+            }
84
+        }
85
+    }
86
+
87
+    //============================String=============================
88
+    /**
89
+     * 普通缓存获取
90
+     * @param key 键
91
+     * @return 值
92
+     */
93
+    public Object get(String key){
94
+        return key==null?null:valueOperations.get(key);
95
+    }
96
+
97
+    /**
98
+     * 普通缓存放入
99
+     * @param key 键
100
+     * @param value 值
101
+     * @return true成功 false失败
102
+     */
103
+    public boolean set(String key,String value) {
104
+        try {
105
+            valueOperations.set(key, value);
106
+            return true;
107
+        } catch (Exception e) {
108
+            e.printStackTrace();
109
+            return false;
110
+        }
111
+
112
+    }
113
+
114
+    /**
115
+     * 普通缓存放入并设置时间
116
+     * @param key 键
117
+     * @param value 值
118
+     * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
119
+     * @return true成功 false 失败
120
+     */
121
+    public boolean set(String key,String value,long time){
122
+        try {
123
+            if(time>0){
124
+                valueOperations.set(key, value, time, TimeUnit.SECONDS);
125
+            }else{
126
+                set(key, value);
127
+            }
128
+            return true;
129
+        } catch (Exception e) {
130
+            e.printStackTrace();
131
+            return false;
132
+        }
133
+    }
134
+
135
+    /**
136
+     * 递增
137
+     * @param key 键
138
+     * @param by 要增加几(大于0)
139
+     * @return
140
+     */
141
+    public long incr(String key, long delta){
142
+        if(delta<0){
143
+            throw new RuntimeException("递增因子必须大于0");
144
+        }
145
+        return valueOperations.increment(key, delta);
146
+    }
147
+
148
+    /**
149
+     * 递减
150
+     * @param key 键
151
+     * @param by 要减少几(小于0)
152
+     * @return
153
+     */
154
+    public long decr(String key, long delta){
155
+        if(delta<0){
156
+            throw new RuntimeException("递减因子必须大于0");
157
+        }
158
+        return valueOperations.increment(key, -delta);
159
+    }
160
+
161
+    //================================Map=================================
162
+    /**
163
+     * HashGet
164
+     * @param key 键 不能为null
165
+     * @param item 项 不能为null
166
+     * @return 值
167
+     */
168
+    public Object hget(String key,String item){
169
+        return hashOperations.get(key, item);
170
+    }
171
+
172
+    /**
173
+     * 获取hashKey对应的所有键值
174
+     * @param key 键
175
+     * @return 对应的多个键值
176
+     */
177
+    public Map<String, Object> hmget(String key){
178
+        return hashOperations.entries(key);
179
+    }
180
+
181
+    /**
182
+     * HashSet
183
+     * @param key 键
184
+     * @param map 对应多个键值
185
+     * @return true 成功 false 失败
186
+     */
187
+    public boolean hmset(String key, Map<String,Object> map){
188
+        try {
189
+            hashOperations.putAll(key, map);
190
+            return true;
191
+        } catch (Exception e) {
192
+            e.printStackTrace();
193
+            return false;
194
+        }
195
+    }
196
+
197
+    /**
198
+     * HashSet 并设置时间
199
+     * @param key 键
200
+     * @param map 对应多个键值
201
+     * @param time 时间(秒)
202
+     * @return true成功 false失败
203
+     */
204
+    public boolean hmset(String key, Map<String,Object> map, long time){
205
+        try {
206
+            hashOperations.putAll(key, map);
207
+            if(time>0){
208
+                expire(key, time);
209
+            }
210
+            return true;
211
+        } catch (Exception e) {
212
+            e.printStackTrace();
213
+            return false;
214
+        }
215
+    }
216
+
217
+    /**
218
+     * 向一张hash表中放入数据,如果不存在将创建
219
+     * @param key 键
220
+     * @param item 项
221
+     * @param value 值
222
+     * @return true 成功 false失败
223
+     */
224
+    public boolean hset(String key,String item,Object value) {
225
+        try {
226
+            hashOperations.put(key, item, value);
227
+            return true;
228
+        } catch (Exception e) {
229
+            e.printStackTrace();
230
+            return false;
231
+        }
232
+    }
233
+
234
+    /**
235
+     * 向一张hash表中放入数据,如果不存在将创建
236
+     * @param key 键
237
+     * @param item 项
238
+     * @param value 值
239
+     * @param time 时间(秒)  注意:如果已存在的hash表有时间,这里将会替换原有的时间
240
+     * @return true 成功 false失败
241
+     */
242
+    public boolean hset(String key,String item,Object value,long time) {
243
+        try {
244
+            hashOperations.put(key, item, value);
245
+            if(time>0){
246
+                expire(key, time);
247
+            }
248
+            return true;
249
+        } catch (Exception e) {
250
+            e.printStackTrace();
251
+            return false;
252
+        }
253
+    }
254
+
255
+    /**
256
+     * 删除hash表中的值
257
+     * @param key 键 不能为null
258
+     * @param item 项 可以使多个 不能为null
259
+     */
260
+    public void hdel(String key, Object... item){
261
+        hashOperations.delete(key,item);
262
+    }
263
+
264
+    /**
265
+     * 判断hash表中是否有该项的值
266
+     * @param key 键 不能为null
267
+     * @param item 项 不能为null
268
+     * @return true 存在 false不存在
269
+     */
270
+    public boolean hHasKey(String key, String item){
271
+        return hashOperations.hasKey(key, item);
272
+    }
273
+
274
+    /**
275
+     * hash递增 如果不存在,就会创建一个 并把新增后的值返回
276
+     * @param key 键
277
+     * @param item 项
278
+     * @param by 要增加几(大于0)
279
+     * @return
280
+     */
281
+    public double hincr(String key, String item,double by){
282
+        return hashOperations.increment(key, item, by);
283
+    }
284
+
285
+    /**
286
+     * hash递减
287
+     * @param key 键
288
+     * @param item 项
289
+     * @param by 要减少记(小于0)
290
+     * @return
291
+     */
292
+    public double hdecr(String key, String item,double by){
293
+        return hashOperations.increment(key, item,-by);
294
+    }
295
+
296
+    //============================set=============================
297
+    /**
298
+     * 根据key获取Set中的所有值
299
+     * @param key 键
300
+     * @return
301
+     */
302
+    public Set<Object> sGet(String key){
303
+        try {
304
+            return setOperations.members(key);
305
+        } catch (Exception e) {
306
+            e.printStackTrace();
307
+            return null;
308
+        }
309
+    }
310
+
311
+    /**
312
+     * 根据value从一个set中查询,是否存在
313
+     * @param key 键
314
+     * @param value 值
315
+     * @return true 存在 false不存在
316
+     */
317
+    public boolean sHasKey(String key,Object value){
318
+        try {
319
+            return setOperations.isMember(key, value);
320
+        } catch (Exception e) {
321
+            e.printStackTrace();
322
+            return false;
323
+        }
324
+    }
325
+
326
+    /**
327
+     * 将数据放入set缓存
328
+     * @param key 键
329
+     * @param values 值 可以是多个
330
+     * @return 成功个数
331
+     */
332
+    public long sSet(String key, Object...values) {
333
+        try {
334
+            return setOperations.add(key, values);
335
+        } catch (Exception e) {
336
+            e.printStackTrace();
337
+            return 0;
338
+        }
339
+    }
340
+
341
+    /**
342
+     * 将set数据放入缓存
343
+     * @param key 键
344
+     * @param time 时间(秒)
345
+     * @param values 值 可以是多个
346
+     * @return 成功个数
347
+     */
348
+    public long sSetAndTime(String key,long time,Object...values) {
349
+        try {
350
+            Long count = setOperations.add(key, values);
351
+            if(time>0) expire(key, time);
352
+            return count;
353
+        } catch (Exception e) {
354
+            e.printStackTrace();
355
+            return 0;
356
+        }
357
+    }
358
+
359
+    /**
360
+     * 获取set缓存的长度
361
+     * @param key 键
362
+     * @return
363
+     */
364
+    public long sGetSetSize(String key){
365
+        try {
366
+            return setOperations.size(key);
367
+        } catch (Exception e) {
368
+            e.printStackTrace();
369
+            return 0;
370
+        }
371
+    }
372
+
373
+    /**
374
+     * 移除值为value的
375
+     * @param key 键
376
+     * @param values 值 可以是多个
377
+     * @return 移除的个数
378
+     */
379
+    public long setRemove(String key, Object ...values) {
380
+        try {
381
+            Long count = setOperations.remove(key, values);
382
+            return count;
383
+        } catch (Exception e) {
384
+            e.printStackTrace();
385
+            return 0;
386
+        }
387
+    }
388
+    //===============================list=================================
389
+
390
+    /**
391
+     * 获取list缓存的内容
392
+     * @param key 键
393
+     * @param start 开始
394
+     * @param end 结束  0 到 -1代表所有值
395
+     * @return
396
+     */
397
+    public List<Object> lGet(String key, long start, long end){
398
+        try {
399
+            return listOperations.range(key, start, end);
400
+        } catch (Exception e) {
401
+            e.printStackTrace();
402
+            return null;
403
+        }
404
+    }
405
+
406
+    /**
407
+     * 获取list缓存的所有内容
408
+     * @param key
409
+     * @return
410
+     */
411
+    public List<Object> lGetAll(String key){
412
+        return lGet(key,0,-1);
413
+    }
414
+
415
+    /**
416
+     * 获取list缓存的长度
417
+     * @param key 键
418
+     * @return
419
+     */
420
+    public long lGetListSize(String key){
421
+        try {
422
+            return listOperations.size(key);
423
+        } catch (Exception e) {
424
+            e.printStackTrace();
425
+            return 0;
426
+        }
427
+    }
428
+
429
+    /**
430
+     * 通过索引 获取list中的值
431
+     * @param key 键
432
+     * @param index 索引  index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
433
+     * @return
434
+     */
435
+    public Object lGetIndex(String key,long index){
436
+        try {
437
+            return listOperations.index(key, index);
438
+        } catch (Exception e) {
439
+            e.printStackTrace();
440
+            return null;
441
+        }
442
+    }
443
+
444
+    /**
445
+     * 将list放入缓存
446
+     * @param key 键
447
+     * @param value 值
448
+     * @param time 时间(秒)
449
+     * @return
450
+     */
451
+    public boolean lSet(String key, Object value) {
452
+        try {
453
+            listOperations.rightPush(key, value);
454
+            return true;
455
+        } catch (Exception e) {
456
+            e.printStackTrace();
457
+            return false;
458
+        }
459
+    }
460
+
461
+    /**
462
+     * 将list放入缓存
463
+     * @param key 键
464
+     * @param value 值
465
+     * @param time 时间(秒)
466
+     * @return
467
+     */
468
+    public boolean lSet(String key, Object value, long time) {
469
+        try {
470
+            listOperations.rightPush(key, value);
471
+            if (time > 0) expire(key, time);
472
+            return true;
473
+        } catch (Exception e) {
474
+            e.printStackTrace();
475
+            return false;
476
+        }
477
+    }
478
+
479
+    /**
480
+     * 将list放入缓存
481
+     * @param key 键
482
+     * @param value 值
483
+     * @param time 时间(秒)
484
+     * @return
485
+     */
486
+    public boolean lSet(String key, List<Object> value) {
487
+        try {
488
+            listOperations.rightPushAll(key, value);
489
+            return true;
490
+        } catch (Exception e) {
491
+            e.printStackTrace();
492
+            return false;
493
+        }
494
+    }
495
+
496
+    /**
497
+     * 将list放入缓存
498
+     * @param key 键
499
+     * @param value 值
500
+     * @param time 时间(秒)
501
+     * @return
502
+     */
503
+    public boolean lSet(String key, List<Object> value, long time) {
504
+        try {
505
+            listOperations.rightPushAll(key, value);
506
+            if (time > 0) expire(key, time);
507
+            return true;
508
+        } catch (Exception e) {
509
+            e.printStackTrace();
510
+            return false;
511
+        }
512
+    }
513
+
514
+    /**
515
+     * 根据索引修改list中的某条数据
516
+     * @param key 键
517
+     * @param index 索引
518
+     * @param value 值
519
+     * @return
520
+     */
521
+    public boolean lUpdateIndex(String key, long index,Object value) {
522
+        try {
523
+            listOperations.set(key, index, value);
524
+            return true;
525
+        } catch (Exception e) {
526
+            e.printStackTrace();
527
+            return false;
528
+        }
529
+    }
530
+
531
+    /**
532
+     * 移除N个值为value
533
+     * @param key 键
534
+     * @param count 移除多少个
535
+     * @param value 值
536
+     * @return 移除的个数
537
+     */
538
+    public long lRemove(String key,long count,Object value) {
539
+        try {
540
+            Long remove = listOperations.remove(key, count, value);
541
+            return remove;
542
+        } catch (Exception e) {
543
+            e.printStackTrace();
544
+            return 0;
545
+        }
546
+    }
547
+}

+ 39 - 0
src/main/java/com/chinaitop/depot/utils/WebConfig.java

@@ -0,0 +1,39 @@
1
+package com.chinaitop.depot.utils;
2
+
3
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
4
+import org.springframework.context.annotation.Bean;
5
+import org.springframework.context.annotation.Configuration;
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+
9
+/**
10
+ * @desc 注册bean, 将自定义过滤器添加到过滤器链中
11
+ * @Author hf
12
+ * @date 2019/3/11
13
+ * @return
14
+ */
15
+@Configuration
16
+public class WebConfig {
17
+
18
+   /**
19
+    * 注册过滤器,有两种方式:
20
+    * 1) 使用 @Component 注解<br>
21
+    * 2) 添加到过滤器链中,此方式适用于使用第三方的过滤器。将过滤器写到 WebConfig 类中,如下:
22
+    */
23
+   @Bean
24
+   public FilterRegistrationBean filterRegistrationBean() {
25
+
26
+      FilterRegistrationBean registrationBean = new FilterRegistrationBean();
27
+
28
+      CustomFilter filter = new CustomFilter();
29
+      registrationBean.setFilter(filter);
30
+
31
+      //设置过滤器拦截请求
32
+      List<String> urls = new ArrayList<>();
33
+      urls.add("/*");
34
+      registrationBean.setUrlPatterns(urls);
35
+
36
+      return registrationBean;
37
+   }
38
+
39
+}