Shiro教程(四)Shiro + Redis实现


上一篇博客讲到了 Shiro  + redis  的配置,其实没说完,但是在上篇说完不合适,所以在这里来细化说明, Shiro  首先是支持任何存储的和它来一起完成这个任务的,因为它提供了接口是交给我们来实现的。

要实现的接口:

1.org.apache.shiro.cache.CacheManager (缓存管理)

2.org.apache.shiro.cache.Cache (实现缓存存储)

3.org.apache.shiro.session.SessionListener (Session监听)

缓存的配置:

  1. <!-- 用户缓存 -->
  2. <bean id="customShiroCacheManager" class="com.sojson.core.shiro.cache.impl.CustomShiroCacheManager">
  3. <property name="shiroCacheManager" ref="jedisShiroCacheManager"/>
  4. </bean>
  5. <!-- shiro 缓存实现,对ShiroCacheManager,我是采用redis的实现 -->
  6. <bean id="jedisShiroCacheManager" class="com.sojson.core.shiro.cache.impl.JedisShiroCacheManager">
  7. <property name="jedisManager" ref="jedisManager"/>
  8. </bean>
  9. <!-- redis 的缓存 -->
  10. <bean id="jedisManager" class="com.sojson.core.shiro.cache.JedisManager">
  11. <property name="jedisPool" ref="jedisPool"/>
  12. </bean>

首先是用户的缓存类。CustomShiroCacheManager.java

 
  1. package com.sojson.core.shiro.cache.impl;
  2. import org.apache.shiro.cache.Cache;
  3. import org.apache.shiro.cache.CacheException;
  4. import org.apache.shiro.cache.CacheManager;
  5. import org.apache.shiro.util.Destroyable;
  6. import com.sojson.core.shiro.cache.ShiroCacheManager;
  7. /**
  8. *
  9. * 开发公司:sojson.com<br/>
  10. * 版权:sojson.com<br/>
  11. * <p>
  12. *
  13. * shiro Custom Cache
  14. *
  15. * <p>
  16. *
  17. * 区分 责任人 日期    说明<br/>
  18. * 创建 周柏成 2016年4月29日  <br/>
  19. * <p>
  20. * *******
  21. * <p>
  22. * @author zhou-baicheng
  23. * @email json@sojson.com
  24. * @version 1.0,2016年4月29日 <br/>
  25. *
  26. */
  27. public class CustomShiroCacheManager implements CacheManager, Destroyable {
  28. private ShiroCacheManager shiroCacheManager;
  29. @Override
  30. public <K, V> Cache<K, V> getCache(String name) throws CacheException {
  31. return getShiroCacheManager().getCache(name);
  32. }
  33. @Override
  34. public void destroy() throws Exception {
  35. shiroCacheManager.destroy();
  36. }
  37. public ShiroCacheManager getShiroCacheManager() {
  38. return shiroCacheManager;
  39. }
  40. public void setShiroCacheManager(ShiroCacheManager shiroCacheManager) {
  41. this.shiroCacheManager = shiroCacheManager;
  42. }
  43. }

JRedis管理类JedisShiroCacheManager.java

  1. package com.sojson.core.shiro.cache.impl;
  2. import org.apache.shiro.cache.Cache;
  3. import com.sojson.core.shiro.cache.JedisManager;
  4. import com.sojson.core.shiro.cache.JedisShiroCache;
  5. import com.sojson.core.shiro.cache.ShiroCacheManager;
  6. /**
  7. *
  8. * 开发公司:itboy.net<br/>
  9. * 版权:itboy.net<br/>
  10. * <p>
  11. *
  12. * JRedis管理
  13. *
  14. * <p>
  15. *
  16. * 区分 责任人 日期    说明<br/>
  17. * 创建 周柏成 2016年5月6日  <br/>
  18. * <p>
  19. * *******
  20. * <p>
  21. * @author zhou-baicheng
  22. * @email i@itboy.net
  23. * @version 1.0,2016年5月6日 <br/>
  24. *
  25. */
  26. public class JedisShiroCacheManager implements ShiroCacheManager {
  27. private JedisManager jedisManager;
  28. @Override
  29. public <K, V> Cache<K, V> getCache(String name) {
  30. return new JedisShiroCache<K, V>(name, getJedisManager());
  31. }
  32. @Override
  33. public void destroy() {
  34. //如果和其他系统,或者应用在一起就不能关闭
  35. //getJedisManager().getJedis().shutdown();
  36. }
  37. public JedisManager getJedisManager() {
  38. return jedisManager;
  39. }
  40. public void setJedisManager(JedisManager jedisManager) {
  41. this.jedisManager = jedisManager;
  42. }
  43. }

Redis  操作类(工具类)JedisManager.java 

 
  1. package com.sojson.core.shiro.cache;
  2. import java.util.Collection;
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. import org.apache.shiro.session.Session;
  6. import redis.clients.jedis.Jedis;
  7. import redis.clients.jedis.JedisPool;
  8. import redis.clients.jedis.exceptions.JedisConnectionException;
  9. import com.sojson.common.utils.LoggerUtils;
  10. import com.sojson.common.utils.SerializeUtil;
  11. /**
  12. *
  13. * 开发公司:sojson.com<br/>
  14. * 版权:sojson.com<br/>
  15. * <p>
  16. *
  17. * Redis Manager Utils
  18. *
  19. * <p>
  20. *
  21. * 区分 责任人 日期    说明<br/>
  22. * 创建 周柏成 2016年4月29日  <br/>
  23. * <p>
  24. * *******
  25. * <p>
  26. * @author zhou-baicheng
  27. * @email json@sojson.com
  28. * @version 1.0,2016年4月29日 <br/>
  29. *
  30. */
  31. public class JedisManager {
  32. private JedisPool jedisPool;
  33. public Jedis getJedis() {
  34. Jedis jedis = null;
  35. try {
  36. jedis = getJedisPool().getResource();
  37. } catch (Exception e) {
  38. throw new JedisConnectionException(e);
  39. }
  40. return jedis;
  41. }
  42. public void returnResource(Jedis jedis, boolean isBroken) {
  43. if (jedis == null)
  44. return;
  45. if (isBroken)
  46. getJedisPool().returnBrokenResource(jedis);
  47. else
  48. getJedisPool().returnResource(jedis);
  49. }
  50. public byte[] getValueByKey(int dbIndex, byte[] key) throws Exception {
  51. Jedis jedis = null;
  52. byte[] result = null;
  53. boolean isBroken = false;
  54. try {
  55. jedis = getJedis();
  56. jedis.select(dbIndex);
  57. result = jedis.get(key);
  58. } catch (Exception e) {
  59. isBroken = true;
  60. throw e;
  61. } finally {
  62. returnResource(jedis, isBroken);
  63. }
  64. return result;
  65. }
  66. public void deleteByKey(int dbIndex, byte[] key) throws Exception {
  67. Jedis jedis = null;
  68. boolean isBroken = false;
  69. try {
  70. jedis = getJedis();
  71. jedis.select(dbIndex);
  72. Long result = jedis.del(key);
  73. LoggerUtils.fmtDebug(getClass(), "删除Session结果:%s" , result);
  74. } catch (Exception e) {
  75. isBroken = true;
  76. throw e;
  77. } finally {
  78. returnResource(jedis, isBroken);
  79. }
  80. }
  81. public void saveValueByKey(int dbIndex, byte[] key, byte[] value, int expireTime)
  82. throws Exception {
  83. Jedis jedis = null;
  84. boolean isBroken = false;
  85. try {
  86. jedis = getJedis();
  87. jedis.select(dbIndex);
  88. jedis.set(key, value);
  89. if (expireTime > 0)
  90. jedis.expire(key, expireTime);
  91. } catch (Exception e) {
  92. isBroken = true;
  93. throw e;
  94. } finally {
  95. returnResource(jedis, isBroken);
  96. }
  97. }
  98. public JedisPool getJedisPool() {
  99. return jedisPool;
  100. }
  101. public void setJedisPool(JedisPool jedisPool) {
  102. this.jedisPool = jedisPool;
  103. }
  104. /**
  105. * 获取所有Session
  106. * @param dbIndex
  107. * @param redisShiroSession
  108. * @return
  109. * @throws Exception
  110. */
  111. @SuppressWarnings("unchecked")
  112. public Collection<Session> AllSession(int dbIndex, String redisShiroSession) throws Exception {
  113. Jedis jedis = null;
  114. boolean isBroken = false;
  115. Set<Session> sessions = new HashSet<Session>();
  116. try {
  117. jedis = getJedis();
  118. jedis.select(dbIndex);
  119. Set<byte[]> byteKeys = jedis.keys((JedisShiroSessionRepository.REDIS_SHIRO_ALL).getBytes());
  120. if (byteKeys != null && byteKeys.size() > 0) {
  121. for (byte[] bs : byteKeys) {
  122. Object obj = SerializeUtil.deserialize(jedis.get(bs),
  123. Object.class);
  124. if(obj instanceof Session){
  125. sessions.add((Session)obj);
  126. }
  127. }
  128. }
  129. } catch (Exception e) {
  130. isBroken = true;
  131. throw e;
  132. } finally {
  133. returnResource(jedis, isBroken);
  134. }
  135. return sessions;
  136. }
  137. }

SessionListener 的实现。

 
  1. package com.sojson.core.shiro.listenter;
  2. import org.apache.shiro.session.Session;
  3. import org.apache.shiro.session.SessionListener;
  4. import com.sojson.core.shiro.session.ShiroSessionRepository;
  5. public class CustomSessionListener implements SessionListener {
  6. private ShiroSessionRepository shiroSessionRepository;
  7. /**
  8. * 一个回话的生命周期开始
  9. */
  10. @Override
  11. public void onStart(Session session) {
  12. //TODO
  13. System.out.println("on start");
  14. }
  15. /**
  16. * 一个回话的生命周期结束
  17. */
  18. @Override
  19. public void onStop(Session session) {
  20. //TODO
  21. System.out.println("on stop");
  22. }
  23. @Override
  24. public void onExpiration(Session session) {
  25. shiroSessionRepository.deleteSession(session.getId());
  26. }
  27. public ShiroSessionRepository getShiroSessionRepository() {
  28. return shiroSessionRepository;
  29. }
  30. public void setShiroSessionRepository(ShiroSessionRepository shiroSessionRepository) {
  31. this.shiroSessionRepository = shiroSessionRepository;
  32. }
  33. }

关于生命周期的问题。可以自己实现,可以做一些在线用户的相关操作,以及踢出等操作。



原文地址:https://www.sojson.com/blog/135.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值