Cookies are small text files that Web designers place on the client (browser), cookies can not only implement the Save password function, but also save the latest browsing record through cookies to increase the user experience.
In the login interface to add remember the password function, my first thought is to invoke cookies in the Java background to store account passwords, 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 the sake of security, we get in the background of the password is mostly in JS through MD5 encrypted ciphertext, if the ciphertext into the cookie, in JS to get to also have no effect;
Then consider accessing cookies in JS with the following code:
Set 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;
}
Gets
the 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) = = ') c = c.substring (1);
if (c.indexof (name)!=-1) {
var cnamevalue = unescape (c.substring (Name.length, c.length));
Return decrypt (Cnamevalue, passkey);
}
Return "";
}
Clears the cookie
function ClearCookie (CNAME) {
Setcookie (CNAME, "",-1);
}
Setcookie (CNAME, cvalue, exdays) Three parameters are stored cookie name, cookie value, cookie valid number of days
Since cookies cannot contain special characters such as equals, spaces, semicolons, and so on, I use the escape () function to encode a cookie when I set it, and I use the unescape () function to decode it when I get the cookie. But the escape () function does not encode ASCII letters and numbers, so the account, password, stored in the cookie, is stored in plaintext and is unsafe. So the internet to find a string encryption decryption algorithm, the algorithm needs to pass two parameters, a need to encrypt the string, a custom encryption key passkey. When you set up cookies using Encrypt (value, passkey) encryption, you use decrypt (value, passkey) to decrypt the cookie when you read it, which is attached at the end of this article.
Call to access Cookie method:
1. Define CheckBox
<input type= "checkbox" id= "RememberMe" checked= "checked"/> Remember password
2, to determine the number of password input correct after the call
if ($ (' #rememberMe '). Is (': Checked ')} {
Setcookie (' CustomerName ', $ (' #username '). Val (). Trim (), 7)
Setcookie (' Customerpass ', $ (' #password '). Val (). Trim (), 7)
}
3, enter the login interface, to determine whether the cookie has an account password, if there will automatically fill
$ (function () {
//Get cookie
var cusername = GetCookie (' CustomerName ');
var Cpassword = GetCookie (' Customerpass ');
if (cusername!= "" && cpassword!= "") {
$ ("#username"). Val (cusername);
$ ("#password"). Val (Cpassword);
}
Finally attach string encryption and decryption algorithm
Eval (function (p,a,c,k,e,r) {e=function (c) {return (c<a? ': E (parseint (C/A))) + ((c=c%a) >35? String.fromCharCode (c+29): C.tostring ())};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); 6 g=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 (a.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); 6 g=m.b (2,c) -1;6 h=l ( A.O (a.7-8,a.7), p); A=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 (L (A.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|for|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,{})
PS: Below look at the next JS operation cookies related functions
Set the cookie function, three parameters, one is the name of the cookie, one is the value, the other is to set the cookie save time, Unit: Day
function Setcookie (name,value,days) {
var days= arguments[2]?arguments[2]:30; This cookie will be saved for 30 days
var exp=new date ();//new date ("December, 9998")
; Exp.settime (Exp.gettime () + days*86400000);
Document.cookie=name + "=" + Escape (value) + "expires=" + exp.togmtstring ();
}
Take cookies function
GetCookie (name) {
var arr=document.cookie.match (new RegExp (^|) +name+ "= ([^;] *)(;|$)"));
if (arr!=null) {return
unescape (arr[2]);
}
return null;
}
Delete Cookie function
Delcookie (name) {
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval=getcookie (name);
if (cval!=null) {
document.cookie= name + "= +cval+"; expires= "+exp.togmtstring ();
}
}
The above is a small set of JavaScript to introduce how to use cookies to implement remember the password function and cookies related functions, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!