Java Web 自動登錄


一、客戶端選擇自動登錄復選框,LoginServlet得到數據后,判斷是否選擇了復選框,若成功選中則創建cookies對象,並添加到響應頭中

 

 1 //若用戶選擇自動登錄,則生成cookies保存必要信息
 2                 if("auto".equals(autoLogin)){
 3          //cookies需要設置編碼格式

             Cookie cookie = new Cookie("username", URLEncoder.encode(username, "utf-8"));
 4                     Cookie cookie2 = new Cookie("password",password);
 5                     //設置保存時間
 6                     cookie.setMaxAge(7*24*60*60);
 7                     cookie2.setMaxAge(7*24*60*60);
 8                     //設置保存路徑
 9                     cookie.setPath(request.getContextPath()+"/");
10                     //添加到響應頭
11                     response.addCookie(cookie);
12                     response.addCookie(cookie2);
13                 }

 

 

二、下次當用戶來到Login.jsp后,會從cookies(每一個站點有唯一的cookies)中判斷是否有存入的username和password,若有則直接發送給LoginServlet判斷,若沒有則執行普通登錄操作(JSP中寫代碼)

 

 1 <%
 2    Cookie[] cookies=request.getCookies();
 3    String username = null;
 4    String password = null;
 5    if(cookies!=null){
 6            for(int i=0;i<cookies.length;i++){
 7                String name = cookies[i].getName();
 8                if("username".equals(name)){
 9          //如果是中文,cookies需要解碼

            username = URLDecoder.decode(cookies[i].getValue(), "utf-8");
10                }else if("password".equals(name)){
11                    password = cookies[i].getValue();
12                }
13            }
14    }
15    //當用戶名和密碼不為空時,自動登錄
16    if((username!=null&&!("".equals(username)))&&(password!=null&&!("".equals(password)))){
17            session.setAttribute("username", username);
18            session.setAttribute("password", password);
19            response.sendRedirect(request.getContextPath()+"/LoginServlet");//get請求
20    }
21  %>


 三、LoginServlet做自動登錄檢驗,同樣是傳值到數據庫校驗,此時,若校驗成功則帶着用戶名信息到Index.jsp,若檢驗失敗則重定向至exitServlet執行刪除cookies操作

 1 //1.通過session得到參數
 2         HttpSession session = request.getSession();
 3         String username = (String)session.getAttribute("username");
 4         String password = (String)session.getAttribute("password");
 5         //進行if判斷,防止有用戶直接通過URL帶參數的形式進行訪問
 6         if(username!=null&&password!=null){
 7         //2.連接數據庫進行數據校驗
 8             User user = LogSercice.Instance().checkLogin(username,password);
 9             if(user==null){
10                 //返回處理刪除cookies的servlet
11                 response.sendRedirect(request.getContextPath()+"/exitServlet");
12             }else {
13                 //登陸成功,跳轉到主頁面,並顯示"歡迎您,XXX"
14                 
15                 response.sendRedirect(request.getContextPath()+"/Index.jsp");
16             }
17         }
18         
19     }

 

四、若失敗至SexitServlet刪除cookies 

 

 1 //1.刪除會話
 2         HttpSession session = request.getSession();
 3         session.invalidate();
 4         //2.得到cookies
 5         Cookie[] cookies = request.getCookies();
 6         //3.遍歷刪除cookies中的值
 7         for(int i=0;i<cookies.length;i++){
 8             Cookie cookie = cookies[i];
 9             cookie.setMaxAge(0);
10             cookie.setValue(null);
11             cookie.setPath(request.getContextPath()+"/");
12             response.addCookie(cookie);
13         }
14         response.sendRedirect(request.getContextPath()+"/Login.jsp");
15     }

 

 

 

五、當成功進入到Index.jsp中,此時想按下超鏈接退出登錄(即下次不再執行自動登錄時), 超鏈接的href直接鏈接至exitServlet即可

1 href="<%=basePath%>exitServlet"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM