import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionAttributeListener ;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class SessionConfig implements HttpSessionListener, HttpSessionAttributeListener {
private static final Map<String, HttpSession> sessions = new ConcurrentHashMap<>();
public synchronized static String getSessionidCheck(String type, String compareId){
String result = "";
for( String key : sessions.keySet() ){
HttpSession hs = sessions.get(key);
if(hs != null && hs.getAttribute(type) != null && hs.getAttribute(type).toString().equals(compareId) ){
result = key.toString();
removeSessionForDoubleLogin(result);
}
}
return result;
}
private static void removeSessionForDoubleLogin(String sessionId){
if(sessionId!= null && sessionId.length() > 0){
sessions.get(sessionId).invalidate();
sessions.remove(sessionId);
}
}
@Override
public void attributeAdded(HttpSessionBindingEvent se){
String attributeName = se.getName();
if("userKey".equals(attributeName)) {
sessions.put(se.getSession().getId(), se.getSession());
}
}
@Override
public void sessionCreated(HttpSessionEvent se) {
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
if(sessions.get(se.getSession().getId()) != null){
sessions.get(se.getSession().getId()).invalidate();
sessions.remove(se.getSession().getId());
}
}
}
'좋은지식 > 잡학다식' 카테고리의 다른 글
[JAVA] UTF-8로 CSV 파일 생성 시 한글 깨짐 현상 (0) | 2022.10.25 |
---|---|
[Java] Tomcat Redis Session Cluster Manager (0) | 2022.07.08 |
깃허브 이클립스 연동 (0) | 2021.09.08 |
윈도우10 XPS 설치 방법 (0) | 2021.07.30 |
물류 용어 정리 (0) | 2021.07.05 |