ShiroRedisUtil.kt 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.gxzc.zen.umps.util
  2. import com.gxzc.zen.common.util.SpringContextHolder
  3. import org.apache.shiro.session.Session
  4. import org.apache.shiro.session.mgt.eis.SessionDAO
  5. /**
  6. * Shiro redis 工具类
  7. * @author NorthLan
  8. * @date 2018/5/3
  9. * @url https://noahlan.com
  10. */
  11. object ShiroRedisUtil {
  12. private var sessionDAO = SpringContextHolder.getBean(SessionDAO::class.java)
  13. get() {
  14. if (field == null) {
  15. field = SpringContextHolder.getBean(SessionDAO::class.java)
  16. }
  17. return field
  18. }
  19. fun getActiveSessions(): MutableCollection<Session> {
  20. return sessionDAO!!.activeSessions
  21. }
  22. private fun updateSession(session: Session) {
  23. sessionDAO!!.update(session)
  24. }
  25. fun removeAllSessionsAttributeKey(key: Any?) {
  26. val sessions = getActiveSessions()
  27. sessions.forEach { s ->
  28. s.setAttribute(key, null)
  29. updateSession(s)
  30. }
  31. }
  32. fun removeSessionAttributeKey(session: Session, key: Any?) {
  33. session.setAttribute(key, null)
  34. updateSession(session)
  35. }
  36. }