import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SessionCheck {
private static SessionCheck sessionManager ;
//private static Thread sessionThread;
Hashtable<String, SessionUser> accessUserList = new Hashtable<String, SessionUser>();
//private boolean isRunning = false;
private SessionCheck() {
}
public static SessionCheck getInstance() {
if(sessionManager == null) {
sessionManager = new SessionCheck();
}
/*if(sessionThread == null){
sessionThread = new Thread( sessionManager);
sessionThread.start();
}
*/
return sessionManager;
}
/*
public void run() {
System.out.println("Session Check start.......");
try {
while(true) {
System.out.println("Session Check Begin....");
sessionCheckProc();
isRunning= true;
System.out.println("Session Check End....");
Thread.sleep(20000);
}
} catch (Exception e) {
} finally {
System.out.println("Session Check Terminated....");
isRunning = false;
}
}*/
/*
private void sessionCheckProc() {
// TODO Auto-generated method stub
// 30분 초과 접속시 모두로그아웃 시킴
Enumeration e = accessUserList.keys();
while(e.hasMoreElements()) {
String uid = (String)e.nextElement();
SessionUser user = (SessionUser)accessUserList.get(uid);
System.out.println( System.currentTimeMillis() - user.getLastAccessMiles() );
if( System.currentTimeMillis() - user.getLastAccessMiles() > 30000) {
// logout target
accessUserList.remove(uid);
System.out.println("Thread Remove user :"+uid);
}
}
}
*/
/**
*로그인을 시도 하였을 때 기존 로그인 정보를 삭제한다.
*새로 로그인 된 정보를 입력하여 처리한다.
* @param uid
*/
public void doLogin(String uid, String uname, String ipaddr, HttpSession session) {
SessionUser user = accessUserList.get(uid);
// 다른곳에서로그인 되어 있거나 , 세션이 끊어지지 않았다면 세션을 종료 시킨다.
if(user != null) {
try{
user.getSession().invalidate();
}catch(Exception e){
}
System.out.println("Remove user :"+uid);
accessUserList.remove(uid);
}
SessionUser newUser = new SessionUser();
newUser.setLastAccessMiles(System.currentTimeMillis());
newUser.setIpAddress(ipaddr);
newUser.setUserNm(uname);
newUser.setUserId(uid);
newUser.setSession(session);
System.out.println("Login comp ==>"+uid);
accessUserList.put(uid, newUser);
}
public boolean isLogin(String uid, String ip, String SESSION_ID) {
SessionUser user = accessUserList.get(uid);
if(user == null)
return false;
// 세션아이디 및 접속 아이피가 동일하여야 함.
if(user.getIpAddress().equals(ip) && user.getSessionId().equals(SESSION_ID)) {
// 최종 접속시간을 변경하여 준다.
user.setLastAccessMiles(System.currentTimeMillis());
return true;
}
return false;
}
public static String getJSessionID(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
try {
for(int i=0; i<cookies.length; i++) {
String cname = cookies[i].getName();
String value = cookies[i].getValue();
if(cname.toUpperCase().equals("JSESSIONID"))
return cookies[i].getValue();
}
} catch (Exception e) {
}
return null;
}
public List<SessionUser> getLoginUsers() {
List<SessionUser> users = new ArrayList<SessionUser>();
Enumeration e = accessUserList.keys();
while(e.hasMoreElements()) {
String uid = (String)e.nextElement();
SessionUser user = accessUserList.get(uid);
System.out.println(user.getUserId()+" "+user.getLastAccessMiles());
users.add(user);
}
Collections.sort(users);
return users;
}
}
댓글 없음:
댓글 쓰기