Solve the user forgot password, by sending email back
@Action ("send") public String SendMail () { accountservices.sendforgetmail (email); return SUCCESS;}
//through the database query input box email account, with this account and then really send,//the properties of the online thread are written as constants Public voidSendforgetmail (FinalString Email) { Account Account=accountdao.findbyemail (email); if(Account! =NULL) { /*in the message sent, the mailbox of the account to which the password was retrieved, and token token are unique, prevent the URL from being modified to change the password*/ FinalString token =Uuid.randomuuid (). toString (); /*put in cache and expire after 5 minutes*/Cache.put ("Forget:" +email, 300, token); /*E -Mail is a process, can not wait to send out in the jump, will send into a thread*/Thread Thread=NewThread (NewRunnable () { Public voidrun () {Htmlemail mail=NewHtmlemail (); //setting the host server addressMail.sethostname ("smtp.126.com"); Mail.setauthentication ("Kaishengit", "[email protected] @rd"); //whether validation is requiredMail.settls (true); Mail.setcharset ("UTF-8"); Try { //SenderMail.setfrom ("[Email protected]"); //ThemeMail.setsubject ("Password Retrieve mail"); //contentMail.sethtmlmsg ("Click this link to recover password: <a href=\" http://localhost/forget/validate.action?email= "+email+" &token= "+ token+ "\" > Reset Password </a> "); Mail.addto (email); Mail.send (); } Catch(emailexception e) {e.printstacktrace (); } } }); Thread.Start (); } }
//Cache written as tool class Public classCache {Private StaticMemcachedclient client =buildeclient (); Private Staticmemcachedclient buildeclient () {Try { return NewMemcachedclient (addrutil.getaddresses ("127.0.0.1:11211")); } Catch(IOException e) {Throw NewRuntimeException ("Memcachedclient get Error", E); } } Public Static voidPut (String key,intTime,object value) {Client.add (key, time, value); } Public StaticObject get (String key) {returnClient.get (key); } Public Static voidRemove (String key) {client.delete (key); } }
Click Retrieve password in the mailbox
@Action ("Validate")public = Accountservices.validatemailandtoken (email,token); if NULL ) { return INPUT; } return SUCCESS;}
//Service in PublicAccount Validatemailandtoken (string email, string token) {string Cachetoken= (String) cache.get ("Forget:" +email); if(Cachetoken = =NULL) {System.out.println ("Can't Get Token"); return NULL; } Else { if(Token.equals (Cachetoken)) {Cache.remove ("Forget:" +email); returnaccountdao.findbyemail (email); } Else{System.out.println ("Two tokens Inconsistent"); return NULL; } } }
Forget your password and retrieve it via email