From 42a2772d1aa7493bcc4fac3e24ee8eda4eebc23d Mon Sep 17 00:00:00 2001
From: xubinbin <1323875150@qq.com>
Date: 星期二, 12 十二月 2023 17:09:04 +0800
Subject: [PATCH] bugfix:请求头带token, SecurityUtils 获取用户id 一直为0 #1195

---
 src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java |  604 ++++--------------------------------------------------
 1 files changed, 47 insertions(+), 557 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java b/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
old mode 100644
new mode 100755
index 0427f8b..101a3b3
--- a/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
+++ b/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
@@ -1,557 +1,47 @@
-package com.genersoft.iot.vmp.utils.redis;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.stereotype.Component;
-import org.springframework.util.CollectionUtils;
-
-/**    
- * @Description:Redis宸ュ叿绫�
- * @author: songww
- * @date:   2020骞�5鏈�6鏃� 涓嬪崍8:27:29     
- */
-@Component
-public class RedisUtil {
-
-	@Autowired
-    private RedisTemplate redisTemplate;
-	
-	/**
-     * 鎸囧畾缂撳瓨澶辨晥鏃堕棿
-     * @param key 閿�
-     * @param time 鏃堕棿锛堢锛�
-     * @return true / false
-     */
-    public boolean expire(String key, long time) {
-        try {
-            if (time > 0) {
-                redisTemplate.expire(key, time, TimeUnit.SECONDS);
-            }
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 鏍规嵁 key 鑾峰彇杩囨湡鏃堕棿
-     * @param key 閿�
-     * @return
-     */
-    public long getExpire(String key) {
-        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
-    }
-
-    /**
-     * 鍒ゆ柇 key 鏄惁瀛樺湪
-     * @param key 閿�
-     * @return true / false
-     */
-    public boolean hasKey(String key) {
-        try {
-            return redisTemplate.hasKey(key);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 鍒犻櫎缂撳瓨
-     * @SuppressWarnings("unchecked") 蹇界暐绫诲瀷杞崲璀﹀憡
-     * @param key 閿紙涓�涓垨鑰呭涓級
-     */
-    public void del(String... key) {
-        if (key != null && key.length > 0) {
-            if (key.length == 1) {
-                redisTemplate.delete(key[0]);
-            } else {
-//                浼犲叆涓�涓� Collection<String> 闆嗗悎
-                redisTemplate.delete(CollectionUtils.arrayToList(key));
-            }
-        }
-    }
-
-//    ============================== String ==============================
-
-    /**
-     * 鏅�氱紦瀛樿幏鍙�
-     * @param key 閿�
-     * @return 鍊�
-     */
-    public Object get(String key) {
-        return key == null ? null : redisTemplate.opsForValue().get(key);
-    }
-
-    /**
-     * 鏅�氱紦瀛樻斁鍏�
-     * @param key 閿�
-     * @param value 鍊�
-     * @return true / false
-     */
-    public boolean set(String key, Object value) {
-        try {
-            redisTemplate.opsForValue().set(key, value);
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 鏅�氱紦瀛樻斁鍏ュ苟璁剧疆鏃堕棿
-     * @param key 閿�
-     * @param value 鍊�
-     * @param time 鏃堕棿锛堢锛夛紝濡傛灉 time < 0 鍒欒缃棤闄愭椂闂�
-     * @return true / false
-     */
-    public boolean set(String key, Object value, long time) {
-        try {
-            if (time > 0) {
-                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
-            } else {
-                set(key, value);
-            }
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 閫掑
-     * @param key 閿�
-     * @param delta 閫掑澶у皬
-     * @return
-     */
-    public long incr(String key, long delta) {
-        if (delta < 0) {
-            throw new RuntimeException("閫掑鍥犲瓙蹇呴』澶т簬 0");
-        }
-        return redisTemplate.opsForValue().increment(key, delta);
-    }
-
-    /**
-     * 閫掑噺
-     * @param key 閿�
-     * @param delta 閫掑噺澶у皬
-     * @return
-     */
-    public long decr(String key, long delta) {
-        if (delta < 0) {
-            throw new RuntimeException("閫掑噺鍥犲瓙蹇呴』澶т簬 0");
-        }
-        return redisTemplate.opsForValue().increment(key, delta);
-    }
-
-//    ============================== Map ==============================
-
-    /**
-     * HashGet
-     * @param key 閿紙no null锛�
-     * @param item 椤癸紙no null锛�
-     * @return 鍊�
-     */
-    public Object hget(String key, String item) {
-        return redisTemplate.opsForHash().get(key, item);
-    }
-
-    /**
-     * 鑾峰彇 key 瀵瑰簲鐨� map
-     * @param key 閿紙no null锛�
-     * @return 瀵瑰簲鐨勫涓敭鍊�
-     */
-    public Map<Object, Object> hmget(String key) {
-        return redisTemplate.opsForHash().entries(key);
-    }
-
-    /**
-     * HashSet
-     * @param key 閿�
-     * @param map 鍊�
-     * @return true / false
-     */
-    public boolean hmset(String key, Map<Object, Object> map) {
-        try {
-            redisTemplate.opsForHash().putAll(key, map);
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * HashSet 骞惰缃椂闂�
-     * @param key 閿�
-     * @param map 鍊�
-     * @param time 鏃堕棿
-     * @return true / false
-     */
-    public boolean hmset(String key, Map<Object, Object> map, long time) {
-        try {
-            redisTemplate.opsForHash().putAll(key, map);
-            if (time > 0) {
-                expire(key, time);
-            }
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 鍚戜竴寮� Hash琛� 涓斁鍏ユ暟鎹紝濡備笉瀛樺湪鍒欏垱寤�
-     * @param key 閿�
-     * @param item 椤�
-     * @param value 鍊�
-     * @return true / false
-     */
-    public boolean hset(String key, String item, Object value) {
-        try {
-            redisTemplate.opsForHash().put(key, item, value);
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 鍚戜竴寮� Hash琛� 涓斁鍏ユ暟鎹紝骞惰缃椂闂达紝濡備笉瀛樺湪鍒欏垱寤�
-     * @param key 閿�
-     * @param item 椤�
-     * @param value 鍊�
-     * @param time 鏃堕棿锛堝鏋滃師鏉ョ殑 Hash琛� 璁剧疆浜嗘椂闂达紝杩欓噷浼氳鐩栵級
-     * @return true / false
-     */
-    public boolean hset(String key, String item, Object value, long time) {
-        try {
-            redisTemplate.opsForHash().put(key, item, value);
-            if (time > 0) {
-                expire(key, time);
-            }
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 鍒犻櫎 Hash琛� 涓殑鍊�
-     * @param key 閿�
-     * @param item 椤癸紙鍙互澶氫釜锛宯o null锛�
-     */
-    public void hdel(String key, Object... item) {
-        redisTemplate.opsForHash().delete(key, item);
-    }
-
-    /**
-     * 鍒ゆ柇 Hash琛� 涓槸鍚︽湁璇ラ敭鐨勫��
-     * @param key 閿紙no null锛�
-     * @param item 鍊硷紙no null锛�
-     * @return true / false
-     */
-    public boolean hHasKey(String key, String item) {
-        return redisTemplate.opsForHash().hasKey(key, item);
-    }
-
-    /**
-     * Hash閫掑锛屽鏋滀笉瀛樺湪鍒欏垱寤轰竴涓紝骞舵妸鏂板鐨勫�艰繑鍥�
-     * @param key 閿�
-     * @param item 椤�
-     * @param by 閫掑澶у皬 > 0
-     * @return
-     */
-    public Double hincr(String key, String item, Double by) {
-        return redisTemplate.opsForHash().increment(key, item, by);
-    }
-
-    /**
-     * Hash閫掑噺
-     * @param key 閿�
-     * @param item 椤�
-     * @param by 閫掑噺澶у皬
-     * @return
-     */
-    public Double hdecr(String key, String item, Double by) {
-        return redisTemplate.opsForHash().increment(key, item, -by);
-    }
-
-//    ============================== Set ==============================
-
-    /**
-     * 鏍规嵁 key 鑾峰彇 set 涓殑鎵�鏈夊��
-     * @param key 閿�
-     * @return 鍊�
-     */
-    public Set<Object> sGet(String key) {
-        try {
-            return redisTemplate.opsForSet().members(key);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return null;
-        }
-    }
-
-    /**
-     * 浠庨敭涓� key 鐨� set 涓紝鏍规嵁 value 鏌ヨ鏄惁瀛樺湪
-     * @param key 閿�
-     * @param value 鍊�
-     * @return true / false
-     */
-    public boolean sHasKey(String key, Object value) {
-        try {
-            return redisTemplate.opsForSet().isMember(key, value);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 灏嗘暟鎹斁鍏� set缂撳瓨
-     * @param key 閿��
-     * @param values 鍊硷紙鍙互澶氫釜锛�
-     * @return 鎴愬姛涓暟
-     */
-    public long sSet(String key, Object... values) {
-        try {
-            return redisTemplate.opsForSet().add(key, values);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return 0;
-        }
-    }
-
-    /**
-     * 灏嗘暟鎹斁鍏� set缂撳瓨锛屽苟璁剧疆鏃堕棿
-     * @param key 閿�
-     * @param time 鏃堕棿
-     * @param values 鍊硷紙鍙互澶氫釜锛�
-     * @return 鎴愬姛鏀惧叆涓暟
-     */
-    public long sSet(String key, long time, Object... values) {
-        try {
-            long count = redisTemplate.opsForSet().add(key, values);
-            if (time > 0) {
-                expire(key, time);
-            }
-            return count;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return 0;
-        }
-    }
-
-    /**
-     * 鑾峰彇 set缂撳瓨鐨勯暱搴�
-     * @param key 閿�
-     * @return 闀垮害
-     */
-    public long sGetSetSize(String key) {
-        try {
-            return redisTemplate.opsForSet().size(key);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return 0;
-        }
-    }
-
-    /**
-     * 绉婚櫎 set缂撳瓨涓紝鍊间负 value 鐨�
-     * @param key 閿�
-     * @param values 鍊�
-     * @return 鎴愬姛绉婚櫎涓暟
-     */
-    public long setRemove(String key, Object... values) {
-        try {
-            return redisTemplate.opsForSet().remove(key, values);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return 0;
-        }
-    }
-
-//    ============================== List ==============================
-
-    /**
-     * 鑾峰彇 list缂撳瓨鐨勫唴瀹�
-     * @param key 閿�
-     * @param start 寮�濮�
-     * @param end 缁撴潫锛�0 鍒� -1 浠h〃鎵�鏈夊�硷級
-     * @return
-     */
-    public List<Object> lGet(String key, long start, long end) {
-        try {
-            return redisTemplate.opsForList().range(key, start, end);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return null;
-        }
-    }
-
-    /**
-     * 鑾峰彇 list缂撳瓨鐨勯暱搴�
-     * @param key 閿�
-     * @return 闀垮害
-     */
-    public long lGetListSize(String key) {
-        try {
-            return redisTemplate.opsForList().size(key);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return 0;
-        }
-    }
-
-    /**
-     * 鏍规嵁绱㈠紩 index 鑾峰彇閿负 key 鐨� list 涓殑鍏冪礌
-     * @param key 閿�
-     * @param index 绱㈠紩
-     *              褰� index >= 0 鏃� {0:琛ㄥご, 1:绗簩涓厓绱爙
-     *              褰� index < 0 鏃� {-1:琛ㄥ熬, -2:鍊掓暟绗簩涓厓绱爙
-     * @return 鍊�
-     */
-    public Object lGetIndex(String key, long index) {
-        try {
-            return redisTemplate.opsForList().index(key, index);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return null;
-        }
-    }
-
-    /**
-     * 灏嗗�� value 鎻掑叆閿负 key 鐨� list 涓紝濡傛灉 list 涓嶅瓨鍦ㄥ垯鍒涘缓绌� list
-     * @param key 閿�
-     * @param value 鍊�
-     * @return true / false
-     */
-    public boolean lSet(String key, Object value) {
-        try {
-            redisTemplate.opsForList().rightPush(key, value);
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 灏嗗�� value 鎻掑叆閿负 key 鐨� list 涓紝骞惰缃椂闂�
-     * @param key 閿�
-     * @param value 鍊�
-     * @param time 鏃堕棿
-     * @return true / false
-     */
-    public boolean lSet(String key, Object value, long time) {
-        try {
-            redisTemplate.opsForList().rightPush(key, value);
-            if (time > 0) {
-                expire(key, time);
-            }
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 灏� values 鎻掑叆閿负 key 鐨� list 涓�
-     * @param key 閿�
-     * @param values 鍊�
-     * @return true / false
-     */
-    public boolean lSetList(String key, List<Object> values) {
-        try {
-            redisTemplate.opsForList().rightPushAll(key, values);
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 灏� values 鎻掑叆閿负 key 鐨� list 涓紝骞惰缃椂闂�
-     * @param key 閿�
-     * @param values 鍊�
-     * @param time 鏃堕棿
-     * @return true / false
-     */
-    public boolean lSetList(String key, List<Object> values, long time) {
-        try {
-            redisTemplate.opsForList().rightPushAll(key, values);
-            if (time > 0) {
-                expire(key, time);
-            }
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 鏍规嵁绱㈠紩 index 淇敼閿负 key 鐨勫��
-     * @param key 閿�
-     * @param index 绱㈠紩
-     * @param value 鍊�
-     * @return true / false
-     */
-    public boolean lUpdateIndex(String key, long index, Object value) {
-        try {
-            redisTemplate.opsForList().set(key, index, value);
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            return false;
-        }
-    }
-
-    /**
-     * 鍦ㄩ敭涓� key 鐨� list 涓垹闄ゅ�间负 value 鐨勫厓绱�
-     * @param key 閿�
-     * @param count 濡傛灉 count == 0 鍒欏垹闄� list 涓墍鏈夊�间负 value 鐨勫厓绱�
-     *              濡傛灉 count > 0 鍒欏垹闄� list 涓渶宸﹁竟閭d釜鍊间负 value 鐨勫厓绱�
-     *              濡傛灉 count < 0 鍒欏垹闄� list 涓渶鍙宠竟閭d釜鍊间负 value 鐨勫厓绱�
-     * @param value
-     * @return
-     */
-    public long lRemove(String key, long count, Object value) {
-        try {
-            return redisTemplate.opsForList().remove(key, count, value);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return 0;
-        }
-    }
-    
-    /**
-     * 妯$硦鏌ヨ
-     * @param key 閿�
-     * @return true / false
-     */
-    public List<Object> keys(String key) {
-        try {
-        	Set<String> set = redisTemplate.keys(key);
-            return new ArrayList<>(set);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return null;
-        }
-    }
-}
+package com.genersoft.iot.vmp.utils.redis;
+
+import com.google.common.collect.Lists;
+import org.springframework.data.redis.core.Cursor;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.ScanOptions;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Redis宸ュ叿绫�
+ *
+ * @author swwheihei
+ * @date 2020骞�5鏈�6鏃� 涓嬪崍8:27:29
+ */
+@SuppressWarnings(value = {"rawtypes", "unchecked"})
+public class RedisUtil {
+
+    /**
+     * 妯$硦鏌ヨ
+     *
+     * @param query 鏌ヨ鍙傛暟
+     * @return
+     */
+    public static List<Object> scan(RedisTemplate redisTemplate, String query) {
+
+        Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
+            ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build();
+            Cursor<byte[]> scan = connection.scan(scanOptions);
+            Set<String> keys = new HashSet<>();
+            while (scan.hasNext()) {
+                byte[] next = scan.next();
+                keys.add(new String(next));
+            }
+            return keys;
+        });
+
+        return Lists.newArrayList(resultKeys);
+    }
+}
+
+
+

--
Gitblit v1.8.0