Chat Server-Decrypt strangers (8) Recover password, modify personal information

Source: Internet
Author: User

Tip: Because the project is a little bit large for me personally, so may be in some aspects of unclear or logic is not strong enough, if there is a problem please timely @ me.
Original project: https://github.com/LineChen/

Second, retrieve the password

Note the point:
1. Retrieving the password is not simply to remove the password from the database and send it to the user. First of all, the password stored in the database is MD5 converted, can not get the password plaintext, and even if you can get, also can not send password plaintext to the customer, otherwise this violates the security principle.
2. This is done: the system generates a password directly, then modifies the database, and then sends the password to the user by mail

The class that sent the message:
Package com.server_utils;
Import java.util.*;

Import javax.mail.*;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;

public class Sendemailtoclient {

Properties Properties=null; Session Session=null; Message Messgae=null; Transport tran=null;public sendemailtoclient (String clinetmailaddress,string subject,string content) {try {prop        Erties=new Properties (); Properties.setproperty ("Mail.transport.protocol", "SMTP");//Send Mail protocol properties.setproperty ("Mail.smtp.auth", "true ");//need to verify//Properties.setproperty (" Mail.debug "," true ");//Set debug mode background output mail sending process session = Session.getinstan        CE (properties);        Session.setdebug (false);//debug Mode--the console displays details of the Sent message//message information Messgae = new MimeMessage (session); Messgae.setfrom (New InternetAddress ("[email protected]");//Set Sender Messgae.settext (content);//Set message content mes          Sgae.setsubject (subject);//Set Message subject//Send mail tran = Session.gettransport (); Tran.connect ("smtp.163.com", +, "[email protected]", "BigBad670067");//Connect to Sina Mailbox Server//Tran.connect ("smtp.qq.co M ", +," [email protected] "," xxxx ");//Connect to QQ Mailbox server       Tran.sendmessage (Messgae, New address[]{new InternetAddress (clinetmailaddress)});//Set Mail receiver Tran.close ();    } catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }   }

}

Handling code:
/**
* Process Retrieve password
*
* @param session
* @param momomsg
*/
public void handlefindpasswd (iosession session, Imomomsg momomsg) {
Jsonobject json = Json.parseobject (Momomsg.msgjson);
String useremail = json.getstring (msgkeys.useremail);//e-mail address
Sqlmodel model = new Sqlmodel ();
String userName = Model.getusername (UserEmail, true);
Imomomsg Notify = new Imomomsg ();
Notify.symbol = ' + ';
Jsonobject Notifyjson = new Jsonobject ();
String newpwd = Passwordutil.getinstance (). CREATENEWPWD ();
if (!username.equals ("null")) {
String sql = "Update imomo_clients set userpasswd =?" where useremail =? ";
String[] Paras = {passwordutil.getinstance (). ToMD5 (NEWPWD),
UserEmail};
if (Model.updatedb (SQL, paras)) {
Send mail ...
New Sendemailtoclient (useremail, "Retrieve password", "respectable" + userName
+ ": \ n Hello, the system for you randomly generated password is:" + newpwd + ", login please change password as soon as possible!");
Notifyjson.put (Msgkeys.msgtype,
imomomsgtypes.find_passwd_success);
} else {
Notifyjson.put (Msgkeys.msgtype,
imomomsgtypes.find_passwd_failed);
}
} else {
System.out.println ("no user");
Notifyjson.put (Msgkeys.msgtype, imomomsgtypes.find_passwd_failed);
}
Notify.msgjson = Notifyjson.tojsonstring ();
Session.write (Notify);
}

third, reset the password, modify the user name, modify personal information such as personality signature
The processing method is the same, modify the database directly based on the user ID
/**
* Processing and modification of personal information
*
* @param momomsg
*/
public void Handleresetuserinfo (Imomomsg momomsg) {
Jsonobject json = Json.parseobject (Momomsg.msgjson);
String userId = json.getstring (Msgkeys.userid);
int type = Json.getintvalue (Msgkeys.msgtype);
Sqlmodel model = new Sqlmodel ();
String sql = "";
String[] paras = new string[2];
Switch (type) {
Case Imomomsgtypes.reset_username:
sql = "Update imomo_clients set userName =?" where userId =? ";
Paras[0] = json.getstring (msgkeys.username);
Break
Case Imomomsgtypes.reset_sex:
sql = "Update imomo_clients set usersex =?" where userId =? ";
Paras[0] = json.getstring (msgkeys.usersex);
Break
Case Imomomsgtypes.reset_birthday:
sql = "Update imomo_clients set userbirthday =?" where userId =? ";
Paras[0] = json.getstring (msgkeys.userbirthday);
Break
Case Imomomsgtypes.reset_signatue:
sql = "Update imomo_clients set personsignature =?" where userId =? ";
Paras[0] = json.getstring (msgkeys.personsignature);
Break
}
PARAS[1] = userId;
if (Model.updatedb (SQL, paras)) {
SYSTEM.OUT.PRINTLN ("Modification with information success");
} else {
System.out.println ("Failed to modify user's letter");
}
Do not send notification messages
}

Four, modify the profile picture
To modify the profile picture is not just to modify the user profile picture file is done, but also to timely notify their friends "I changed my avatar."
The reason for this is that each time a client requests a friend list, it will determine if there is a friend's avatar on the phone locally, and if there is no need to request it (this is also to reduce unnecessary traffic). So if you do not take the initiative to remind Friends "I change my Avatar", if one of his friends mobile phone local already exist, that in this friend list is displayed in the previous picture.
Handling code:
/**
* Processing modified avatar
*
* @param session
* @param momomsg
*/
public void Handleresethea D (iosession session, Imomomsg momomsg) {
Jsonobject json = Json.parseobject (Momomsg.msgjson);
String userId = json.getstring (Msgkeys.userid);
String Userheadpath = staticvalues.head_p_path + userId + ". png";
Imomomsg Notify = new Imomomsg ();
Notify.symbol = ' + ';
Jsonobject Notifyjson = new Jsonobject ();
try {
Filetools.getinstance (). Savemultyfile (Userheadpath,
Momomsg.msgbytes);
//Modified successfully
System.out.println ("Change avatar Success");
Notifyjson.put (Msgkeys.msgtype, imomomsgtypes.reset_head_success);

  Sqlmodel model = new Sqlmodel ();        string[] friendlist = Model.getfriendids (userId);        Imomomsg resethead = new Imomomsg ();        Resethead.symbol = '-';        Jsonobject Resetheadjson = new Jsonobject ();        Resetheadjson.put (Msgkeys.msgtype, Imomomsgtypes.reset_head); Resetheadjson.put (Msgkeys.friendid, UserID);//notify the friend who changed the Avatar's Resetheadjson.put (Msgkeys.userid, UserID);//I sent the R        Esethead.msgjson = Resetheadjson.tojsonstring ();        Resethead.msgbytes = momomsg.msgbytes; for (String friendid:friendlist) {//To determine whether online, direct forwarding online, not online cache to the database if (manageclientsession.iscontainsi D (friendID)) {manageclientsession.getsession (friendID). write (Resethead);  

SYSTEM.OUT.PRINTLN ("forwarded successfully:");
} else {
if (!model.istableexists ("MC_" + friendID)//"MC_" + userId
Model.createcachetable (friendid);//Create Cache database
Msgdb msgdb = Msgtranceutil.getinstance (). TRANCE_NET2DB (
Resethead);
if (Model.insertcachemsg (msgdb, friendID)) {
System.out.println ("cache success");
} else {
System.out.println ("Cache failed");
}
}
}

    } catch (Exception e) {        // 修改失败        NotifyJson.put(MsgKeys.msgType, iMoMoMsgTypes.RESET_HEAD_FAILED);

System.out.println ("Change Avatar Failed");
}
Notify.msgjson = Notifyjson.tojsonstring ();
Session.write (Notify);
}

Here to consider whether to send offline messages, that is, if the user's friend online, send a message directly, otherwise send offline message (that is, the message is saved in the database, and so on the next time the friend on the line to check the database to see if there are offline messages, some words can be sent) this next time to discuss the specific.

Chat Server-Decrypt strangers (8) Recover password, modify personal information

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.