处理登录业务,读取mysql账号信息和登录信息对比验证登录有效性,同时进行登录策略限制,限制30分钟内只能尝试10次。
DB_PROXY::doLogin
处理登陆业务,30分钟内允许错误登陆10次
CLoginStrategy
登录策略抽象基类,子类CInterLoginStrategy和CExterLoginStrategy继承于它,来制定不同的登录策略
CInterLoginStrategy::doLogin
查询mysql里strName用户信息,将strPass和数据库存储密码对比,相同则认为用户有效返回true,否则返回false,同时将该用户详情存入user
CExterLoginStrategy::doLogin
暂未实现,为空
mysql> select id,name,password from IMUser;
+----+------+----------------------------------+
| id | name | password |
+----+------+----------------------------------+
| 1 | ql1 | edfb3d9b7ea97dfc808a10d5562cd2f9 |
| 2 | ql2 | 4561916ae3e51f2493516566823dbf0a |
| 3 | ql3 | 7e5119589297f3d87f90e59d9f118056 |
| 17 | ql2 | 982fe69c1a43d84c9860800720084b7d |
| 18 | ql3 | 332b3ccc70e164d057e3e79ea0ce74f6 |
+----+------+----------------------------------+
5 rows in set (0.00 sec)
//
// test_login.cpp
// test_login
//
// Created by blueBling on 22-04-26.
// Copyright (c) 2022年blueBling. All rights reserved.
//
#include "Login.h"
#include "IM.Server.pb.h"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
//#include <unistd.h>
//
//unsigned int sleep(unsigned int seconds);
int test_login() {
IM::Server::IMValidateReq msg;
// message IMValidateReq{
// //cmd id: 0x0703
// required string user_name = 1;
// required string password = 2;
// optional bytes attach_data = 20;
// }
string name("ql1");
string passwd("e10adc3949ba59abbe56e057f20f883e");
string passwd_err("e10adc3949ba59abbe56e057f20f883d");
// 以错误密码间隔1分钟登陆1次
uint32_t tmNow = time(NULL);
int cnt = 0;
while(cnt < 15) {
msg.set_user_name(name.c_str(), name.size());
msg.set_password(passwd_err.c_str(), passwd_err.size());
msg.set_attach_data("");
CImPdu pdu;
pdu.SetPBMsg(&msg);
pdu.SetSeqNum(1);
pdu.SetServiceId(IM::BaseDefine::SID_OTHER);
pdu.SetCommandId(IM::BaseDefine::CID_OTHER_VALIDATE_REQ);
DB_PROXY::doLogin(&pdu, 6);
cnt++;
sleep(2);
}
return 0;
}
int main(){
test_login();
//这里mysql和redis连接池未释放存在内存泄漏问题,解决方法参考test_dbpool
return 0;
}