laowo 3 lat temu
rodzic
commit
8c8664fbcc

+ 137 - 137
src/main/java/com/usky/config/redis/JedisConfig.java

@@ -1,137 +1,137 @@
-package com.usky.config.redis;
-
-
-import com.usky.utils.jwt.common.StringUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.PropertySource;
-import redis.clients.jedis.JedisPool;
-import redis.clients.jedis.JedisPoolConfig;
-
-/**
- * Jedis配置,项目启动注入JedisPool
- * http://www.cnblogs.com/GodHeng/p/9301330.html
- * @author laowo
- * @date 2020/9/5 10:35
- */
-@Configuration
-@EnableAutoConfiguration
-@PropertySource("classpath:config.properties")
-@ConfigurationProperties(prefix = "redis")
-public class JedisConfig {
-
-    /**
-     * logger
-     */
-    private static final Logger logger = LoggerFactory.getLogger(JedisConfig.class);
-
-    private String host;
-
-    private int port;
-
-    private String password;
-
-    private int timeout;
-
-    @Value("${redis.pool.max-active}")
-    private int maxActive;
-
-    @Value("${redis.pool.max-wait}")
-    private int maxWait;
-
-    @Value("${redis.pool.max-idle}")
-    private int maxIdle;
-
-    @Value("${redis.pool.min-idle}")
-    private int minIdle;
-
-    @Bean
-    public JedisPool redisPoolFactory() {
-        try {
-            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
-            jedisPoolConfig.setMaxIdle(maxIdle);
-            jedisPoolConfig.setMaxWaitMillis(maxWait);
-            jedisPoolConfig.setMaxTotal(maxActive);
-            jedisPoolConfig.setMinIdle(minIdle);
-            // 密码为空设置为null
-            if (StringUtil.isBlank(password)) {
-                password = null;
-            }
-            JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
-            logger.info("初始化Redis连接池JedisPool成功!地址: {}:{}", host, port);
-            return jedisPool;
-        } catch (Exception e) {
-            logger.error("初始化Redis连接池JedisPool异常:{}", e.getMessage());
-        }
-        return null;
-    }
-
-    public String getHost() {
-        return host;
-    }
-
-    public void setHost(String host) {
-        this.host = host;
-    }
-
-    public int getPort() {
-        return port;
-    }
-
-    public void setPort(int port) {
-        this.port = port;
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    public void setPassword(String password) {
-        this.password = password;
-    }
-
-    public int getTimeout() {
-        return timeout;
-    }
-
-    public void setTimeout(int timeout) {
-        this.timeout = timeout;
-    }
-
-    public int getMaxActive() {
-        return maxActive;
-    }
-
-    public void setMaxActive(int maxActive) {
-        this.maxActive = maxActive;
-    }
-
-    public int getMaxWait() {
-        return maxWait;
-    }
-
-    public void setMaxWait(int maxWait) {
-        this.maxWait = maxWait;
-    }
-
-    public int getMaxIdle() {
-        return maxIdle;
-    }
-
-    public void setMaxIdle(int maxIdle) {
-        this.maxIdle = maxIdle;
-    }
-
-    public int getMinIdle() {
-        return minIdle;
-    }
-
-    public void setMinIdle(int minIdle) {
-        this.minIdle = minIdle;
-    }
-}
+//package com.usky.config.redis;
+//
+//
+//import com.usky.utils.jwt.common.StringUtil;
+//import org.slf4j.Logger;
+//import org.slf4j.LoggerFactory;
+//import org.springframework.beans.factory.annotation.Value;
+//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+//import org.springframework.boot.context.properties.ConfigurationProperties;
+//import org.springframework.context.annotation.Bean;
+//import org.springframework.context.annotation.Configuration;
+//import org.springframework.context.annotation.PropertySource;
+//import redis.clients.jedis.JedisPool;
+//import redis.clients.jedis.JedisPoolConfig;
+//
+///**
+// * Jedis配置,项目启动注入JedisPool
+// * http://www.cnblogs.com/GodHeng/p/9301330.html
+// * @author laowo
+// * @date 2020/9/5 10:35
+// */
+//@Configuration
+//@EnableAutoConfiguration
+//@PropertySource("classpath:config.properties")
+//@ConfigurationProperties(prefix = "redis")
+//public class JedisConfig {
+//
+//    /**
+//     * logger
+//     */
+//    private static final Logger logger = LoggerFactory.getLogger(JedisConfig.class);
+//
+//    private String host;
+//
+//    private int port;
+//
+//    private String password;
+//
+//    private int timeout;
+//
+//    @Value("${redis.pool.max-active}")
+//    private int maxActive;
+//
+//    @Value("${redis.pool.max-wait}")
+//    private int maxWait;
+//
+//    @Value("${redis.pool.max-idle}")
+//    private int maxIdle;
+//
+//    @Value("${redis.pool.min-idle}")
+//    private int minIdle;
+//
+//    @Bean
+//    public JedisPool redisPoolFactory() {
+//        try {
+//            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
+//            jedisPoolConfig.setMaxIdle(maxIdle);
+//            jedisPoolConfig.setMaxWaitMillis(maxWait);
+//            jedisPoolConfig.setMaxTotal(maxActive);
+//            jedisPoolConfig.setMinIdle(minIdle);
+//            // 密码为空设置为null
+//            if (StringUtil.isBlank(password)) {
+//                password = null;
+//            }
+//            JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
+//            logger.info("初始化Redis连接池JedisPool成功!地址: {}:{}", host, port);
+//            return jedisPool;
+//        } catch (Exception e) {
+//            logger.error("初始化Redis连接池JedisPool异常:{}", e.getMessage());
+//        }
+//        return null;
+//    }
+//
+//    public String getHost() {
+//        return host;
+//    }
+//
+//    public void setHost(String host) {
+//        this.host = host;
+//    }
+//
+//    public int getPort() {
+//        return port;
+//    }
+//
+//    public void setPort(int port) {
+//        this.port = port;
+//    }
+//
+//    public String getPassword() {
+//        return password;
+//    }
+//
+//    public void setPassword(String password) {
+//        this.password = password;
+//    }
+//
+//    public int getTimeout() {
+//        return timeout;
+//    }
+//
+//    public void setTimeout(int timeout) {
+//        this.timeout = timeout;
+//    }
+//
+//    public int getMaxActive() {
+//        return maxActive;
+//    }
+//
+//    public void setMaxActive(int maxActive) {
+//        this.maxActive = maxActive;
+//    }
+//
+//    public int getMaxWait() {
+//        return maxWait;
+//    }
+//
+//    public void setMaxWait(int maxWait) {
+//        this.maxWait = maxWait;
+//    }
+//
+//    public int getMaxIdle() {
+//        return maxIdle;
+//    }
+//
+//    public void setMaxIdle(int maxIdle) {
+//        this.maxIdle = maxIdle;
+//    }
+//
+//    public int getMinIdle() {
+//        return minIdle;
+//    }
+//
+//    public void setMinIdle(int minIdle) {
+//        this.minIdle = minIdle;
+//    }
+//}

+ 3 - 3
src/main/java/com/usky/config/shiro/UserRealm.java

@@ -7,7 +7,7 @@ import com.usky.service.sys.menuService.MenuService;
 import com.usky.service.sys.user.LoginService;
 import com.usky.service.sys.user.UserService;
 import com.usky.utils.RedisUtil;
-import com.usky.utils.jwt.JedisUtil;
+
 import com.usky.utils.jwt.JwtUtil;
 import com.usky.utils.jwt.common.StringUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -97,9 +97,9 @@ public class UserRealm extends AuthorizingRealm {
             throw new AuthenticationException("该帐号不存在(The account does not exist.)");
         }
         // 开始认证,要AccessToken认证通过,且Redis中存在RefreshToken,且两个Token时间戳一致
-        if (JwtUtil.verify(token) && JedisUtil.exists(Constant.PREFIX_SHIRO_REFRESH_TOKEN + loginName)) {
+        if (JwtUtil.verify(token) && redisUtil.hasKey(Constant.PREFIX_SHIRO_REFRESH_TOKEN + loginName)) {
             // 获取RefreshToken的时间戳
-            String currentTimeMillisRedis = JedisUtil.getObject(Constant.PREFIX_SHIRO_REFRESH_TOKEN + loginName).toString();
+            String currentTimeMillisRedis = redisUtil.get(Constant.PREFIX_SHIRO_REFRESH_TOKEN + loginName).toString();
             // 获取AccessToken时间戳,与RefreshToken的时间戳对比
             if (JwtUtil.getClaim(token, Constant.CURRENT_TIME_MILLIS).equals(currentTimeMillisRedis)) {
                 return new SimpleAuthenticationInfo(token, token, "userRealm");

+ 31 - 17
src/main/java/com/usky/config/shiro/cache/CustomCache.java

@@ -1,24 +1,35 @@
 package com.usky.config.shiro.cache;
 
 import com.usky.constant.Constant;
-import com.usky.utils.jwt.JedisUtil;
+
+import com.usky.utils.RedisUtil;
+import com.usky.utils.SpringContextUtils;
 import com.usky.utils.jwt.JwtUtil;
 import com.usky.utils.jwt.common.PropertiesUtil;
 import com.usky.utils.jwt.common.SerializableUtil;
 import org.apache.shiro.cache.Cache;
 import org.apache.shiro.cache.CacheException;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
 
 import java.util.*;
 
 /**
  * 重写Shiro的Cache保存读取
+ *
  * @author laowo
  * @date 2020/9/4 17:31
  */
-public class CustomCache<K,V> implements Cache<K,V> {
+public class CustomCache<K, V> implements Cache<K, V> {
+
+
+    public RedisUtil redisUtil() {
+        return (RedisUtil) SpringContextUtils.getBean("redisUtil");
+    }
 
     /**
      * 缓存的key名称获取为shiro:cache:account
+     *
      * @param key
      * @return java.lang.String
      * @author laowo
@@ -33,10 +44,10 @@ public class CustomCache<K,V> implements Cache<K,V> {
      */
     @Override
     public Object get(Object key) throws CacheException {
-        if(Boolean.FALSE.equals(JedisUtil.exists(this.getKey(key)))){
+        if (Boolean.FALSE.equals(redisUtil().hasKey(this.getKey(key)))) {
             return null;
         }
-        return JedisUtil.getObject(this.getKey(key));
+        return redisUtil().get(this.getKey(key));
     }
 
     /**
@@ -48,7 +59,7 @@ public class CustomCache<K,V> implements Cache<K,V> {
         PropertiesUtil.readProperties("config.properties");
         String shiroCacheExpireTime = PropertiesUtil.getProperty("shiroCacheExpireTime");
         // 设置Redis的Shiro缓存
-        return JedisUtil.setObject(this.getKey(key), value, Integer.parseInt(shiroCacheExpireTime));
+        return redisUtil().set(this.getKey(key), value, Integer.parseInt(shiroCacheExpireTime));
     }
 
     /**
@@ -56,10 +67,10 @@ public class CustomCache<K,V> implements Cache<K,V> {
      */
     @Override
     public Object remove(Object key) throws CacheException {
-        if(Boolean.FALSE.equals(JedisUtil.exists(this.getKey(key)))){
+        if (Boolean.FALSE.equals(redisUtil().hasKey(this.getKey(key)))) {
             return null;
         }
-        JedisUtil.delKey(this.getKey(key));
+        redisUtil().del(this.getKey(key));
         return null;
     }
 
@@ -68,7 +79,8 @@ public class CustomCache<K,V> implements Cache<K,V> {
      */
     @Override
     public void clear() throws CacheException {
-        Objects.requireNonNull(JedisUtil.getJedis()).flushDB();
+        //   Objects.requireNonNull(JedisUtil.getJedis()).flushDB();
+        redisUtil().removeAll();
     }
 
     /**
@@ -76,8 +88,9 @@ public class CustomCache<K,V> implements Cache<K,V> {
      */
     @Override
     public int size() {
-        Long size = Objects.requireNonNull(JedisUtil.getJedis()).dbSize();
-        return size.intValue();
+        //  Long size = Objects.requireNonNull(JedisUtil.getJedis()).dbSize();
+
+        return redisUtil().keys().size();
     }
 
     /**
@@ -85,12 +98,13 @@ public class CustomCache<K,V> implements Cache<K,V> {
      */
     @Override
     public Set keys() {
-        Set<byte[]> keys = Objects.requireNonNull(JedisUtil.getJedis()).keys("*".getBytes());
-        Set<Object> set = new HashSet<Object>();
-        for (byte[] bs : keys) {
-            set.add(SerializableUtil.unserializable(bs));
-        }
-        return set;
+//        Set<byte[]> keys = Objects.requireNonNull(JedisUtil.getJedis()).keys("*".getBytes());
+//        Set<Object> set = new HashSet<Object>();
+//        for (byte[] bs : keys) {
+//            set.add(SerializableUtil.unserializable(bs));
+//        }
+        redisUtil().keys();
+        return  redisUtil().keys();
     }
 
     /**
@@ -101,7 +115,7 @@ public class CustomCache<K,V> implements Cache<K,V> {
         Set keys = this.keys();
         List<Object> values = new ArrayList<Object>();
         for (Object key : keys) {
-            values.add(JedisUtil.getObject(this.getKey(key)));
+            values.add(redisUtil().get(this.getKey(key)));
         }
         return values;
     }

+ 9 - 4
src/main/java/com/usky/config/shiro/jwt/JwtFilter.java

@@ -5,7 +5,8 @@ import com.auth0.jwt.exceptions.TokenExpiredException;
 import com.usky.constant.Constant;
 import com.usky.entity.ResponseBean;
 import com.usky.exception.CustomException;
-import com.usky.utils.jwt.JedisUtil;
+
+import com.usky.utils.RedisUtil;
 import com.usky.utils.jwt.JwtUtil;
 import com.usky.utils.jwt.common.JsonConvertUtil;
 import com.usky.utils.jwt.common.PropertiesUtil;
@@ -13,6 +14,7 @@ import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
 import org.apache.shiro.web.util.WebUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 
 import javax.servlet.ServletRequest;
@@ -27,6 +29,9 @@ import java.io.PrintWriter;
  * @date 2020/8/30 15:47
  */
 public class JwtFilter extends BasicHttpAuthenticationFilter {
+
+    @Autowired
+    private RedisUtil redisUtil;
     /**
      * logger
      */
@@ -138,9 +143,9 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
         // 获取当前Token的帐号信息
         String account = JwtUtil.getClaim(token, Constant.ACCOUNT);
         // 判断Redis中RefreshToken是否存在
-        if (JedisUtil.exists(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account)) {
+        if (redisUtil.hasKey(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account)) {
             // Redis中RefreshToken还存在,获取RefreshToken的时间戳
-            String currentTimeMillisRedis = JedisUtil.getObject(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account).toString();
+            String currentTimeMillisRedis = redisUtil.get(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account).toString();
             // 获取当前AccessToken中的时间戳,与RefreshToken的时间戳对比,如果当前时间戳一致,进行AccessToken刷新
             if (JwtUtil.getClaim(token, Constant.CURRENT_TIME_MILLIS).equals(currentTimeMillisRedis)) {
                 // 获取当前最新时间戳
@@ -149,7 +154,7 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
                 PropertiesUtil.readProperties("config.properties");
                 String refreshTokenExpireTime = PropertiesUtil.getProperty("refreshTokenExpireTime");
                 // 设置RefreshToken中的时间戳为当前最新时间戳,且刷新过期时间重新为30分钟过期(配置文件可配置refreshTokenExpireTime属性)
-                JedisUtil.setObject(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account, currentTimeMillis, Integer.parseInt(refreshTokenExpireTime));
+                redisUtil.set(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account, currentTimeMillis, Integer.parseInt(refreshTokenExpireTime));
                 // 刷新AccessToken,设置时间戳为当前最新时间戳
                 token = JwtUtil.sign(account, currentTimeMillis);
                 // 将新刷新的AccessToken再次进行Shiro的登录

+ 3 - 4
src/main/java/com/usky/controller/login/LoginController.java

@@ -9,7 +9,6 @@ import com.usky.utils.AuthorizationUtils;
 import com.usky.utils.RedisUtil;
 import com.usky.utils.Result;
 import com.usky.utils.jwt.AesCipherUtil;
-import com.usky.utils.jwt.JedisUtil;
 import com.usky.utils.jwt.JwtUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
@@ -65,12 +64,12 @@ public class LoginController {
         String key = AesCipherUtil.deCrypto(user.getPassword());
         if (key.equals(loginName + passWord)) {
             // 清除可能存在的Shiro权限信息缓存
-            if (JedisUtil.exists(Constant.PREFIX_SHIRO_CACHE + loginName)) {
-                JedisUtil.delKey(Constant.PREFIX_SHIRO_CACHE + loginName);
+            if (redisUtil.hasKey(Constant.PREFIX_SHIRO_CACHE + loginName)) {
+                redisUtil.hasKey(Constant.PREFIX_SHIRO_CACHE + loginName);
             }
             // 设置RefreshToken,时间戳为当前时间戳,直接设置即可(不用先删后设,会覆盖已有的RefreshToken)
             String currentTimeMillis = String.valueOf(System.currentTimeMillis());
-            JedisUtil.setObject(Constant.PREFIX_SHIRO_REFRESH_TOKEN + loginName, currentTimeMillis, Integer.parseInt(refreshTokenExpireTime));
+            redisUtil.set(Constant.PREFIX_SHIRO_REFRESH_TOKEN + loginName, currentTimeMillis, Integer.parseInt(refreshTokenExpireTime));
             // 从Header中Authorization返回AccessToken,时间戳为当前时间戳
             String token = JwtUtil.sign(loginName, currentTimeMillis);
             httpServletResponse.setHeader("Authorization", token);

+ 37 - 0
src/main/java/com/usky/utils/RedisUtil.java

@@ -615,4 +615,41 @@ public class RedisUtil {
             e.printStackTrace();
         }
     }
+
+    /**
+     * 删除所有key
+     */
+    public void removeAll() {
+        try {
+            Set<String> keys = keys();
+            redisTemplate.delete(keys);
+        } catch (Throwable e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 获取所有key
+     *
+     * @return
+     */
+    public Set<String> keys() {
+        String realKey = "*";
+
+        try {
+            return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
+                Set<String> binaryKeys = new HashSet<>();
+                Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(realKey).count(Integer.MAX_VALUE).build());
+                while (cursor.hasNext()) {
+                    binaryKeys.add(new String(cursor.next()));
+                }
+
+                return binaryKeys;
+            });
+        } catch (Throwable e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
 }

+ 256 - 256
src/main/java/com/usky/utils/jwt/JedisUtil.java

@@ -1,256 +1,256 @@
-package com.usky.utils.jwt;
-
-
-import com.usky.constant.Constant;
-import com.usky.exception.CustomException;
-import com.usky.utils.jwt.common.SerializableUtil;
-import com.usky.utils.jwt.common.StringUtil;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-import redis.clients.jedis.Jedis;
-import redis.clients.jedis.JedisPool;
-
-import java.util.Set;
-
-/**
- * JedisUtil(推荐存Byte数组,存Json字符串效率更慢)
- * @author laowo
- * @date 2020/9/4 15:45
- */
-@Component
-public class JedisUtil {
-
-    /**
-     * 静态注入JedisPool连接池
-     * 本来是正常注入JedisUtil,可以在Controller和Service层使用,但是重写Shiro的CustomCache无法注入JedisUtil
-     * 现在改为静态注入JedisPool连接池,JedisUtil直接调用静态方法即可
-     * https://blog.csdn.net/W_Z_W_888/article/details/79979103
-     */
-    private static JedisPool jedisPool;
-
-    @Autowired
-    public void setJedisPool(JedisPool jedisPool) {
-        JedisUtil.jedisPool = jedisPool;
-    }
-
-    /**
-     * 获取Jedis实例
-     * @param 
-     * @return redis.clients.jedis.Jedis
-     * @author laowo
-     * @date 2020/9/4 15:47
-     */
-    public static synchronized Jedis getJedis() {
-        try {
-            if (jedisPool != null) {
-                return jedisPool.getResource();
-            } else {
-                return null;
-            }
-        } catch (Exception e) {
-            throw new CustomException("获取Jedis资源异常:" + e.getMessage());
-        }
-    }
-
-    /**
-     * 释放Jedis资源
-     * @param
-     * @return void
-     * @author laowo
-     * @date 2020/9/5 9:16
-     */
-    public static void closePool() {
-        try {
-            jedisPool.close();
-        } catch (Exception e) {
-            throw new CustomException("释放Jedis资源异常:" + e.getMessage());
-        }
-    }
-
-    /**
-     * 获取redis键值-object
-     * @param key
-     * @return java.lang.Object
-     * @author laowo
-     * @date 2020/9/4 15:47
-     */
-    public static Object getObject(String key) {
-        try (Jedis jedis = jedisPool.getResource()) {
-            byte[] bytes = jedis.get(key.getBytes());
-            if (StringUtil.isNotNull(bytes)) {
-                return SerializableUtil.unserializable(bytes);
-            }
-        } catch (Exception e) {
-            throw new CustomException("获取Redis键值getObject方法异常:key=" + key + " cause=" + e.getMessage());
-        }
-        return null;
-    }
-
-    /**
-     * 设置redis键值-object
-     * @param key
-	 * @param value
-     * @return java.lang.String
-     * @author laowo
-     * @date 2020/9/4 15:49
-     */
-    public static String setObject(String key, Object value) {
-        try (Jedis jedis = jedisPool.getResource()) {
-            return jedis.set(key.getBytes(), SerializableUtil.serializable(value));
-        } catch (Exception e) {
-            throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * 设置redis键值-object-expiretime
-     * @param key
-	 * @param value
-	 * @param expiretime
-     * @return java.lang.String
-     * @author laowo
-     * @date 2020/9/4 15:50
-     */
-    public static String setObject(String key, Object value, int expiretime) {
-        String result;
-        try (Jedis jedis = jedisPool.getResource()) {
-            result = jedis.set(key.getBytes(), SerializableUtil.serializable(value));
-            if (Constant.OK.equals(result)) {
-                jedis.expire(key.getBytes(), expiretime);
-            }
-            return result;
-        } catch (Exception e) {
-            throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * 获取redis键值-Json
-     * @param key
-     * @return java.lang.Object
-     * @author laowo
-     * @date 2020/9/4 15:47
-     */
-    public static String getJson(String key) {
-        try (Jedis jedis = jedisPool.getResource()) {
-            return jedis.get(key);
-        } catch (Exception e) {
-            throw new CustomException("获取Redis键值getJson方法异常:key=" + key + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * 设置redis键值-Json
-     * @param key
-     * @param value
-     * @return java.lang.String
-     * @author Wang926454
-     * @date 2020/9/4 15:49
-     */
-    public static String setJson(String key, String value) {
-        try (Jedis jedis = jedisPool.getResource()) {
-            return jedis.set(key, value);
-        } catch (Exception e) {
-            throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * 设置redis键值-Json-expiretime
-     * @param key
-     * @param value
-     * @param expiretime
-     * @return java.lang.String
-     * @author Wang926454
-     * @date 2020/9/4 15:50
-     */
-    public static String setJson(String key, String value, int expiretime) {
-        String result;
-        try (Jedis jedis = jedisPool.getResource()) {
-            result = jedis.set(key, value);
-            if (Constant.OK.equals(result)) {
-                jedis.expire(key, expiretime);
-            }
-            return result;
-        } catch (Exception e) {
-            throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * 删除key
-     * @param key
-     * @return java.lang.Long
-     * @author Wang926454
-     * @date 2020/9/4 15:50
-     */
-    public static Long delKey(String key) {
-        try (Jedis jedis = jedisPool.getResource()) {
-            return jedis.del(key.getBytes());
-        } catch (Exception e) {
-            throw new CustomException("删除Redis的键delKey方法异常:key=" + key + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * key是否存在
-     * @param key
-     * @return java.lang.Boolean
-     * @author Wang926454
-     * @date 2020/9/4 15:51
-     */
-    public static Boolean exists(String key) {
-        try (Jedis jedis = jedisPool.getResource()) {
-            return jedis.exists(key.getBytes());
-        } catch (Exception e) {
-            throw new CustomException("查询Redis的键是否存在exists方法异常:key=" + key + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * 模糊查询获取key集合(keys的速度非常快,但在一个大的数据库中使用它仍然可能造成性能问题,生产不推荐使用)
-     * @param key
-     * @return java.util.Set<java.lang.String>
-     * @author Wang926454
-     * @date 2020/9/6 9:43
-     */
-    public static Set<String> keysS(String key) {
-        try (Jedis jedis = jedisPool.getResource()) {
-            return jedis.keys(key);
-        } catch (Exception e) {
-            throw new CustomException("模糊查询Redis的键集合keysS方法异常:key=" + key + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * 模糊查询获取key集合(keys的速度非常快,但在一个大的数据库中使用它仍然可能造成性能问题,生产不推荐使用)
-     * @param key
-     * @return java.util.Set<java.lang.String>
-     * @author Wang926454
-     * @date 2020/9/6 9:43
-     */
-    public static Set<byte[]> keysB(String key) {
-        try (Jedis jedis = jedisPool.getResource()) {
-            return jedis.keys(key.getBytes());
-        } catch (Exception e) {
-            throw new CustomException("模糊查询Redis的键集合keysB方法异常:key=" + key + " cause=" + e.getMessage());
-        }
-    }
-
-    /**
-     * 获取过期剩余时间
-     * @param key
-     * @return java.lang.String
-     * @author Wang926454
-     * @date 2020/9/11 16:26
-     */
-    public static Long ttl(String key) {
-        Long result = -2L;
-        try (Jedis jedis = jedisPool.getResource()) {
-            result = jedis.ttl(key);
-            return result;
-        } catch (Exception e) {
-            throw new CustomException("获取Redis键过期剩余时间ttl方法异常:key=" + key + " cause=" + e.getMessage());
-        }
-    }
-}
+//package com.usky.utils.jwt;
+//
+//
+//import com.usky.constant.Constant;
+//import com.usky.exception.CustomException;
+//import com.usky.utils.jwt.common.SerializableUtil;
+//import com.usky.utils.jwt.common.StringUtil;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.stereotype.Component;
+//import redis.clients.jedis.Jedis;
+//import redis.clients.jedis.JedisPool;
+//
+//import java.util.Set;
+//
+///**
+// * JedisUtil(推荐存Byte数组,存Json字符串效率更慢)
+// * @author laowo
+// * @date 2020/9/4 15:45
+// */
+//@Component
+//public class JedisUtil {
+//
+//    /**
+//     * 静态注入JedisPool连接池
+//     * 本来是正常注入JedisUtil,可以在Controller和Service层使用,但是重写Shiro的CustomCache无法注入JedisUtil
+//     * 现在改为静态注入JedisPool连接池,JedisUtil直接调用静态方法即可
+//     * https://blog.csdn.net/W_Z_W_888/article/details/79979103
+//     */
+//    private static JedisPool jedisPool;
+//
+//    @Autowired
+//    public void setJedisPool(JedisPool jedisPool) {
+//        JedisUtil.jedisPool = jedisPool;
+//    }
+//
+//    /**
+//     * 获取Jedis实例
+//     * @param
+//     * @return redis.clients.jedis.Jedis
+//     * @author laowo
+//     * @date 2020/9/4 15:47
+//     */
+//    public static synchronized Jedis getJedis() {
+//        try {
+//            if (jedisPool != null) {
+//                return jedisPool.getResource();
+//            } else {
+//                return null;
+//            }
+//        } catch (Exception e) {
+//            throw new CustomException("获取Jedis资源异常:" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 释放Jedis资源
+//     * @param
+//     * @return void
+//     * @author laowo
+//     * @date 2020/9/5 9:16
+//     */
+//    public static void closePool() {
+//        try {
+//            jedisPool.close();
+//        } catch (Exception e) {
+//            throw new CustomException("释放Jedis资源异常:" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 获取redis键值-object
+//     * @param key
+//     * @return java.lang.Object
+//     * @author laowo
+//     * @date 2020/9/4 15:47
+//     */
+//    public static Object getObject(String key) {
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            byte[] bytes = jedis.get(key.getBytes());
+//            if (StringUtil.isNotNull(bytes)) {
+//                return SerializableUtil.unserializable(bytes);
+//            }
+//        } catch (Exception e) {
+//            throw new CustomException("获取Redis键值getObject方法异常:key=" + key + " cause=" + e.getMessage());
+//        }
+//        return null;
+//    }
+//
+//    /**
+//     * 设置redis键值-object
+//     * @param key
+//	 * @param value
+//     * @return java.lang.String
+//     * @author laowo
+//     * @date 2020/9/4 15:49
+//     */
+//    public static String setObject(String key, Object value) {
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            return jedis.set(key.getBytes(), SerializableUtil.serializable(value));
+//        } catch (Exception e) {
+//            throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 设置redis键值-object-expiretime
+//     * @param key
+//	 * @param value
+//	 * @param expiretime
+//     * @return java.lang.String
+//     * @author laowo
+//     * @date 2020/9/4 15:50
+//     */
+//    public static String setObject(String key, Object value, int expiretime) {
+//        String result;
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            result = jedis.set(key.getBytes(), SerializableUtil.serializable(value));
+//            if (Constant.OK.equals(result)) {
+//                jedis.expire(key.getBytes(), expiretime);
+//            }
+//            return result;
+//        } catch (Exception e) {
+//            throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 获取redis键值-Json
+//     * @param key
+//     * @return java.lang.Object
+//     * @author laowo
+//     * @date 2020/9/4 15:47
+//     */
+//    public static String getJson(String key) {
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            return jedis.get(key);
+//        } catch (Exception e) {
+//            throw new CustomException("获取Redis键值getJson方法异常:key=" + key + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 设置redis键值-Json
+//     * @param key
+//     * @param value
+//     * @return java.lang.String
+//     * @author Wang926454
+//     * @date 2020/9/4 15:49
+//     */
+//    public static String setJson(String key, String value) {
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            return jedis.set(key, value);
+//        } catch (Exception e) {
+//            throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 设置redis键值-Json-expiretime
+//     * @param key
+//     * @param value
+//     * @param expiretime
+//     * @return java.lang.String
+//     * @author Wang926454
+//     * @date 2020/9/4 15:50
+//     */
+//    public static String setJson(String key, String value, int expiretime) {
+//        String result;
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            result = jedis.set(key, value);
+//            if (Constant.OK.equals(result)) {
+//                jedis.expire(key, expiretime);
+//            }
+//            return result;
+//        } catch (Exception e) {
+//            throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 删除key
+//     * @param key
+//     * @return java.lang.Long
+//     * @author Wang926454
+//     * @date 2020/9/4 15:50
+//     */
+//    public static Long delKey(String key) {
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            return jedis.del(key.getBytes());
+//        } catch (Exception e) {
+//            throw new CustomException("删除Redis的键delKey方法异常:key=" + key + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * key是否存在
+//     * @param key
+//     * @return java.lang.Boolean
+//     * @author Wang926454
+//     * @date 2020/9/4 15:51
+//     */
+//    public static Boolean exists(String key) {
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            return jedis.exists(key.getBytes());
+//        } catch (Exception e) {
+//            throw new CustomException("查询Redis的键是否存在exists方法异常:key=" + key + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 模糊查询获取key集合(keys的速度非常快,但在一个大的数据库中使用它仍然可能造成性能问题,生产不推荐使用)
+//     * @param key
+//     * @return java.util.Set<java.lang.String>
+//     * @author Wang926454
+//     * @date 2020/9/6 9:43
+//     */
+//    public static Set<String> keysS(String key) {
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            return jedis.keys(key);
+//        } catch (Exception e) {
+//            throw new CustomException("模糊查询Redis的键集合keysS方法异常:key=" + key + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 模糊查询获取key集合(keys的速度非常快,但在一个大的数据库中使用它仍然可能造成性能问题,生产不推荐使用)
+//     * @param key
+//     * @return java.util.Set<java.lang.String>
+//     * @author Wang926454
+//     * @date 2020/9/6 9:43
+//     */
+//    public static Set<byte[]> keysB(String key) {
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            return jedis.keys(key.getBytes());
+//        } catch (Exception e) {
+//            throw new CustomException("模糊查询Redis的键集合keysB方法异常:key=" + key + " cause=" + e.getMessage());
+//        }
+//    }
+//
+//    /**
+//     * 获取过期剩余时间
+//     * @param key
+//     * @return java.lang.String
+//     * @author Wang926454
+//     * @date 2020/9/11 16:26
+//     */
+//    public static Long ttl(String key) {
+//        Long result = -2L;
+//        try (Jedis jedis = jedisPool.getResource()) {
+//            result = jedis.ttl(key);
+//            return result;
+//        } catch (Exception e) {
+//            throw new CustomException("获取Redis键过期剩余时间ttl方法异常:key=" + key + " cause=" + e.getMessage());
+//        }
+//    }
+//}

+ 0 - 4
src/main/java/com/usky/utils/jwt/JwtUtil.java

@@ -34,12 +34,10 @@ public class JwtUtil {
      * 过期时间改为从配置文件获取
      */
     private static String accessTokenExpireTime;
-
     /**
      * JWT认证加密私钥(Base64加密)
      */
     private static String encryptJWTKey;
-
     @Value("${accessTokenExpireTime}")
     public void setAccessTokenExpireTime(String accessTokenExpireTime) {
         JwtUtil.accessTokenExpireTime = accessTokenExpireTime;
@@ -70,7 +68,6 @@ public class JwtUtil {
             throw new CustomException("JWTToken认证解密出现UnsupportedEncodingException异常:" + e.getMessage());
         }
     }
-
     /**
      * 获得Token中的信息无需secret解密也能获得
      * @param token
@@ -89,7 +86,6 @@ public class JwtUtil {
             throw new CustomException("解密Token中的公共信息出现JWTDecodeException异常:" + e.getMessage());
         }
     }
-
     /**
      * 生成签名
      * @param account 帐号

+ 4 - 3
src/main/resources/application.yml

@@ -4,8 +4,8 @@ server:
 spring:
   redis:
     database: 0
-    host: 47.111.81.118
-    #host: 172.16.120.184
+    #host: 47.111.81.118
+    host: 172.16.120.184
     lettuce:
       pool:
         max-active: 8   #最大连接数据库连接数,设 0 为没有限制
@@ -13,7 +13,8 @@ spring:
         max-wait: -1ms  #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
         min-idle: 0     #最小等待连接中的数量,设 0 为没有限制
       shutdown-timeout: 100ms
-    password: uskyredis
+   # password: uskyredis
+    password:
     port: 6379
   jackson:
     time-zone: GMT+8

+ 2 - 19
src/main/resources/config.properties

@@ -3,25 +3,8 @@ encryptAESKey=V2FuZzkyNjQ1NGRTQkFQSUpXVA==
 # JWT认证加密私钥(Base64加密)
 encryptJWTKey=U0JBUElKV1RkV2FuZzkyNjQ1NA==
 # AccessToken过期时间-5分钟-5*60(秒为单位)
-accessTokenExpireTime=300
+accessTokenExpireTime=1800
 # RefreshToken过期时间-30分钟-30*60(秒为单位)
 refreshTokenExpireTime=1800
 # Shiro缓存过期时间-5分钟-5*60(秒为单位)(一般设置与AccessToken过期时间一致)
-shiroCacheExpireTime=300
-
-# Redis服务器地址
-redis.host=172.16.120.184
-# Redis服务器连接端口
-redis.port=6379
-# Redis服务器连接密码(默认为空)
-redis.password=
-# 连接超时时间(毫秒)
-redis.timeout=10000
-# 连接池最大连接数(使用负值表示没有限制)
-redis.pool.max-active=200
-# 连接池最大阻塞等待时间(使用负值表示没有限制)
-redis.pool.max-wait=-1
-# 连接池中的最大空闲连接
-redis.pool.max-idle=8
-# 连接池中的最小空闲连接
-redis.pool.min-idle=0
+shiroCacheExpireTime=1800