聊天伺服器-解密陌生人(8)找回密碼、修改個人資訊,解密陌生人

來源:互聯網
上載者:User

聊天伺服器-解密陌生人(8)找回密碼、修改個人資訊,解密陌生人

提示: 因為工程稍微有點大對我個人來說,所以可能在某些方面講的不清楚或邏輯性不夠強,如果有問題請及時@我。
原工程:https://github.com/LineChen/

二、找回密碼

注意點:
1.找回密碼不是簡單的從資料庫把密碼取出來然後發送給使用者。首先,資料庫儲存的密碼是經過MD5轉換的,無法得到密碼明文,再說就算能得到,也不能發送密碼明文給客戶,不然這又違背了安全性原則。
2.這裡是這樣處理的:系統直接產生一個密碼,然後修改資料庫,然後把這個密碼通過郵件方式發送給使用者

發送郵件的類:
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 {        properties=new Properties();        properties.setProperty("mail.transport.protocol", "smtp");//發送郵件協議        properties.setProperty("mail.smtp.auth", "true");//需要驗證        // properties.setProperty("mail.debug", "true");//設定debug模式後台輸出郵件發送的過程        session = Session.getInstance(properties);        session.setDebug(false);//debug模式->控制台會顯示發送郵件的具體細節        //郵件資訊        messgae = new MimeMessage(session);        messgae.setFrom(new InternetAddress("15764230067@163.com"));//設定發送人        messgae.setText(content);//設定郵件內容        messgae.setSubject(subject);//設定郵件主題        //發送郵件        tran = session.getTransport();          tran.connect("smtp.163.com", 25, "15764230067@163.com", "BigBad670067");//串連到新浪郵箱伺服器        // tran.connect("smtp.qq.com", 25, "Michael8@qq.vip.com", "xxxx");//串連到QQ郵箱伺服器        tran.sendMessage(messgae, new Address[]{ new InternetAddress(clinetmailAddress)});//設定郵件接收人        tran.close();    } catch (Exception e) {        // TODO: handle exception        e.printStackTrace();    }   }

}

處理代碼:
/**
* 處理找回密碼
*
* @param session
* @param moMoMsg
*/
public void handleFindPasswd(IoSession session, iMoMoMsg moMoMsg) {
JSONObject json = JSON.parseObject(moMoMsg.msgJson);
String userEmail = json.getString(MsgKeys.userEmail);// 郵箱地址
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)) {
// 發送郵件…
new SendEmailToClient(userEmail, “找回密碼”, “尊敬的” + userName
+ “:\n 您好,系統為您隨機產生的密碼是:” + newPwd + “,登入後請儘快修改密碼!”);
NotifyJson.put(MsgKeys.msgType,
iMoMoMsgTypes.FIND_PASSWD_SUCCESS);
} else {
NotifyJson.put(MsgKeys.msgType,
iMoMoMsgTypes.FIND_PASSWD_FAILED);
}
} else {
System.out.println(“沒有該使用者”);
NotifyJson.put(MsgKeys.msgType, iMoMoMsgTypes.FIND_PASSWD_FAILED);
}
Notify.msgJson = NotifyJson.toJSONString();
session.write(Notify);
}

三、重設密碼、修改使用者名稱、修改個性簽名等個人資訊
處理方法都是一樣的,根據使用者Id直接修改資料庫
/**
* 處理修改個人資訊
*
* @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(“修改用資訊成功”);
} else {
System.out.println(“修改使用者信失敗”);
}
// 不發送通知訊息
}

四、修改個人頭像
修改個人頭像不僅僅是修改一下帳戶圖片檔案就完事了,還要及時通知自己的好友“我換頭像啦”。
原因是:這裡用戶端每次請求好友名單時會判斷手機本地是否有該好友的頭像,如果有就不用向伺服器請求(這樣也是為了減少不必要的流量)。所以如果這裡不去主動提醒好友“我換頭像了”,假如他的某個好友手機本地已經存在頭像了,那在這個好友名單中顯示的頭像一直以前的。
處理代碼:
/**
* 處理修改頭像
*
* @param session
* @param moMoMsg
*/
public void handleResetHead(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);
// 修改成功
System.out.println(“修改頭像成功”);
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);// 通知好友是誰改頭像的        resetHeadJson.put(MsgKeys.userId, userId);// 我發的        resetHead.msgJson = resetHeadJson.toJSONString();        resetHead.msgBytes = moMoMsg.msgBytes;        for (String friendId : friendList) {            // 判斷是否線上,線上直接轉寄,不線上緩衝到資料庫中            if (ManageClientSession.isContainsId(friendId)) {                ManageClientSession.getSession(friendId).write(resetHead);

// System.out.println(“轉寄成功..”);
} else {
if (!model.isTableExists(“mc_” + friendId))// “mc_” + userId
model.createCacheTable(friendId);// 建立快取資料庫
MsgDb msgDb = MsgTranceUtil.getInstance().Trance_Net2Db(
resetHead);
if (model.insertCacheMsg(msgDb, friendId)) {
// System.out.println(“緩衝成功”);
} else {
// System.out.println(“緩衝失敗”);
}
}
}

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

// System.out.println(“修改頭像失敗”);
}
Notify.msgJson = NotifyJson.toJSONString();
session.write(Notify);
}

這裡要考慮是否發送離線訊息,也就是如果該使用者的好友線上,就直接發送訊息,否則發送離線訊息(就是先把該訊息儲存在資料庫中,等那個好友下次上線的時候先去資料庫查詢一下看是否有離線訊息,有的話就發送即可)這個下次再具體討論。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.