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

Using cookies in js to implement the password remembering function

高洛峰
Release: 2016-12-09 13:23:56
Original
1562 people have browsed it

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 );
Copy after login

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);
}
Copy after login

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

Remember 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)
     }
Copy after login

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);
 }
}
Copy after login

automatically


Related labels:
js
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template