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.  
  6. <!-- shiro 缓存实现,对ShiroCacheManager,我是采用redis的实现 -->
  7. <bean id="jedisShiroCacheManager" class="com.sojson.core.shiro.cache.impl.JedisShiroCacheManager">
  8. <property name="jedisManager" ref="jedisManager"/>
  9. </bean>
  10. <!-- redis 的缓存 -->
  11. <bean id="jedisManager" class="com.sojson.core.shiro.cache.JedisManager">
  12. <property name="jedisPool" ref="jedisPool"/>
  13. </bean>

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

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

JRedis管理类JedisShiroCacheManager.java

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

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

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

SessionListener 的实现。

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

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

猜你喜欢

转载自blog.csdn.net/baidu_37366055/article/details/88072101