SessionServiceImpl.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.bootdo.system.service.impl;
  2. import com.bootdo.system.domain.UserDO;
  3. import com.bootdo.system.domain.UserOnline;
  4. import com.bootdo.system.domain.UserToken;
  5. import com.bootdo.system.service.SessionService;
  6. import org.apache.shiro.session.Session;
  7. import org.apache.shiro.session.mgt.eis.SessionDAO;
  8. import org.apache.shiro.subject.SimplePrincipalCollection;
  9. import org.apache.shiro.subject.support.DefaultSubjectContext;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import java.security.Principal;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.List;
  16. /**
  17. * 待完善
  18. *
  19. * @author bootdo
  20. */
  21. @Service
  22. public class SessionServiceImpl implements SessionService {
  23. private final SessionDAO sessionDAO;
  24. @Autowired
  25. public SessionServiceImpl(SessionDAO sessionDAO) {
  26. this.sessionDAO = sessionDAO;
  27. }
  28. @Override
  29. public List<UserOnline> list() {
  30. List<UserOnline> list = new ArrayList<>();
  31. Collection<Session> sessions = sessionDAO.getActiveSessions();
  32. for (Session session : sessions) {
  33. UserOnline userOnline = new UserOnline();
  34. if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
  35. continue;
  36. } else {
  37. SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) session
  38. .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
  39. UserDO userDO = (UserDO) principalCollection.getPrimaryPrincipal();
  40. userOnline.setUsername(userDO.getUsername());
  41. }
  42. userOnline.setId((String) session.getId());
  43. userOnline.setHost(session.getHost());
  44. userOnline.setStartTimestamp(session.getStartTimestamp());
  45. userOnline.setLastAccessTime(session.getLastAccessTime());
  46. userOnline.setTimeout(session.getTimeout() / (1000L * 60L));
  47. list.add(userOnline);
  48. }
  49. return list;
  50. }
  51. @Override
  52. public List<UserDO> listOnlineUser() {
  53. List<UserDO> list = new ArrayList<>();
  54. UserDO userDO;
  55. Collection<Session> sessions = sessionDAO.getActiveSessions();
  56. for (Session session : sessions) {
  57. SimplePrincipalCollection principalCollection = new SimplePrincipalCollection();
  58. if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
  59. continue;
  60. } else {
  61. principalCollection = (SimplePrincipalCollection) session
  62. .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
  63. userDO = (UserDO) principalCollection.getPrimaryPrincipal();
  64. list.add(userDO);
  65. }
  66. }
  67. return list;
  68. }
  69. @Override
  70. public Collection<Session> sessionList() {
  71. return sessionDAO.getActiveSessions();
  72. }
  73. @Override
  74. public boolean forceLogout(String sessionId) {
  75. Session session = sessionDAO.readSession(sessionId);
  76. session.setTimeout(0);
  77. return true;
  78. }
  79. }