Home  >  Article  >  Web Front-end  >  Using cookies in js to implement the password remembering function

Using cookies in js to implement the password remembering function

高洛峰
高洛峰Original
2016-12-09 13:23:561488browse

To add the remember password function to the login interface, the first thing I thought of was to call cookies in the java background to store the account password, which is roughly as follows:

HttpServletRequest request
HttpServletResponse response
Cookie username = new Cookie("username ","cookievalue");
Cookie password = new Cookie("password ","cookievalue");
response.addCookie(username );
response.addCookie(password );

But for security reasons, most of the passwords we obtain in the background are in js In the ciphertext encrypted by MD5, if you put the ciphertext in a cookie, it will have no effect if you get it in js;

Then consider accessing cookies in js, the code is as follows:

//设置cookie
var passKey = '4c05c54d952b11e691d76c0b843ea7f9';
function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires="+d.toUTCString();
  document.cookie = cname + "=" + encrypt(escape(cvalue), passKey) + "; " + expires;
}
//获取cookie
function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==&#39; &#39;) c = c.substring(1);
    if (c.indexOf(name) != -1){
     var cnameValue = unescape(c.substring(name.length, c.length));
     return decrypt(cnameValue, passKey);
    }
  }
  return "";
}
//清除cookie
function clearCookie(cname) {
  setCookie(cname, "", -1);
}

setCookie( cname, cvalue, exdays) are the stored cookie name, cookie value, and cookie validity days. Since cookies cannot contain special characters such as equal signs, spaces, and semicolons, I use the escape() function when setting cookies. Encode the string and use the unescape() function to decode it when getting the cookie. However, the escape() function does not encode ASCII letters and numbers, so the account number and password stored in the cookie are stored in clear text, which is not safe. So I went online to find an algorithm for encrypting and decrypting strings. This algorithm requires passing two parameters, a string that needs to be encrypted, and a custom encryption key passKey. Use encrypt(value, passkey) to encrypt when setting a cookie, and use decrypt(value, passKey) to decrypt when reading a cookie. The algorithm is attached at the end of this article.

Call to access cookie method:

1. Define checkbox

e82075ce4b8038762d5af2d134fe978cRemember password

2. Determine whether the account password is entered correctly Then call

if($(&#39;#rememberMe&#39;).is(&#39;:checked&#39;)){
      setCookie(&#39;customername&#39;, $(&#39;#username&#39;).val().trim(), 7)
      setCookie(&#39;customerpass&#39;, $(&#39;#password&#39;).val().trim(), 7)
     }

3. After entering the login interface, determine whether there is an account password in the cookie, and if so, fill it in

$(function(){
 
 //获取cookie
 var cusername = getCookie(&#39;customername&#39;);
 var cpassword = getCookie(&#39;customerpass&#39;);
 if(cusername != "" && cpassword != ""){
  $("#username").val(cusername);
  $("#password").val(cpassword);
 }
}

automatically


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn