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 );}}