Use cookies in js to remember passwords

Source: Internet
Author: User
Js uses cookies to implement the Remember password function. In addition to the Remember password function on the login interface, I first thought of calling the cookie in the java background to store the account password, which is roughly as follows:

HttpServletRequest requestHttpServletResponse responseCookie username = new Cookie("username ","cookievalue");Cookie password = new Cookie("password ","cookievalue");response.addCookie(username );response.addCookie(password );

However, for the sake of security, the passwords we obtain in the background are mostly the ciphertext encrypted by MD5 in js. If we put the ciphertext in the cookie, it does not work in js;

Then, consider accessing the cookie in js. The Code is as follows:

// Set cookievar passKey = '4c05c54d952b11e691d76c0b843ea7f9 '; function setCookie (cname, cvalue, exdays) {var d = new Date (); d. setTime (d. getTime () + (exdays * 24x60*60*1000); var expires = "expires =" + d. toUTCString (); document. cookie = cname + "=" + encrypt (escape (cvalue), passKey) + ";" + expires;} // obtain cookiefunction getCookie (cname) {var name = cname + "="; var ca = document. cookie. split (';'); for (var I = 0; I
 
  

The setCookie (cname, cvalue, exdays) parameters are the names of the stored cookies, the cookie value, and the cookie validity period.

Because the cookie cannot contain special characters such as equal signs, spaces, and semicolons, I use the escape () function to encode the string when setting the cookie. When obtaining the cookie, I use the unescape () function to decode it. However, the escape () function does not encode ASCII letters and numbers. Therefore, the accounts and passwords stored in cookies are stored in plain text, which is not safe. Therefore, I found an encryption and decryption algorithm for strings on the Internet. This algorithm requires two parameters: a string to be encrypted and a custom encryption key passKey. Encrypt (value, passkey) is used to encrypt the cookie. decrypt (value, passKey) is used to decrypt the cookie. This algorithm is appended to the end of this article.

Call the cookie access method:

1. Define checkbox

Remember password

2. Make sure that the account and password are entered correctly before calling

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

3. Go to the logon page and check whether there is an account and password in the cookie.

$ (Function () {// get cookie var cusername = getCookie ('mermername'); var cpassword = getCookie ('mermerpass'); if (cusername! = "" & Cpassword! = "") {$ ("# Username"). val (cusername); $ ("# password"). val (cpassword );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.