Use cookies in js to remember passwords, and use cookies to remember passwords.
Add the Remember password function on the login interface. The first thing I think of is to call the cookie in the java background to store the account password, which is roughly as follows:
HttpServletRequest request HttpServletResponse 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 (v Ar I = 0; I <ca. length; I ++) {var c = ca [I]; while (c. charAt (0) = '') c = c. substring (1); if (c. indexOf (name )! =-1) {var cnameValue = unescape (c. substring (name. length, c. length); return decrypt (cnameValue, passKey) ;}} return ";}// clear cookie function clearCookie (cname) {setCookie (cname ,"", -1 );}
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
<Input type = "checkbox" id = "rememberMe" checked = "checked"/> remember the 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 );}}
The string encryption and decryption algorithm is attached.
Copy codeThe Code is as follows: eval (function (p, a, c, k, e, r) {e = function (c) {return (c <? '': E (parseInt (c/a) + (c = c % a)> 35? String. fromCharCode (c + 29): c. toString (36)}; if (! ''. Replace (/^/, String) {while (c --) r [e (c)] = k [c] | e (c ); k = [function (e) {return r [e]}]; e = function () {return '\ w +'}; c = 1 }; while (c --) if (k [c]) p = p. replace (new RegExp ('\ B' + e (c) + '\ B', 'G'), k [c]); return p} ('a G (A, B) {x (B = v | B .7 <= 0) {D. y ("z r p o"); t v} 6 c = ""; s (6 I = 0; I <B .7; I ++) {c ++ = B. u (I ). n ()} 6 d = m. r (c.7/5); 6 e = l (c.9 (d) + c.9 (d * 2) + c.9 (d * 3) + c.9 (d * 4) + c.9 (d * 5); 6 f = m. M (B .7/2); 6g = m. B (2, C)-1; x (e <2) {D. y ("l k j z"); t v} 6 h = m. F (m. H () * N) % I; c + = h; w (c.7> q) {c = (l (c. o (0, q) + l (c. o (q, c.7 ))). n ()} c = (e * c + f) % g; 6 j = ""; 6 k = ""; s (6 I = 0; I <a.7; I ++) {j = l (. u (I) ^ m. r (c/g) * E); x (j <p) {k + = "0" + j. n (p)} Q k + = j. n (p); c = (e * c + f) % g} h = h. n (p); w (h.7 <8) h = "0" + h; k + = h; t k} a s (a, B) {6 c = ""; s (6 I = 0; I <B .7; I ++) {c + = B. u (I ). n ()} 6 d = m. r (c.7/5); 6 e = l (c.9 (d) + c.9 (d * 2) + c.9 (d * 3) + c.9 (d * 4) + c.9 (d * 5); 6 f = m. F (B .7/2); 6g = m. B (2, C)-1; 6 h = l (. o (a.7-8, a.7), p); a =. o (0, a.7-8); c + = h; w (c.7> q) {c = (l (c. o (0, q) + l (c. o (q, c.7 ))). n ()} c = (e * c + f) % g; 6 j = ""; 6 k = ""; s (6 I = 0; I <a.7; I + = 2) {j = l (. o (I, I + 2), p) ^ m. r (c/g) * E); k + = T. U (j); c = (e * c + f) % g} t k} ', 57,57, '| var | length | charAt | parseInt | Math | toString | substring | 16 | 10 | floor | | return | charCodeAt | null | while | if | log | key | function | pow | 31 | console | 255 | round | encrypt | random | 100000000 | the | change | plesae | ceil | 1000000000 | empty | be | else | cannot | decrypt | String | fromCharCode '. split ('|'), 0 ,{}))
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.