重构登陆cmd命令

This commit is contained in:
王超 2020-04-03 01:56:40 +08:00
parent 839972cfef
commit 25e462d34d
13 changed files with 155 additions and 109 deletions

View File

@ -44,6 +44,8 @@ public interface ImConst
String TCP = "tcp";
String UNKNOWN = "unknown";
String COOKIE_NAME_FOR_SESSION = "jim-s";
/**
* 消息体最多为多少,只支持多少M数据

View File

@ -3,21 +3,25 @@
*/
package org.jim.common.packets;
import org.jim.common.packets.Message;
/**
* 版本: [1.0]
* 功能说明:
* 功能说明: 登陆命令请求包体
* 作者: WChao 创建时间: 2017年9月12日 下午3:13:22
*/
public class LoginReqBody extends Message {
private static final long serialVersionUID = -10113316720288444L;
private String loginname;
/**
* 用户Id
*/
private String userId;
/**
* 密码
*/
private String password;
/**
* 登陆token
*/
private String token;
public LoginReqBody(){}
@ -26,31 +30,39 @@ public class LoginReqBody extends Message {
this.token = token;
this.cmd = Command.COMMAND_LOGIN_REQ.getNumber();
}
public LoginReqBody(String loginname,String password){
this.loginname = loginname;
public LoginReqBody(String userId,String password){
this.userId = userId;
this.password = password;
this.cmd = Command.COMMAND_LOGIN_REQ.getNumber();
}
public LoginReqBody(String loginname,String password,String token){
this(loginname,password);
public LoginReqBody(String userId,String password,String token){
this(userId,password);
this.token = token;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}

View File

@ -3,29 +3,40 @@
*/
package org.jim.common.packets;
import org.jim.common.ImStatus;
import org.jim.common.Status;
import org.jim.common.packets.User;
import sun.security.pkcs11.P11Util;
/**
* 版本: [1.0]
* 功能说明:
* 作者: WChao 创建时间: 2017年9月12日 下午3:15:28
*/
public class LoginRespBody extends RespBody {
public class LoginRespBody extends RespBody {
private static final long serialVersionUID = 1L;
private String token;
private User user;
public LoginRespBody(Command command , Status status){
this(command,status,null);
public LoginRespBody(){
this.setCommand(Command.COMMAND_LOGIN_RESP);
}
public LoginRespBody(Command command , Status status , User user){
super(command, status);
this.user = user;
public LoginRespBody(Status status){
this(status,null);
}
public LoginRespBody(Status status , User user){
this(status, user, null);
}
public LoginRespBody(Status status , User user, String token){
super(Command.COMMAND_LOGIN_RESP, status);
this.user = user;
this.token = token;
}
public User getUser() {
return user;
}
@ -41,9 +52,18 @@ public class LoginRespBody extends RespBody {
public void setToken(String token) {
this.token = token;
}
@Override
public void clear() {
setToken(null);
setUser(null);
public static LoginRespBody success(){
return new LoginRespBody(ImStatus.C10007);
}
public static LoginRespBody failed(){
return new LoginRespBody(ImStatus.C10008);
}
public static LoginRespBody failed(String msg){
LoginRespBody loginRespBody = new LoginRespBody(ImStatus.C10008);
loginRespBody.setMsg(msg);
return loginRespBody;
}
}

View File

@ -32,22 +32,32 @@ public class RespBody implements Serializable{
*/
private Object data;
public RespBody(){}
public RespBody(Command command){
this.command = command;
}
public RespBody(Command command,Object data){
this(command);
this.data = data;
}
public RespBody(Command command , Status status){
this(command);
this.code = status.getCode();
this.msg = status.getMsg();
}
public RespBody(Status status){
this.code = status.getCode();
this.msg = status.getMsg();
this(status.getCode(), status.getMsg());
}
public RespBody(Integer code, String msg){
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
@ -74,13 +84,16 @@ public class RespBody implements Serializable{
this.command = command;
return this;
}
public Object getData() {
return data;
}
public RespBody setData(Object data) {
this.data = data;
return this;
}
@Override
public String toString() {
return JsonKit.toJSONEnumNoUsingName(this);
@ -89,6 +102,5 @@ public class RespBody implements Serializable{
public byte[] toByte(){
return JsonKit.toJSONBytesEnumNoUsingName(this);
}
public void clear(){};
}

View File

@ -19,7 +19,7 @@ public class User implements Serializable{
/**
* 用户id;
*/
private String id;
private String userId;
/**
* user nick
*/
@ -54,8 +54,9 @@ public class User implements Serializable{
private JSONObject extras;
public User(){}
public User(String id , String nick){
this.id = id;
public User(String userId , String nick){
this.userId = userId;
this.nick = nick;
}
public String getNick() {
@ -77,12 +78,6 @@ public class User implements Serializable{
public void setGroups(List<Group> groups) {
this.groups = groups;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStatus() {
return status;
}
@ -113,5 +108,12 @@ public class User implements Serializable{
public void setExtras(JSONObject extras) {
this.extras = extras;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}

View File

@ -42,7 +42,7 @@ public class ImDemoGroupListener implements ImGroupListener {
if(clientUser == null) {
return;
}
User notifyUser = new User(clientUser.getId(),clientUser.getNick());
User notifyUser = new User(clientUser.getUserId(),clientUser.getNick());
exitGroupNotifyRespBody.setUser(notifyUser);
RespBody respBody = new RespBody(Command.COMMAND_EXIT_GROUP_NOTIFY_RESP,exitGroupNotifyRespBody);

View File

@ -22,11 +22,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
/**
* @author WChao
@ -48,17 +44,17 @@ public class LoginServiceProcessor implements LoginCmdProcessor {
/**
* 根据用户名和密码获取用户
* @param loginName
* @param password
* @param loginReqBody
* @param imChannelContext
* @return
* @author: WChao
*/
public User getUser(String loginName, String password) {
String text = loginName+password;
public User getUser(LoginReqBody loginReqBody, ImChannelContext imChannelContext) {
String text = loginReqBody.getUserId()+loginReqBody.getPassword();
String key = ImConst.AUTH_KEY;
String token = Md5.sign(text, key, CHARSET);
User user = getUser(token);
user.setId(loginName);
user.setUserId(loginReqBody.getUserId());
return user;
}
/**
@ -72,7 +68,7 @@ public class LoginServiceProcessor implements LoginCmdProcessor {
User user = tokenMap.get(token);
if (user == null) {
user = new User();
user.setId(UUIDSessionIdGenerator.instance.sessionId(null));
user.setUserId(UUIDSessionIdGenerator.instance.sessionId(null));
user.setNick(familyName[RandomUtil.randomInt(0, familyName.length - 1)] + secondName[RandomUtil.randomInt(0, secondName.length - 1)]);
user.setGroups(initGroups(user));
@ -98,7 +94,7 @@ public class LoginServiceProcessor implements LoginCmdProcessor {
Group myFriend = new Group("1","我的好友");
List<User> myFriendGroupUsers = new ArrayList<User>();
User user1 = new User();
user1.setId(UUIDSessionIdGenerator.instance.sessionId(null));
user1.setUserId(UUIDSessionIdGenerator.instance.sessionId(null));
user1.setNick(familyName[RandomUtil.randomInt(0, familyName.length - 1)] + secondName[RandomUtil.randomInt(0, secondName.length - 1)]);
user1.setAvatar(nextImg());
myFriendGroupUsers.add(user1);
@ -121,31 +117,18 @@ public class LoginServiceProcessor implements LoginCmdProcessor {
* 当登陆失败时设置user属性需要为空相反登陆成功user属性是必须非空的;
*/
@Override
public LoginRespBody doLogin(LoginReqBody loginReqBody , ImChannelContext channelContext) {
String loginName = loginReqBody.getLoginname();
String password = loginReqBody.getPassword();
ImSessionContext imSessionContext = channelContext.getSessionContext();
String handshakeToken = imSessionContext.getToken();
User user;
LoginRespBody loginRespBody;
if (!StringUtils.isBlank(handshakeToken)) {
user = this.getUser(handshakeToken);
}else{
user = this.getUser(loginName, password);
public LoginRespBody doLogin(LoginReqBody loginReqBody, ImChannelContext imChannelContext) {
if(Objects.nonNull(loginReqBody.getUserId()) && Objects.nonNull(loginReqBody.getPassword())){
return LoginRespBody.success();
}else {
return LoginRespBody.failed();
}
if(user == null){
loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10008);
}else{
loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10007,user);
}
return loginRespBody;
}
@Override
public void onSuccess(ImChannelContext channelContext) {
public void onSuccess(User user, ImChannelContext channelContext) {
logger.info("登录成功回调方法");
ImSessionContext imSessionContext = channelContext.getSessionContext();
User user = imSessionContext.getClient().getUser();
if(user.getGroups() != null){
//发送加入群组通知
for(Group group : user.getGroups()){
@ -159,4 +142,9 @@ public class LoginServiceProcessor implements LoginCmdProcessor {
}
}
}
@Override
public void onFailed(ImChannelContext imChannelContext) {
}
}

View File

@ -45,7 +45,7 @@ public class JoinGroupReqHandler extends AbstractCmdHandler {
ImSessionContext imSessionContext = imChannelContext.getSessionContext();
User clientUser = imSessionContext.getClient().getUser();
User notifyUser = new User(clientUser.getId(),clientUser.getNick());
User notifyUser = new User(clientUser.getUserId(),clientUser.getNick());
Group joinGroup = JsonKit.toBean(packet.getBody(),Group.class);
String groupId = joinGroup.getGroup_id();

View File

@ -32,49 +32,45 @@ public class LoginReqHandler extends AbstractCmdHandler {
@Override
public ImPacket handler(ImPacket packet, ImChannelContext imChannelContext) throws ImException {
if(Objects.isNull(packet.getBody())){
Jim.bSend(imChannelContext, ProtocolManager.Converter.respPacket(LoginRespBody.failed("body must not null!"),imChannelContext));
Jim.remove(imChannelContext, "body is null!");
return null;
}
ImServerChannelContext imServerChannelContext = (ImServerChannelContext)imChannelContext;
if (packet.getBody() == null) {
Jim.remove(imChannelContext, "body is null");
return null;
}
LoginCmdProcessor loginProcessor = this.getSingleProcessor(LoginCmdProcessor.class);
if(Objects.isNull(loginProcessor)){
log.info("登录失败,没有登录命令业务处理器!");
Jim.remove(imChannelContext, "no login serviceHandler processor!");
return null;
}
ImSessionContext imSessionContext = imChannelContext.getSessionContext();
LoginReqBody loginReqBody = JsonKit.toBean(packet.getBody(),LoginReqBody.class);
LoginRespBody loginRespBody = loginProcessor.doLogin(loginReqBody, imChannelContext);
if (loginRespBody == null || loginRespBody.getUser() == null) {
log.info("登录失败, loginName:{}, password:{}", loginReqBody.getLoginname(), loginReqBody.getPassword());
if(loginRespBody == null){
loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP, ImStatus.C10008);
LoginReqBody loginReqBody = JsonKit.toBean(packet.getBody(), LoginReqBody.class);
LoginCmdProcessor loginProcessor = this.getSingleProcessor(LoginCmdProcessor.class);
LoginRespBody loginRespBody = null;
User user = null;
if(Objects.nonNull(loginProcessor)){
loginRespBody = loginProcessor.doLogin(loginReqBody, imChannelContext);
if (Objects.isNull(loginRespBody) || loginRespBody.getCode() != ImStatus.C10007.getCode()) {
log.warn("login failed, userId:{}, password:{}", loginReqBody.getUserId(), loginReqBody.getPassword());
Jim.bSend(imChannelContext, ProtocolManager.Converter.respPacket(loginRespBody, imChannelContext));
Jim.remove(imChannelContext, "userId or token is incorrect");
return null;
}
loginRespBody.clear();
ImPacket loginRespPacket = new ImPacket(Command.COMMAND_LOGIN_RESP, loginRespBody.toByte());
Jim.bSend(imChannelContext, loginRespPacket);
Jim.remove(imChannelContext, "loginName and token is incorrect");
return null;
user = loginProcessor.getUser(loginReqBody, imChannelContext);
}
if(Objects.isNull(user)){
user = new User(loginReqBody.getUserId(),loginReqBody.getUserId());
}
User user = loginRespBody.getUser();
IProtocol protocol = imServerChannelContext.getProtocolHandler().getProtocol();
String terminal = protocol == null ? "" : protocol.name();
user.setTerminal(terminal);
user.setTerminal(Objects.isNull(protocol) ? Protocol.UNKNOWN : protocol.name());
imSessionContext.getClient().setUser(user);
Jim.bindUser(imServerChannelContext, user.getId());
Jim.bindUser(imServerChannelContext, user.getUserId());
//初始化绑定或者解绑群组;
bindUnbindGroup(imChannelContext, user);
loginProcessor.onSuccess(imChannelContext);
loginRespBody.clear();
loginProcessor.onSuccess(user, imChannelContext);
return ProtocolManager.Converter.respPacket(loginRespBody, imChannelContext);
}
/**
* 初始化绑定或者解绑群组;
*/
public void bindUnbindGroup(ImChannelContext imChannelContext , User user)throws ImException{
String userId = user.getId();
String userId = user.getUserId();
List<Group> groups = user.getGroups();
if( groups != null){
boolean isStore = ImConfig.Const.ON.equals(getImConfig().getIsStore());

View File

@ -85,6 +85,7 @@ public class UserReqHandler extends AbstractCmdHandler {
return user;
}else{
//user = Jim.getUser(userId);
if(user == null) {
return null;
}

View File

@ -6,6 +6,7 @@ package org.jim.server.command.handler.processor.login;
import org.jim.common.ImChannelContext;
import org.jim.common.packets.LoginReqBody;
import org.jim.common.packets.LoginRespBody;
import org.jim.common.packets.User;
import org.jim.server.command.handler.processor.SingleProtocolCmdProcessor;
/**
*
@ -18,11 +19,23 @@ public interface LoginCmdProcessor extends SingleProtocolCmdProcessor {
* @param imChannelContext
* @return
*/
LoginRespBody doLogin(LoginReqBody loginReqBody , ImChannelContext imChannelContext);
LoginRespBody doLogin(LoginReqBody loginReqBody, ImChannelContext imChannelContext);
/**
* 获取用户信息接口方法
* @param loginReqBody
* @param imChannelContext
* @return
*/
User getUser(LoginReqBody loginReqBody, ImChannelContext imChannelContext);
/**
* 登录成功回调方法
* @param imChannelContext
*/
void onSuccess(ImChannelContext imChannelContext);
void onSuccess(User user, ImChannelContext imChannelContext);
/**
* 登陆失败回调方法
* @param imChannelContext
*/
void onFailed(ImChannelContext imChannelContext);
}

View File

@ -170,7 +170,7 @@ public class RedisImBindListener extends AbstractImBindListener{
if(!isStore() || user == null) {
return;
}
String userId = user.getId();
String userId = user.getUserId();
if(StringUtils.isEmpty(userId)) {
return;
}

View File

@ -400,7 +400,7 @@ public class RedisMessageHelper extends AbstractMessageHelper{
if(user == null) {
return;
}
String userId = user.getId();
String userId = user.getUserId();
boolean isOnline = this.isOnline(userId);
if(isOnline){
user.setStatus(ONLINE);