SpringBoot + Shiro + JWT集成Redis缓存(Jedis)

序言

    目录:https://blog.csdn.net/wang926454/article/details/82971291

首先感谢SmithCruise提供的思路,文章地址:https://www.jianshu.com/p/f37f8c295057

根据SmithCruise的项目进行后续更新

    将其改为数据库形式(MySQL)

    实现Shiro的Cache(Redis)功能

    解决无法直接返回401错误

    Token刷新(RefreshToken)

当前博客源码:https://download.csdn.net/download/wang926454/10726052

我的项目地址

    Github:https://github.com/wang926454/ShiroJwt

    Gitee(码云):https://gitee.com/wang926454/ShiroJwt

实现Shiro的Cache(Redis)功能

主要参考

    https://blog.csdn.net/why15732625998/article/details/78729254

    http://www.cnblogs.com/GodHeng/p/9301330.html

    https://blog.csdn.net/W_Z_W_888/article/details/79979103

实现方式

    建立JedisPool(启动注入JedisPool)

    Jedis操作Redis

    重写Shiro的Cache保存读取

    重写Shiro缓存(Cache)管理器

JedisPool搭建

首先加入Jedis的Jar(Shiro的集成这里就不说了)

<!-- Redis-Jedis -->

<dependency>

    <groupId>redis.clients</groupId>

    <artifactId>jedis</artifactId>

    <version>2.9.0</version>

</dependency>

    1

    2

    3

    4

    5

    6

config.properties(Redis的配置属性)

# Redis服务器地址

redis.host=127.0.0.1

# 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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

JedisConfig.java(JedisPool启动配置Bean)

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 Wang926454

* @date 2018/9/5 10:35

*/

@Configuration

@EnableAutoConfiguration

@PropertySource("classpath:config.properties")

@ConfigurationProperties(prefix = "redis")

public class JedisConfig {

    private static 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);

            // JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);

            JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, null);

            logger.info("初始化Redis连接池JedisPool成功!" + " Redis地址: " + 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;

    }

}

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119

    120

    121

    122

    123

    124

    125

    126

    127

JedisUtil(Jedis工具类)

import com.wang.exception.CustomException;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

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 Wang926454

* @date 2018/9/4 15:45

*/

@Component

public class JedisUtil {

    /**

    * Logger

    */

    private static Logger logger = LoggerFactory.getLogger(JedisUtil.class);

    /**

    * Status-OK

    */

    public final static String OK = "OK";

    /**

    * 静态注入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 Wang926454

    * @date 2018/9/4 15:47

    */

    public static synchronized Jedis getJedis() {

        try {

            if (jedisPool != null) {

                Jedis resource = jedisPool.getResource();

                return resource;

            } else {

                return null;

            }

        } catch (Exception e) {

            throw new CustomException("获取Jedis资源异常:" + e.getMessage());

        }

    }

    /**

    * 释放Jedis资源

    * @param

    * @return void

    * @author Wang926454

    * @date 2018/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 Wang926454

    * @date 2018/9/4 15:47

    */

    public static Object getObject(String key) {

        Jedis jedis = null;

        try {

            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());

        } finally {

            if(jedis != null) {

                jedis.close();

            }

        }

        return null;

    }

    /**

    * 设置redis键值-object

    * @param key

* @param value

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/4 15:49

    */

    public static String setObject(String key, Object value) {

        Jedis jedis = null;

        try {

            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());

        } finally {

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * 设置redis键值-object-expiretime

    * @param key

* @param value

* @param expiretime

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/4 15:50

    */

    public static String setObject(String key, Object value, int expiretime) {

        String result = "";

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            result = jedis.set(key.getBytes(), SerializableUtil.serializable(value));

            if(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());

        } finally {

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * 获取redis键值-Json

    * @param key

    * @return java.lang.Object

    * @author Wang926454

    * @date 2018/9/4 15:47

    */

    public static String getJson(String key) {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            return jedis.get(key);

        } catch (Exception e) {

            throw new CustomException("获取Redis键值getJson方法异常:key=" + key + " cause=" + e.getMessage());

        } finally {

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * 设置redis键值-Json

    * @param key

    * @param value

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/4 15:49

    */

    public static String setJson(String key, String value) {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            return jedis.set(key, value);

        } catch (Exception e) {

            throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());

        } finally {

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * 设置redis键值-Json-expiretime

    * @param key

    * @param value

    * @param expiretime

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/4 15:50

    */

    public static String setJson(String key, String value, int expiretime) {

        String result = "";

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            result = jedis.set(key, value);

            if(OK.equals(result)) {

                jedis.expire(key, expiretime);

            }

            return result;

        } catch (Exception e) {

            throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());

        } finally {

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * 删除key

    * @param key

    * @return java.lang.Long

    * @author Wang926454

    * @date 2018/9/4 15:50

    */

    public static Long delKey(String key) {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            return jedis.del(key.getBytes());

        }catch(Exception e) {

            throw new CustomException("删除Redis的键delKey方法异常:key=" + key + " cause=" + e.getMessage());

        }finally{

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * key是否存在

    * @param key

    * @return java.lang.Boolean

    * @author Wang926454

    * @date 2018/9/4 15:51

    */

    public static Boolean exists(String key) {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            return jedis.exists(key.getBytes());

        }catch(Exception e) {

            throw new CustomException("查询Redis的键是否存在exists方法异常:key=" + key + " cause=" + e.getMessage());

        }finally{

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * 模糊查询获取key集合

    * @param key

    * @return java.util.Set<java.lang.String>

    * @author Wang926454

    * @date 2018/9/6 9:43

    */

    public static Set<String> keysS(String key) {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            return jedis.keys(key);

        }catch(Exception e) {

            throw new CustomException("模糊查询Redis的键集合keysS方法异常:key=" + key + " cause=" + e.getMessage());

        }finally{

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * 模糊查询获取key集合

    * @param key

    * @return java.util.Set<java.lang.String>

    * @author Wang926454

    * @date 2018/9/6 9:43

    */

    public static Set<byte[]> keysB(String key) {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            return jedis.keys(key.getBytes());

        }catch(Exception e) {

            throw new CustomException("模糊查询Redis的键集合keysB方法异常:key=" + key + " cause=" + e.getMessage());

        }finally{

            if(jedis != null) {

                jedis.close();

            }

        }

    }

    /**

    * 获取过期剩余时间

    * @param key

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/11 16:26

    */

    public static Long getExpireTime(String key) {

        Long result = -2L;

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            result = jedis.ttl(key);

            return result;

        } catch (Exception e) {

            throw new CustomException("获取Redis键过期剩余时间getExpireTime方法异常:key=" + key + " cause=" + e.getMessage());

        } finally {

            if(jedis != null) {

                jedis.close();

            }

        }

    }

}

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119

    120

    121

    122

    123

    124

    125

    126

    127

    128

    129

    130

    131

    132

    133

    134

    135

    136

    137

    138

    139

    140

    141

    142

    143

    144

    145

    146

    147

    148

    149

    150

    151

    152

    153

    154

    155

    156

    157

    158

    159

    160

    161

    162

    163

    164

    165

    166

    167

    168

    169

    170

    171

    172

    173

    174

    175

    176

    177

    178

    179

    180

    181

    182

    183

    184

    185

    186

    187

    188

    189

    190

    191

    192

    193

    194

    195

    196

    197

    198

    199

    200

    201

    202

    203

    204

    205

    206

    207

    208

    209

    210

    211

    212

    213

    214

    215

    216

    217

    218

    219

    220

    221

    222

    223

    224

    225

    226

    227

    228

    229

    230

    231

    232

    233

    234

    235

    236

    237

    238

    239

    240

    241

    242

    243

    244

    245

    246

    247

    248

    249

    250

    251

    252

    253

    254

    255

    256

    257

    258

    259

    260

    261

    262

    263

    264

    265

    266

    267

    268

    269

    270

    271

    272

    273

    274

    275

    276

    277

    278

    279

    280

    281

    282

    283

    284

    285

    286

    287

    288

    289

    290

    291

    292

    293

    294

    295

    296

    297

    298

    299

    300

    301

    302

    303

    304

    305

    306

    307

    308

    309

    310

    311

    312

    313

    314

    315

    316

    317

    318

    319

    320

    321

    322

    323

    324

    325

    326

    327

    328

SerializableUtil(JedisUtil用到)

import com.wang.exception.CustomException;

import java.io.*;

/**

* Serializable工具(JDK)(也可以使用Protobuf自行百度)

* @author Wang926454

* @date 2018/9/4 15:13

*/

public class SerializableUtil {

    /**

    * 序列化

    * @param object

    * @return byte[]

    * @author Wang926454

    * @date 2018/9/4 15:14

    */

    public static byte[] serializable(Object object) {

        ByteArrayOutputStream baos = null;

        ObjectOutputStream oos = null;

        try {

            baos = new ByteArrayOutputStream();

            oos = new ObjectOutputStream(baos);

            oos.writeObject(object);

            byte[] bytes = baos.toByteArray();

            return bytes;

        } catch (IOException e) {

            e.printStackTrace();

            throw new CustomException("SerializableUtil工具类序列化出现IOException异常");

        } finally {

            try {

                if(oos != null) {

                    oos.close();

                }

                if(baos != null) {

                    baos.close();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    /**

    * 反序列化

    * @param bytes

    * @return java.lang.Object

    * @author Wang926454

    * @date 2018/9/4 15:14

    */

    public static Object unserializable(byte[] bytes) {

        ByteArrayInputStream bais = null;

        ObjectInputStream ois = null;

        try {

            bais = new ByteArrayInputStream(bytes);

            ois = new ObjectInputStream(bais);

            return ois.readObject();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

            throw new CustomException("SerializableUtil工具类反序列化出现ClassNotFoundException异常");

        } catch (IOException e) {

            e.printStackTrace();

            throw new CustomException("SerializableUtil工具类反序列化出现IOException异常");

        } finally {

            try {

                if(ois != null) {

                    ois.close();

                }

                if(bais != null) {

                    bais.close();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

}

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

StringUtil(JedisUtil用到)

public class StringUtil {

    /**

    * 定义下划线

    */

    private static final char UNDERLINE = '_';

    /**

    * String为空判断(不允许空格)

    * @param str

    * @return boolean

    * @author Wang926454

    * @date 2018/9/4 14:49

    */

    public static boolean isBlank(String str) {

        return str == null || "".equals(str.trim());

    }

    /**

    * String不为空判断(不允许空格)

    * @param str

    * @return boolean

    * @author Wang926454

    * @date 2018/9/4 14:51

    */

    public static boolean isNotBlank(String str) {

        return !isBlank(str);

    }

    /**

    * Byte数组为空判断

    * @param bytes

    * @return boolean

    * @author Wang926454

    * @date 2018/9/4 15:39

    */

    public static boolean isNull(byte[] bytes){

        // 根据byte数组长度为0判断

        return bytes.length == 0 || bytes == null;

    }

    /**

    * Byte数组不为空判断

    * @param bytes

    * @return boolean

    * @author Wang926454

    * @date 2018/9/4 15:41

    */

    public static boolean isNotNull(byte[] bytes) {

        return !isNull(bytes);

    }

    /**

    * 驼峰转下划线工具

    * @param param

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/4 14:52

    */

    public static String camelToUnderline(String param) {

        if (isNotBlank(param)) {

            int len = param.length();

            StringBuilder sb = new StringBuilder(len);

            for (int i = 0; i < len; ++i) {

                char c = param.charAt(i);

                if (Character.isUpperCase(c)) {

                    sb.append(UNDERLINE);

                    sb.append(Character.toLowerCase(c));

                } else {

                    sb.append(c);

                }

            }

            return sb.toString();

        } else {

            return "";

        }

    }

    /**

    * 下划线转驼峰工具

    * @param param

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/4 14:52

    */

    public static String underlineToCamel(String param) {

        if (isNotBlank(param)) {

            int len = param.length();

            StringBuilder sb = new StringBuilder(len);

            for (int i = 0; i < len; ++i) {

                char c = param.charAt(i);

                if (c == 95) {

                    ++i;

                    if (i < len) {

                        sb.append(Character.toUpperCase(param.charAt(i)));

                    }

                } else {

                    sb.append(c);

                }

            }

            return sb.toString();

        } else {

            return "";

        }

    }

    /**

    * 在字符串两周添加''

    * @param param

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/4 14:53

    */

    public static String addSingleQuotes(String param) {

        return "\'" + param + "\'";

    }

}

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

重写Shiro的Cache保存读取和Shiro的Cache管理器

CustomCache.java(Cache保存读取)

import com.wang.util.JWTUtil;

import com.wang.util.JedisUtil;

import com.wang.util.SerializableUtil;

import org.apache.shiro.cache.Cache;

import org.apache.shiro.cache.CacheException;

import java.util.*;

/**

* 重写Shiro的Cache保存读取

* @author Wang926454

* @date 2018/9/4 17:31

*/

public class CustomCache<K,V> implements Cache<K,V> {

    /**

    * redis-key-前缀-shiro:cache:

    */

    public final static String PREFIX_SHIRO_CACHE = "shiro:cache:";

    /**

    * 过期时间-5分钟

    */

    private static final Integer EXPIRE_TIME = 5 * 60 * 1000;

    /**

    * 缓存的key名称获取为shiro:cache:account

    * @param key

    * @return java.lang.String

    * @author Wang926454

    * @date 2018/9/4 18:33

    */

    private String getKey(Object key){

        return PREFIX_SHIRO_CACHE + JWTUtil.getUsername(key.toString());

    }

    /**

    * 获取缓存

    */

    @Override

    public Object get(Object key) throws CacheException {

        if(!JedisUtil.exists(this.getKey(key))){

            return null;

        }

        return JedisUtil.getObject(this.getKey(key));

    }

    /**

    * 保存缓存

    */

    @Override

    public Object put(Object key, Object value) throws CacheException {

        // 设置Redis的Shiro缓存

        return JedisUtil.setObject(this.getKey(key), value, EXPIRE_TIME);

    }

    /**

    * 移除缓存

    */

    @Override

    public Object remove(Object key) throws CacheException {

        if(!JedisUtil.exists(this.getKey(key))){

            return null;

        }

        JedisUtil.delKey(this.getKey(key));

        return null;

    }

    /**

    * 清空所有缓存

    */

    @Override

    public void clear() throws CacheException {

        JedisUtil.getJedis().flushDB();

    }

    /**

    * 缓存的个数

    */

    @Override

    public int size() {

        Long size = JedisUtil.getJedis().dbSize();

        return size.intValue();

    }

    /**

    * 获取所有的key

    */

    @Override

    public Set keys() {

        Set<byte[]> keys = JedisUtil.getJedis().keys(new String("*").getBytes());

        Set<Object> set = new HashSet<Object>();

        for (byte[] bs : keys) {

            set.add(SerializableUtil.unserializable(bs));

        }

        return set;

    }

    /**

    * 获取所有的value

    */

    @Override

    public Collection values() {

        Set keys = this.keys();

        List<Object> values = new ArrayList<Object>();

        for (Object key : keys) {

            values.add(JedisUtil.getObject(this.getKey(key)));

        }

        return values;

    }

}

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

CustomCacheManager.java(缓存(Cache)管理器)

import org.apache.shiro.cache.Cache;

import org.apache.shiro.cache.CacheException;

import org.apache.shiro.cache.CacheManager;

/**

* 重写Shiro缓存管理器

* @author Wang926454

* @date 2018/9/4 17:41

*/

public class CustomCacheManager implements CacheManager {

    @Override

    public <K, V> Cache<K, V> getCache(String s) throws CacheException {

        return new CustomCache<K,V>();

    }

}

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

最后在Shiro的配置Bean里设置我们重写的缓存(Cache)管理器

/**

* 配置使用自定义Realm,关闭Shiro自带的session

* 详情见文档 http://shiro.apache.org/session-management.html#SessionManagement-StatelessApplications%28Sessionless%29

* @param userRealm

* @return org.apache.shiro.web.mgt.DefaultWebSecurityManager

* @author Wang926454

* @date 2018/8/31 10:55

*/

@Bean("securityManager")

public DefaultWebSecurityManager getManager(UserRealm userRealm) {

    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();

    // 使用自定义Realm

    manager.setRealm(userRealm);

    // 关闭Shiro自带的session

    DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();

    DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();

    defaultSessionStorageEvaluator.setSessionStorageEnabled(false);

    subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);

    manager.setSubjectDAO(subjectDAO);

    // 设置自定义缓存(Cache)管理器

    manager.setCacheManager(new CustomCacheManager());

    return manager;

}

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

OK,我们现在可以在Realm的doGetAuthorizationInfo()方法打断点看下请求第一次后Redis多了一条缓存数据,下次就不会再调用doGetAuthorizationInfo()方法,除非缓存失效

在这里插入图片描述

当前博客源码:https://download.csdn.net/download/wang926454/10726052

我的项目地址

    Github:https://github.com/wang926454/ShiroJwt

    Gitee(码云):https://gitee.com/wang926454/ShiroJwt

---------------------

作者:时间可以改变一切

来源:CSDN

原文:https://blog.csdn.net/wang926454/article/details/82978632

版权声明:本文为博主原创文章,转载请附上博文链接!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容