중복 로그인 - alert

2022. 2. 22. 16:45spring


EgovHttpSessionBindingListener.java

package egovframework.com.cmm.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class EgovHttpSessionBindingListener implements HttpSessionBindingListener {
	
	public static Map<String, String> expireUserMap = new  HashMap<>();

	@Override
	public void valueBound(HttpSessionBindingEvent event) {
		if (EgovMultiLoginPreventor.findByLoginId(event.getName())) { // 중복 로그인
			HttpSession expireSession = EgovMultiLoginPreventor.loginUsers.get(event.getName());
			expireUserMap.put(expireSession.toString(), "Y");
		} else {
			expireUserMap.put(event.getSession().toString(), "N");
		}
		EgovMultiLoginPreventor.loginUsers.put(event.getName(), event.getSession());
	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent event) {
		EgovMultiLoginPreventor.loginUsers.remove(event.getName(), event.getSession());
	}
}


EgovMultiLoginPreventor.java

package egovframework.com.cmm.util;

import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpSession;

public class EgovMultiLoginPreventor {

	public static ConcurrentHashMap<String, HttpSession> loginUsers = new ConcurrentHashMap<String, HttpSession>();

	public static boolean findByLoginId(String loginId) {
		return loginUsers.containsKey(loginId);
	}

	public static void invalidateByLoginId(String loginId) {
		Enumeration<String> e = loginUsers.keys();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			if (key.equals(loginId)) {
				loginUsers.get(key).invalidate();
			}
		}
	}
}


EgovSessionAspect.java

@Aspect
@Component
@Slf4j
public class EgovSessionAspect {

	/**
	 * 세션 체크
	 * 
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around("execution(public * ocps..*Controller.*(..))")
	public Object checkSession(ProceedingJoinPoint joinPoint) throws Throwable {

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();

		String methodName = joinPoint.getSignature().getName();
		String fullClassName = joinPoint.getSignature().toShortString();

		Object[] args = joinPoint.getArgs();
		ModelMap modelMap = null;
		int pos = 0;
		for (Object object : args) {
			if (object instanceof ModelMap) {
				modelMap = (ModelMap) object;
				break;
			}
			pos++;
		}

		if (modelMap != null) {
			SessionVO sessionVO = (SessionVO) EgovUserDetailsHelper.getAuthenticatedUser();
			modelMap.addAttribute("sessionVO", sessionVO);

			// 중복 로그인 체크
			if (sessionVO != null && !MapUtils.isEmpty(EgovHttpSessionBindingListener.expireUserMap)) {

				HttpSession session = request.getSession();
				String expireYn = EgovHttpSessionBindingListener.expireUserMap.get(session.toString());
				
				if ("Y".equals(expireYn)) { // 중복 로그인 발생

					EgovHttpSessionBindingListener.expireUserMap.remove(session.toString());
					session.invalidate(); // 기존 사용자 세션 invalidate()

					response.setContentType("text/html;charset=utf-8");
					PrintWriter printwriter = response.getWriter();
					printwriter.println("<html>");
					printwriter.println("<script type=\"text/javaScript\">");
					printwriter.println("alert('중복 로그인이 발생하여 로그아웃 되었습니다.')");
					printwriter.println("window.location = '/main.do';");
					printwriter.println("</script>");
					printwriter.println("</html>");
					printwriter.flush();
					printwriter.close();
				}
			}
		}
     }