SessionCacheManager.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
  3. */
  4. package com.jeeplus.common.security.shiro.cache;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.Set;
  8. import javax.servlet.http.HttpServletRequest;
  9. import org.apache.shiro.SecurityUtils;
  10. import org.apache.shiro.UnavailableSecurityManagerException;
  11. import org.apache.shiro.cache.Cache;
  12. import org.apache.shiro.cache.CacheException;
  13. import org.apache.shiro.cache.CacheManager;
  14. import org.apache.shiro.session.InvalidSessionException;
  15. import org.apache.shiro.session.Session;
  16. import org.apache.shiro.subject.Subject;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import com.google.common.collect.Sets;
  20. import com.jeeplus.common.web.Servlets;
  21. /**
  22. * 自定义授权缓存管理类
  23. * @author jeeplus
  24. * @version 2014-7-21
  25. */
  26. public class SessionCacheManager implements CacheManager {
  27. @Override
  28. public <K, V> Cache<K, V> getCache(String name) throws CacheException {
  29. return new SessionCache<K, V>(name);
  30. }
  31. /**
  32. * SESSION缓存管理类
  33. */
  34. public class SessionCache<K, V> implements Cache<K, V> {
  35. private Logger logger = LoggerFactory.getLogger(getClass());
  36. private String cacheKeyName = null;
  37. public SessionCache(String cacheKeyName) {
  38. this.cacheKeyName = cacheKeyName;
  39. }
  40. public Session getSession(){
  41. Session session = null;
  42. try{
  43. Subject subject = SecurityUtils.getSubject();
  44. session = subject.getSession(false);
  45. if (session == null){
  46. session = subject.getSession();
  47. }
  48. }catch (InvalidSessionException e){
  49. logger.error("Invalid session error", e);
  50. }catch (UnavailableSecurityManagerException e2){
  51. logger.error("Unavailable SecurityManager error", e2);
  52. }
  53. return session;
  54. }
  55. @SuppressWarnings("unchecked")
  56. @Override
  57. public V get(K key) throws CacheException {
  58. if (key == null){
  59. return null;
  60. }
  61. V v = null;
  62. HttpServletRequest request = Servlets.getRequest();
  63. if (request != null){
  64. v = (V)request.getAttribute(cacheKeyName);
  65. if (v != null){
  66. return v;
  67. }
  68. }
  69. V value = null;
  70. value = (V)getSession().getAttribute(cacheKeyName);
  71. logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
  72. if (request != null && value != null){
  73. request.setAttribute(cacheKeyName, value);
  74. }
  75. return value;
  76. }
  77. @Override
  78. public V put(K key, V value) throws CacheException {
  79. if (key == null){
  80. return null;
  81. }
  82. getSession().setAttribute(cacheKeyName, value);
  83. if (logger.isDebugEnabled()){
  84. HttpServletRequest request = Servlets.getRequest();
  85. logger.debug("put {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
  86. }
  87. return value;
  88. }
  89. @SuppressWarnings("unchecked")
  90. @Override
  91. public V remove(K key) throws CacheException {
  92. V value = null;
  93. value = (V)getSession().removeAttribute(cacheKeyName);
  94. logger.debug("remove {} {}", cacheKeyName, key);
  95. return value;
  96. }
  97. @Override
  98. public void clear() throws CacheException {
  99. getSession().removeAttribute(cacheKeyName);
  100. logger.debug("clear {}", cacheKeyName);
  101. }
  102. @Override
  103. public int size() {
  104. logger.debug("invoke session size abstract size method not supported.");
  105. return 0;
  106. }
  107. @Override
  108. public Set<K> keys() {
  109. logger.debug("invoke session keys abstract size method not supported.");
  110. return Sets.newHashSet();
  111. }
  112. @Override
  113. public Collection<V> values() {
  114. logger.debug("invoke session values abstract size method not supported.");
  115. return Collections.emptyList();
  116. }
  117. }
  118. }