JavaWeb學習筆記4——Cookie永久登入案例
阿新 • • 發佈:2018-12-08
當用戶在自家上網時,應該不難發現,在你第一次登入後,下次訪問就不需要再次登入,這是因為在第一次登入時網站就記住了你的登入資訊。實現這個功能其實很簡單,只需要把登入資訊,比如賬號、密碼等儲存在Cookie中,並控制Cookie的有效期,下一次訪問時再驗證Cookie中的登入資訊就可以了。
但是直接把密碼儲存在Cookie的做法是比較危險的,所以我們今天的案例採用了另一種方法:把賬號按照一定的規則進行加密後儲存到Cookie中,下次訪問時只需要判斷帳號的加密規則是否正確。本例把賬號儲存到名為account的Cookie中,把賬號連同金鑰用MD5演算法加密後儲存到名為ssid的Cookie中,驗證時,驗證Cookie中的賬號與金鑰加密後是否與Cookie中的ssid相等。
程式碼如下:
loginCookie.jsp
<%@ page language="java" pageEncoding="UTF-8" isErrorPage="false"%> <%@ page import="java.security.MessageDigest" %> <%!private static final String KEY = ":[email protected]"; //金鑰 //md5加密演算法 public final static String calcMD5(String ss) { String s = ss == null ? "" : ss; char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { byte[] strTemp = s.getBytes(); MessageDigest mdTemp = MessageDigest.getInstance("MD5"); mdTemp.update(strTemp); byte[] md = mdTemp.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { return null; } }%> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String action = request.getParameter("action"); if ("login".equals(action)) { //獲取各種引數 String account = request.getParameter("account"); String password = request.getParameter("password"); int timeout = new Integer(request.getParameter("timeout")); String ssid = calcMD5(account + KEY); //將賬號存入accountCookie Cookie accountCookie = new Cookie("account", account); accountCookie.setMaxAge(timeout); //將賬號、金鑰加密後存入accountCookie Cookie ssidCookie = new Cookie("ssid", ssid); ssidCookie.setMaxAge(timeout); response.addCookie(accountCookie); response.addCookie(ssidCookie); //重新請求本頁面 response.sendRedirect(request.getRequestURI() + "?" + System.currentTimeMillis()); return; } else if ("logout".equals(action)) { Cookie accountCookie = new Cookie("account", ""); accountCookie.setMaxAge(0);//設定有效期為0,即刪除 Cookie ssidCookie = new Cookie("ssid", ""); ssidCookie.setMaxAge(0);//設定有效期為0,即刪除 response.addCookie(accountCookie); response.addCookie(ssidCookie); //重新請求本頁面 response.sendRedirect(request.getRequestURI() + "?" + System.currentTimeMillis()); return; } boolean login = false; String account = null; String ssid = null; if (request.getCookies() != null) { for (Cookie cookie : request.getCookies()) { if (cookie.getName().equals("account")) account = cookie.getValue(); if (cookie.getName().equals("ssid")) ssid = cookie.getValue(); } } if (account != null && ssid != null) { login = ssid.equals(calcMD5(account + KEY));//如果加密規則正確,則login置為true,已登入 } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <legend><%=login ? "歡迎回來" : "請先登入"%></legend> <% if (login) { %> 歡迎您,${cookie.account.value}. <a href="${pageContext.request.requestURI }?action=logout"> 登出</a> <% } else { %> <form action="${pageContext.request.requestURI }?action=login" method="post"> <table> <tr> <td>賬號:</td> <td><input type="text" name="account" style="width:200px;"></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"></td> </tr> <tr> <td>有效期:</td> <td><input type="radio" name="timeout" value=-1 checked> 關閉瀏覽器即失效 <br /> <input type="radio" name="timeout" value="<%=30 * 24 * 60 * 60%>">30天內有效 <br /> <input type="radio" name="timeout" value="<%=Integer.MAX_VALUE%>">永久有效 <br /></td> </tr> <tr> <td></td> <td><input type="submit" value="登入" class="button"></td> </tr> </table> </form> <%} %>
登陸時可對登入資訊的有效期進行選擇,這是通過設定Cookie的age屬性來實現的,執行結果如圖: