SessionServiceImpl.java 3.7 KB

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