經過Session和Cookie配合實現簡單的自動登陸

使用Spring MVC框架,Session和Cookie本身管理html

Cookie管理的代碼
public class CookieUtil {

    public static int COOKIE_MAX_AGE = 60 * 60;//3600s

    public static void addCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie != null)
            response.addCookie(cookie);
    }

    public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain, boolean httpOnly, int maxAge, String path, boolean secure) {
        if (cookieName != null && !cookieName.equals("")) {
            if (cookieValue == null) {
                cookieValue = "";
            }

            Cookie newCookie = new Cookie(cookieName, cookieValue);
            if (domain != null) {
                newCookie.setDomain(domain);
            }
            newCookie.setHttpOnly(httpOnly);
            if (maxAge > 0) {
                newCookie.setMaxAge(maxAge);
            }
            if (path == null) {
                newCookie.setPath("/");
            } else {
                newCookie.setPath(path);
            }
            newCookie.setSecure(secure);
            addCookie(response, newCookie);
        }
    }

    public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain) {
        addCookie(response, cookieName, cookieValue, domain, true, COOKIE_MAX_AGE, "/", false);
    }

    public static Cookie getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookieName == null || cookieName.equals("")) {
            return null;
        }
        for (Cookie c : cookies) {
            if (c.getName().equals(cookieName))
                return c;
        }
        return null;
    }

    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        Cookie cookie = getCookie(request, cookieName);
        if (cookie == null) {
            return null;
        } else {
            return cookie.getValue();
        }
    }

    public static void deleteCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie != null) {
            cookie.setPath("/");
            cookie.setMaxAge(0);
            cookie.setValue(null);
            response.addCookie(cookie);
        }
    }

    public static void delCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
        Cookie c = getCookie(request, cookieName);
        if (c != null && c.getName().equals(cookieName)) {
            deleteCookie(response, c);
        }
    }

    public static void editCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, String domain) {
        Cookie c = getCookie(request, cookieName);
        if (c != null && cookieName != null && !cookieName.equals("") && c.getName().equals(cookieName)) {
            addCookie(response, cookieName, cookieValue, domain);
        }
    }
}
複製代碼
Session的管理代碼
public class SessionUtil {
    private static HashMap<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();

    public static synchronized void addSession(HttpSession session) {
        if (session != null) {
            sessionMap.put(session.getId(), session);
        }
    }

    public static synchronized void deleteSession(HttpSession session) {
        if (session != null) {
            sessionMap.remove(session.getId());
        }
    }

    public static HttpSession getSession(String sessionID) {
        if (sessionID == null) {
            return null;
        }
        return sessionMap.get(sessionID);
    }

    public static HashMap<String, HttpSession> getSessionMap() {
        return sessionMap;
    }
}
複製代碼
開始模擬自動登陸
  1. 首先要監聽Session的建立和摧毀
public class SessionListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("建立session......"+se.getSession());
        SessionUtil.addSession(se.getSession());
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("摧毀session......"+se.getSession());
        SessionUtil.deleteSession(se.getSession());
    }
}
複製代碼

而後在web.xml中註冊listenerweb

<listener>
    <listener-class>com.reet.listener.SessionListener</listener-class>
</listener>
複製代碼
  1. 模擬手動登陸 這裏設置Cookie存在的時常爲60s
@RequestMapping("/manual_login")
    public String cookie(HttpServletRequest request,HttpServletResponse response) {

        HttpSession session = request.getSession(true);
        CookieUtil.addCookie(response,"JSESSIONID",session.getId(),"localhost", true, 60, "/", false);

        return "forward:/index.html";
    }
複製代碼
  1. 模擬自動登陸
@RequestMapping("/auto_login")
    public String autoLogin(HttpServletRequest request, HttpServletResponse response) {
        String sessionID = CookieUtil.getCookieValue(request,"JSESSIONID");
        if(sessionID==null||sessionID.equals("")){
            return "forward:/login";
        }
        HttpSession session = SessionUtil.getSession(sessionID);
        if(session==null){
            return "forward:/login";
        }
        return "welcome";
    }
複製代碼
  • 這裏就是若是一開始沒有調用/manual_login,那麼cookie爲空,sessionID就必定爲空,自動forward到輸入帳號密碼的界面
  • 當調用了/manual_login,這時在刷新/auto_login,就會自動跳轉到welcome界面
  • 這時等待60s,再次刷新界面,因爲cookie已經失效,這時也就會再次forward到輸入帳號密碼的界面了
相關文章
相關標籤/搜索