diff --git a/pom.xml b/pom.xml
index a564248..cf967cd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -118,6 +118,11 @@
poi-ooxml
5.2.3
+
+ org.apache.shiro
+ shiro-core
+ 1.10.0
+
diff --git a/src/main/java/com/keyware/htey/HteyApplication.java b/src/main/java/com/keyware/htey/HteyApplication.java
index 9503628..e524805 100644
--- a/src/main/java/com/keyware/htey/HteyApplication.java
+++ b/src/main/java/com/keyware/htey/HteyApplication.java
@@ -1,8 +1,10 @@
package com.keyware.htey;
+import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
@SpringBootApplication
@MapperScan({"com.keyware.htey.mybatis.itf"})
@@ -11,5 +13,8 @@ public class HteyApplication {
public static void main(String[] args) {
SpringApplication.run(HteyApplication.class, args);
}
-
+ @Bean
+ public MemorySessionDAO sessionDAO() {
+ return new MemorySessionDAO();
+ }
}
diff --git a/src/main/java/com/keyware/htey/controller/user/UserController.java b/src/main/java/com/keyware/htey/controller/user/UserController.java
index b39372d..063515a 100644
--- a/src/main/java/com/keyware/htey/controller/user/UserController.java
+++ b/src/main/java/com/keyware/htey/controller/user/UserController.java
@@ -1,9 +1,12 @@
package com.keyware.htey.controller.user;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
+import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -15,6 +18,7 @@ import com.keyware.htey.entity.department.Department;
import com.keyware.htey.entity.user.AuditLog;
import com.keyware.htey.entity.user.User;
import com.keyware.htey.entity.usersecret.UserSecret;
+import com.keyware.htey.listener.SessionCounter;
import com.keyware.htey.service.itf.AuditLogService;
import com.keyware.htey.service.itf.DepartmentService;
import com.keyware.htey.service.itf.UserService;
@@ -32,6 +36,9 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.annotations.Param;
+import org.apache.shiro.session.Session;
+import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@@ -64,8 +71,8 @@ public class UserController {
private DepartmentService departmentService;
@Autowired
private UsersecretService usersecretService;
- //@Autowired
- //private SessionDAO sessionDAO;
+ @Autowired
+ private SessionDAO sessionDAO;
/**
* @author yangmengchuan
@@ -866,19 +873,51 @@ public class UserController {
return ajaxMessage;
}
- @RequestMapping(
- value = {"/selectLoginList"},
- produces = {"application/json; charset=utf-8"}
- )
+ /**
+ * @param userNameForSerch
+ * @param pageNum
+ * @param pageSize
+ * @return java.util.List
+ * @description 查询已登录用户
+ * @Author daijiajun
+ * @date 2025/1/16
+ **/
+ @GetMapping("/selectLoginList")
@ResponseBody
@Operation(summary = "查询已登录用户", description = "返回查询已登录用户信息")
@ApiResponse(responseCode = "200", description = "查询已登录用户成功")
- public List selectLoginList(String userNameForSerch) {
- //Collection sessions = this.sessionDAO.getActiveSessions();
- List list = new ArrayList();
- Map maploginIp = new HashMap();
- Map maploginTime = new HashMap();
- //Iterator var7 = sessions.iterator();
- return new ArrayList<>();
+ public List selectLoginList(@RequestParam(required = false) String userNameForSerch,
+ @RequestParam(defaultValue = "0") int pageNum,
+ @RequestParam(defaultValue = "10") int pageSize) {
+ Collection sessions = this.sessionDAO.getActiveSessions();
+ List list = new ArrayList<>();
+ Map maploginIp = new HashMap<>();
+ Map maploginTime = new HashMap<>();
+ Iterator var7 = sessions.iterator();
+ while (var7.hasNext()) {
+ Session session = var7.next();
+ Date date = session.getStartTimestamp();
+ String loginIp = SessionCounter.sessionIpMap.get(session.getId());
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ String loginTime = formatter.format(date);
+ String userId = (String)session.getAttribute("user_id");
+ if (userId != null) {
+ maploginIp.put(userId, loginIp);
+ maploginTime.put(userId, loginTime);
+ list.add(userId);
+ }
+ }
+ if (list.size() == 0) {
+ return new ArrayList<>();
+ }
+ List userList = this.userService.selectAllLogin(list, userNameForSerch, pageNum,
+ pageSize);
+ Iterator var15 = userList.iterator();
+ while (var15.hasNext()) {
+ User user = var15.next();
+ user.setIP(maploginIp.get(user.getId()));
+ user.setLoginTime(maploginTime.get(user.getId()));
+ }
+ return userList;
}
}
diff --git a/src/main/java/com/keyware/htey/entity/user/User.java b/src/main/java/com/keyware/htey/entity/user/User.java
index 8f3a663..bbf3ac1 100644
--- a/src/main/java/com/keyware/htey/entity/user/User.java
+++ b/src/main/java/com/keyware/htey/entity/user/User.java
@@ -63,4 +63,8 @@ public class User implements Serializable {
private int pageSize;
@TableField(exist = false)
private String systemLevel;
+ @TableField(exist = false)
+ private String iP;
+ @TableField(exist = false)
+ private String loginTime;
}
diff --git a/src/main/java/com/keyware/htey/listener/SessionCounter.java b/src/main/java/com/keyware/htey/listener/SessionCounter.java
new file mode 100644
index 0000000..0685d6d
--- /dev/null
+++ b/src/main/java/com/keyware/htey/listener/SessionCounter.java
@@ -0,0 +1,135 @@
+package com.keyware.htey.listener;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequestEvent;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionEvent;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.util.StringUtils;
+
+/**
+ * @author daijiajun
+ * @version V1.0
+ * @description
+ * @Package com.keyware.htey.listener
+ * @date 2025/1/16 14:52
+ */
+@Slf4j
+public class SessionCounter {
+
+ private static final String CONTENT_TYPE = "text/html; charset=GBK";
+ public static int activeSessions = 0;
+ private HttpServletRequest request;
+ public static ArrayList list = new ArrayList();
+ public static HashMap sessionIpMap = new HashMap();
+
+ public SessionCounter() {
+ }
+
+ public void init() throws ServletException {
+ }
+
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ response.setContentType("text/html; charset=GBK");
+ }
+
+ public void destroy() {
+ }
+
+ public void requestDestroyed(ServletRequestEvent arg0) {
+ }
+
+ public void requestInitialized(ServletRequestEvent arg0) {
+ this.request = (HttpServletRequest)arg0.getServletRequest();
+ }
+
+ public void sessionCreated(HttpSessionEvent httpSessionEvent) {
+ HttpSession session = httpSessionEvent.getSession();
+ String sessionId = session.getId();
+ String userId = (String)session.getAttribute("userId");
+ if (StringUtils.hasText(userId)) {
+ String loginIp = this.request.getRemoteAddr();
+ boolean rs = true;
+ if (list.size() > 0) {
+ for(int i = 0; i < list.size(); ++i) {
+ if (loginIp.equals(list.get(i))) {
+ rs = false;
+ }
+ }
+ }
+
+ if (rs) {
+ list.add(loginIp);
+ ++activeSessions;
+ sessionIpMap.put(sessionId, loginIp);
+ Iterator var9 = list.iterator();
+
+ while(var9.hasNext()) {
+ String ip = (String)var9.next();
+ log.info(ip);
+ }
+
+ log.info("新增SESSION,sessionId = " + sessionId + "; loginIp = " + loginIp + "; 当前总SESSION值为 " + activeSessions);
+ }
+ }
+
+ }
+
+ public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
+ String sessionId = httpSessionEvent.getSession().getId();
+ String loginIp = null;
+
+ try {
+ loginIp = this.request.getRemoteAddr();
+ } catch (Exception var7) {
+ log.error("request异常,获取失效人员IP错误。");
+ }
+
+ if (activeSessions > 0) {
+ if (list.size() > 0) {
+ if (loginIp == null) {
+ String ip = (String)sessionIpMap.get(sessionId);
+ loginIp = ip;
+ Iterator> iterator = sessionIpMap.entrySet().iterator();
+
+ while(iterator.hasNext()) {
+ Map.Entry next = (Map.Entry)iterator.next();
+ if (((String)next.getValue()).equals(ip)) {
+ iterator.remove();
+ }
+ }
+ }
+
+ for(int i = 0; i < list.size(); ++i) {
+ if (((String)list.get(i)).equals(loginIp)) {
+ list.remove(i);
+ --i;
+ }
+ }
+ }
+
+ sessionIpMap.remove(sessionId);
+ --activeSessions;
+ log.info("销毁SESSION,sessionId = " + sessionId + "; loginIp = " + loginIp + "; 当前总SESSION值为 " + activeSessions);
+ if (activeSessions == 0) {
+ list.removeAll(list);
+ sessionIpMap = new HashMap();
+ }
+ }
+
+ }
+
+ public static int getActiveSessions() {
+ return activeSessions;
+ }
+}
+
diff --git a/src/main/java/com/keyware/htey/mybatis/itf/UserMapper.java b/src/main/java/com/keyware/htey/mybatis/itf/UserMapper.java
index e6ae085..033cad0 100644
--- a/src/main/java/com/keyware/htey/mybatis/itf/UserMapper.java
+++ b/src/main/java/com/keyware/htey/mybatis/itf/UserMapper.java
@@ -53,4 +53,7 @@ public interface UserMapper extends BaseMapper {
List selectByUserInfo(User var1);
List selectDepartUser(@Param("departId") String var1, @Param("projectSource") String var2);
+
+ List selectAllLogin(@Param("list") List var1, @Param("userNameForSerch") String var2,
+ @Param("userName") int userName, @Param("pageNum") int pageNum);
}
diff --git a/src/main/java/com/keyware/htey/service/impl/UserServiceImpl.java b/src/main/java/com/keyware/htey/service/impl/UserServiceImpl.java
index a3d9b9d..ae9f42d 100644
--- a/src/main/java/com/keyware/htey/service/impl/UserServiceImpl.java
+++ b/src/main/java/com/keyware/htey/service/impl/UserServiceImpl.java
@@ -9,6 +9,7 @@ import com.keyware.htey.mybatis.itf.UserRoleMapper;
import com.keyware.htey.service.itf.UserService;
import com.keyware.htey.utli.Constant;
import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -127,4 +128,9 @@ public class UserServiceImpl extends ServiceImpl implements Us
return this.userMapper.selectDepartUser(departId, Constant.projectName);
}
+ @Override
+ public List selectAllLogin(List list, String userNameForSerch, int userName, int pageNum) {
+ return this.userMapper.selectAllLogin(list, userNameForSerch, userName, pageNum);
+ }
+
}
diff --git a/src/main/java/com/keyware/htey/service/itf/UserService.java b/src/main/java/com/keyware/htey/service/itf/UserService.java
index 3ebb139..f97ebc4 100644
--- a/src/main/java/com/keyware/htey/service/itf/UserService.java
+++ b/src/main/java/com/keyware/htey/service/itf/UserService.java
@@ -4,6 +4,7 @@ import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
import com.keyware.htey.entity.user.User;
+import org.apache.ibatis.annotations.Param;
public interface UserService extends IService {
@@ -46,4 +47,7 @@ public interface UserService extends IService {
List selectByUserInfo(User var1);
List selectDepartUser(String var1);
+
+ List selectAllLogin(List var1, String var2, @Param("userName") int userName,
+ @Param("pageNum") int pageNum);
}
diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml
index 2805181..48a949a 100644
--- a/src/main/resources/mapper/UserMapper.xml
+++ b/src/main/resources/mapper/UserMapper.xml
@@ -603,4 +603,37 @@
and DEPART_ID=#{departId,jdbcType=VARCHAR}
+
+