daijiajun 用户管理中查询已登录用户

main
admin 3 days ago
parent 58b6768ad1
commit 51328c28cd
  1. 5
      pom.xml
  2. 7
      src/main/java/com/keyware/htey/HteyApplication.java
  3. 65
      src/main/java/com/keyware/htey/controller/user/UserController.java
  4. 4
      src/main/java/com/keyware/htey/entity/user/User.java
  5. 135
      src/main/java/com/keyware/htey/listener/SessionCounter.java
  6. 3
      src/main/java/com/keyware/htey/mybatis/itf/UserMapper.java
  7. 6
      src/main/java/com/keyware/htey/service/impl/UserServiceImpl.java
  8. 4
      src/main/java/com/keyware/htey/service/itf/UserService.java
  9. 33
      src/main/resources/mapper/UserMapper.xml

@ -118,6 +118,11 @@
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.10.0</version>
</dependency>
</dependencies>
<build>

@ -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();
}
}

@ -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<com.keyware.htey.entity.user.User>
* @description 查询已登录用户
* @Author daijiajun
* @date 2025/1/16
**/
@GetMapping("/selectLoginList")
@ResponseBody
@Operation(summary = "查询已登录用户", description = "返回查询已登录用户信息")
@ApiResponse(responseCode = "200", description = "查询已登录用户成功")
public List<User> selectLoginList(String userNameForSerch) {
//Collection<Session> sessions = this.sessionDAO.getActiveSessions();
List<String> list = new ArrayList();
Map<String, String> maploginIp = new HashMap();
Map<String, String> maploginTime = new HashMap();
//Iterator var7 = sessions.iterator();
return new ArrayList<>();
public List<User> selectLoginList(@RequestParam(required = false) String userNameForSerch,
@RequestParam(defaultValue = "0") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
Collection<Session> sessions = this.sessionDAO.getActiveSessions();
List<String> list = new ArrayList<>();
Map<String, String> maploginIp = new HashMap<>();
Map<String, String> maploginTime = new HashMap<>();
Iterator<Session> 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<User> userList = this.userService.selectAllLogin(list, userNameForSerch, pageNum,
pageSize);
Iterator<User> var15 = userList.iterator();
while (var15.hasNext()) {
User user = var15.next();
user.setIP(maploginIp.get(user.getId()));
user.setLoginTime(maploginTime.get(user.getId()));
}
return userList;
}
}

@ -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;
}

@ -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<String> list = new ArrayList();
public static HashMap<String, String> 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<Map.Entry<String, String>> iterator = sessionIpMap.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, String> 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;
}
}

@ -53,4 +53,7 @@ public interface UserMapper extends BaseMapper<User> {
List<User> selectByUserInfo(User var1);
List<User> selectDepartUser(@Param("departId") String var1, @Param("projectSource") String var2);
List<User> selectAllLogin(@Param("list") List<String> var1, @Param("userNameForSerch") String var2,
@Param("userName") int userName, @Param("pageNum") int pageNum);
}

@ -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<UserMapper, User> implements Us
return this.userMapper.selectDepartUser(departId, Constant.projectName);
}
@Override
public List<User> selectAllLogin(List<String> list, String userNameForSerch, int userName, int pageNum) {
return this.userMapper.selectAllLogin(list, userNameForSerch, userName, pageNum);
}
}

@ -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<User> {
@ -46,4 +47,7 @@ public interface UserService extends IService<User> {
List<User> selectByUserInfo(User var1);
List<User> selectDepartUser(String var1);
List<User> selectAllLogin(List<String> var1, String var2, @Param("userName") int userName,
@Param("pageNum") int pageNum);
}

@ -603,4 +603,37 @@
and DEPART_ID=#{departId,jdbcType=VARCHAR}
</if>
</select>
<select id="selectAllLogin" parameterType="java.lang.String" resultMap="BaseResultMap" >
select
K_USER.ID,
K_USER.USER_ID,
K_USER.USER_NAME,
K_USER.PASSWORD,
K_USER.DEPART_ID,
K_USER.USER_CREATION_TIME,
K_USER.USER_PWD_MODIF_TIME,
K_USER.USER_STATUS,
K_USER.USER_LOCK,
K_USER.USER_PWD_ERROR,
K_USER.USER_PWD_ERROR_DATE,
K_USER.EXIT_TIME,K_USER.PASSWORD_OLD,
K_USER.POST, K_USER.EMAIL, K_USER.OFFICE_TELEPHONE,
K_USER.USER_NUMBER, K_USER.ID_CARD, K_USER.MOBILE_PHONE,
K_USER.SEX, K_USER.HOME_PHONE,K_USER.LOGOUT,K_USER.INIT_PWD_TIME,K_USER.SORT ,K_USER.LOGOUT_TIME,
K_USER.UESR_RANK_ID,
DEPARTMENT.DEPT_NAME
from K_USER
left join DEPARTMENT on K_USER.DEPART_ID = DEPARTMENT.ID
where 1=1
<if test="userNameForSerch != null and userNameForSerch != ''">
and (K_USER.USER_NAME like '%'||#{userNameForSerch,jdbcType=VARCHAR}||'%' )
</if>
and K_USER.ID in <foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
LIMIT #{pageNum},#{pageSize}
</foreach>
</select>
</mapper>

Loading…
Cancel
Save