parent
58b6768ad1
commit
51328c28cd
@ -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; |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue