首先,我們需要在登入介面下面做一個複選框,勾選上以後,則自動登入2周,除非點退出後,才取消自動登入
看登入的Action代碼
…………if (!(lf.get("auto_login") == null)) {int times = 1000 * 60 * 60 * 24 * 14;AutoLoginModel alm = new AutoLoginModel();alm.setUsername(username);alm.setSessionid(hs.getId());alm.setTimes(new Date().getTime() + times);Cookie cookie = new Cookie("auto_users", username);cookie.setPath("/");cookie.setMaxAge(times);response.addCookie(cookie);cookie = new Cookie("auto_id", hs.getId());cookie.setPath("/");cookie.setMaxAge(times);response.addCookie(cookie);us.addLoginInfo(alm);}return mapping.findForward("to_login_suc");…………
如上程式碼片段
1、判斷是否選中自動登入,如果是則把使用者名稱,這次登入的SESSIONID及到期時間封裝存到資料庫中,然後產生使用者名稱及SESSIONID的Cookie
2、注意一定要setPath("/");為什麼呢?因為不這樣做的話,cookie的域不一樣,其他頁面則無法取到cookie
這樣,登入時的Cookie就已經記錄好了,然後怎麼判斷自動登入呢?
我們需要寫一個過濾器,用來監聽所有請求,然後當發現有Cookie存在的時候,並且裡面有記錄使用者名稱和密碼
然後我們把Cookie取出來,同時我們需要判斷此時session裡有沒有使用者登入資訊,如果有,則什麼都不做,如果沒有,則繼續步驟,再從資料庫中取出上次記錄的資訊,然後對比,如果Cookie中的使用者名稱和SESSIONID與從資料庫讀出的最新記錄的一樣以及和目前時間的毫秒數做對比如果沒到期,則取出使用者資訊,並儲存在SESSION中,這樣,自動登入就完成了!
我們看看攔截器的代碼
…………public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) arg0;Cookie[] cookies = request.getCookies();if (cookies != null) {if (cookies.length > 1) {HttpSession hs = request.getSession();UsersModel um = (UsersModel) hs.getAttribute("users_info");if (um == null) {String username = "";String sessionid = "";for (int i = 0; i < cookies.length; i++) {Cookie cookie = cookies[i];if (cookie.getName().equalsIgnoreCase("auto_users")) {username = cookie.getValue(); // 得到cookie的使用者名稱}if (cookie.getName().equalsIgnoreCase("auto_id")) {sessionid = cookie.getValue(); // 得到cookie的sessionid}}if (!(username.equals("") || sessionid.equals(""))) {String store_path = request.getServletContext().getRealPath("/WEB-INF/");ApplicationContext context = new FileSystemXmlApplicationContext(store_path + "/applicationContext.xml");UsersService us = (UsersService) context.getBean("usersService");AutoLoginModel alm = us.getLoginInfo(username);if (alm.getUsername().equals(username)&& alm.getSessionid().equals(sessionid)&& new Date().getTime() < alm.getTimes()) {hs.setAttribute("users_info", us.getUsers(username));} else {HttpServletResponse response = (HttpServletResponse) arg1;for (Cookie cookie : cookies) {if ("auto_users".equals(cookie.getName())) {cookie = new Cookie("auto_users", "");cookie.setPath("/");cookie.setMaxAge(0);response.addCookie(cookie);}if ("auto_id".equals(cookie.getName())) {cookie = new Cookie("auto_id", "");cookie.setPath("/");cookie.setMaxAge(0);response.addCookie(cookie);}}}}}}}arg2.doFilter(arg0, arg1);}………………
至於過濾器如何配置,這裡就不多講了
最後在退出的時候
清除Cookie,刪除資料庫中儲存的記錄
…………HttpSession hs = request.getSession();UsersModel um = (UsersModel) hs.getAttribute("users_info");Cookie[] cookies = request.getCookies();if (cookies != null) {for (Cookie cookie : cookies) {if ("auto_users".equals(cookie.getName())) {cookie = new Cookie("auto_users", "");cookie.setMaxAge(0);cookie.setPath("/");response.addCookie(cookie);}if ("auto_id".equals(cookie.getName())) {cookie = new Cookie("auto_id", "");cookie.setMaxAge(0);cookie.setPath("/");response.addCookie(cookie);}}us.delLoginInfo(um.getUsername());}hs.invalidate();return (mapping.findForward("to_login"));…………
自動登入的功能就完成了,思考了一天的結果,呵呵