Remember password function to achieve Cookie

First, what is Cookie

A cookie is a WEB server through the browser information stored on a visitor's hard drive means. Cookie's purpose is to provide users with convenient, value-added opportunities on your site. Although there are a lot of misinformation, Cookie and in fact will not cause a serious security threat. Cookie never performed in any way, and therefore will not bring the virus or attack your system. In addition, because the browser is generally only allows storage of 300 Cookie, each site to store up to 20 Cookie, Cookie of each size is limited to 4KB, Cookie and therefore will not fill your hard disk.

For example, when we first visit the site to enter a user name and password, you can choose to remember the username and password, next time do not re-enter, and this is typical of Cookie applications.

 

Two, Cookie benefits:

The benefits of cookies to the site and users very much:

  • 1. Cookie can track specific number of visits to the site visitors, last access time, etc.
  • 2. Cookie can tell the number of online advertisers ad is clicked, allowing more accurate advertising
  • When the expiration date yet to come 3. Cookie, Cookie enables the user to enter some of the sites you've visited in without typing a user name and password
  • 4. Cookie site statistics can help a user profile in order to achieve a wide range of personalized services in a JSP, we can also use the Cookie, to write some powerful applications.

Some browsers can disable Cookie, Cookie can not be used to complete the core business.

 Third, the main method Cookie class:

int getMaxAge () returns the maximum time before the Cookie expires, in seconds. 
int setMaxAge () Set Cookie expiration time, in seconds. 
String getName () returns the name of Cookie 
String getValue () returns the value of Cookie. 
a new set of values to create a void setValue (String newValue) cookie.

 

Fourth, remember the password function code implementation:  

 The front page code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
 <head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Excel数据上传系统-登录</title>
	<link href="${pageContext.request.contextPath}/content/css/page/login.css" rel="stylesheet" type="text/css" />
 </head>
 
 <body>
 	<div class="second_body">
    	<form data-bind="submit:loginClick">
        	<div class="logo"></div>
            <div class = "Message" = the bind-Data "HTML: Message"> </ div>
            data upload system </ div><div class = "title-zh
            <div class = "title-EN" style = ""> the Data Excel the Upload the System </ div>
            <table border="0" style="width:300px;">
            	<tr>
                	<td style="white-space:nowrap; padding-bottom: 5px;width:55px;">用户名:</td>
                    <td colspan="2"><input type="text" id="userCode" class="login" data-bind="value:form.userCode" /></td>
                </tr>
                <tr>
                    <td class="lable" style="white-space:nowrap; letter-spacing: 0.5em; vertical-align: middle">密码:</td>
                    <td colspan="2"><input type="password" id="password" class="login" data-bind="value:form.password" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="2"><input type="checkbox" data-bind="checked:form.remember" /><span>系统记住我</span></td>
                </tr>
                <tr>
                    <td colspan="3" style="text-align:center">
                        <input type="submit" value="登录" class="login_button" />
                        <input type="button" value="重置" class="reset_botton" data-bind="click:resetClick" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
        
 	<script type="text/javascript" src="${pageContext.request.contextPath}/content/js/jquery/jquery-1.8.1.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/ligerUI/lib/jquery.cookie.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/content/js/core/utils.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/content/js/core/common.js"></script> 
    <script type="text/javascript" src="${pageContext.request.contextPath}/content/js/core/knockout-3.4.1.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/login.js"></script>
 	<script type="text/javascript">
         $(function () {
             ko.applyBindings(new viewModel());
         });
     </script>
 </body>
 
</html>       

 Back office processing code:

 

//登录查询
	@RequestMapping(value={"/doLogin"},method ={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public AjaxJson doLogin(@RequestBody Map<String, String> json,HttpServletRequest request,HttpServletResponse response){
		AjaxJson resutl = new AjaxJson();
		String loginName = json.get("userCode");
	    String password = json.get("password");
	    //用户名密码检查
        if ( StringUtils.isBlank(loginName) || StringUtils.isBlank(password)){
        	resutl.setSuccess(false);
			resutl.setMsg("用户名或密码不能为空!");
        }else {
        	String passwordEncode = MD5Util.MD5Encode(password, "utf-8");
     		ExcelUser user = null;
     		= loginService.findUserByLoginName User (loginName); 
     		IF (User == null) { 
     			resutl.setSuccess (to false); 
     			resutl.setMsg ( "the user name does not exist!"); 
     		} the else { 
 				User = loginService.findUserByNameAndPwd (loginName, passwordEncode ); 
 	     		IF (User == null) { 
 	     			resutl.setSuccess (to false); 
 	     			resutl.setMsg ( "wrong password, please re-enter"); 
 	     		} the else { 
 	     			! IF (user.getUser_status () = null && user.getUser_status ( ) .equals ( "0")) { 
 	     				resutl.setSuccess (false); 
 	         			resutl.setMsg ( "the user name is disabled, contact your administrator!"); 
 	     			} the else { 
 	     				resutl.setObj (the user); 
     	     			the Map <String , String>map = new HashMap<>(); 
     					map.put ( "userCode", loginName) ; 
     					map.put ( "userName", user.getUser_name());
     					map.put("isOauthUser", "0");
     					JSONObject jsonObject=JSONObject.fromObject(map);
     					System.out.println(jsonObject.toString());
     					request.getSession().setAttribute("LOGIN_USER",jsonObject);
     					//创建两个Cookie对象
     		                   Cookie nameCookie = new Cookie("userCode", loginName);
     		                   Cookie pwdCookie = new Cookie("password", password);
     					if("true".equals(String.valueOf(json.get("remember")))){
     						//设置Cookie的有效期为10天
     						nameCookie.setMaxAge(60 * 60 * 24 * 10);
     						pwdCookie.setMaxAge(60 * 60 * 24 * 10);
     						response.addCookie(nameCookie);
          		                   response.addCookie(pwdCookie);
     			        	// make cookie Failure
     			        the else {}
     			        	nameCookie.setMaxAge(0);
     					pwdCookie.setMaxAge(0);
     					response.addCookie(nameCookie);
          		                response.addCookie(pwdCookie);
     			        }
 	     			}
 	     			
 	     		}
     		}
     		
        }
		return resutl;
	}

 After a successful login, and then quit, then again there is no need to enter a user name and password! 

If this article helpful to you, please feel free to look a reward as an incentive, I will continue to work in! ! !

Guess you like

Origin www.cnblogs.com/zhangliang88/p/11202618.html