这个提交包含在:
徐勤民 2025-01-09 14:47:48 +08:00
父节点 6f6a1478cb
当前提交 14be1c1c13
共有 5 个文件被更改,包括 83 次插入18 次删除

查看文件

@ -0,0 +1,10 @@
package cn.org.bjca.trust.java.imserver.common;
import java.util.Random;
public class CommonHelper {
public static String getRandomId() {
return TimeHelper.getTimeString("yyyyMMddHHmm") + (new Random().nextInt(899999999) + 100000000);
}
}

查看文件

@ -0,0 +1,52 @@
package cn.org.bjca.trust.java.imserver.common;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.xuqm.server.appmanager.entitys.UserInfo;
import com.xuqm.server.appmanager.entitys.sys.v1.TenantUserEntity;
import java.util.Calendar;
public class JWTHelper {
//定义自己的密钥
private static final String TOKEN = "token!DASD(#$dsad%$#.";
/**
* 生成token
*
* @param user 传入的有效负荷
* @return
*/
public static String genToken(TenantUserEntity user) {
JWTCreator.Builder builder = JWT.create();
builder.withClaim("userId", user.getUserId());
builder.withClaim("tenantNo", user.getTenantNo());
Calendar instance = Calendar.getInstance();
//定义过期时间
instance.add(Calendar.DATE, 7);
builder.withExpiresAt(instance.getTime());
return builder.sign(Algorithm.HMAC256(TOKEN));
}
/**
* 验证获取token中的有效负载验证失败返回null
*
* @param token
* @return
*/
public static DecodedJWT verify(String token) {
return JWT.require(Algorithm.HMAC256(TOKEN)).build().verify(token);
}
public static UserInfo getUser(String token) {
DecodedJWT jwt = null;
try {
jwt = JWTHelper.verify(token);
} catch (Exception ignored) {
return null;
}
return new UserInfo(jwt.getClaim("userId").asString(), jwt.getClaim("tenantNo").asString());
}
}

查看文件

@ -33,7 +33,7 @@ public class UserV1Controller {
if (null != appid && null != userid && null != ostype) { if (null != appid && null != userid && null != ostype) {
ApplicationEntity app = applicationRepository.findFirstByAppId(appid); ApplicationEntity app = applicationRepository.findFirstByAppId(appid);
if (null == app) return new HttpResult<>(201, "appId不存在", new LoginData()); if (null == app) return new HttpResult<>(201, "appId不存在", null);
UserInfo userInfo = userRepository.findFirstByUserIdAndOsTypeAndAppId(userid, ostype, appid); UserInfo userInfo = userRepository.findFirstByUserIdAndOsTypeAndAppId(userid, ostype, appid);
if (null == userInfo) { if (null == userInfo) {
@ -56,7 +56,7 @@ public class UserV1Controller {
return new HttpResult<>(200, "成功", new LoginData("114.115.203.60", "18883", userInfo)); return new HttpResult<>(200, "成功", new LoginData("114.115.203.60", "18883", userInfo));
} else { } else {
return new HttpResult<>(201, "参数错误", new LoginData()); return new HttpResult<>(201, "参数错误", null);
} }
} }

查看文件

@ -35,6 +35,13 @@ public class ConnectMessage extends PacketMessage {
connectMessage.setAppId(this.getAppId()); connectMessage.setAppId(this.getAppId());
connectMessage.setOsType(this.getOsType()); connectMessage.setOsType(this.getOsType());
connectMessage.setToken(this.getToken()); connectMessage.setToken(this.getToken());
DeviceEntity deviceEntity = getDeviceEntity();
connectMessage.setDevice(deviceEntity);
return connectMessage;
}
private DeviceEntity getDeviceEntity() {
DeviceEntity deviceEntity = new DeviceEntity(); DeviceEntity deviceEntity = new DeviceEntity();
deviceEntity.setDeviceId(this.getDevice().getDeviceId()); deviceEntity.setDeviceId(this.getDevice().getDeviceId());
@ -47,8 +54,6 @@ public class ConnectMessage extends PacketMessage {
deviceEntity.setSupportedAbis(this.getDevice().getSupportedAbis()); deviceEntity.setSupportedAbis(this.getDevice().getSupportedAbis());
deviceEntity.setSupported32BitAbis(this.getDevice().getSupported32BitAbis()); deviceEntity.setSupported32BitAbis(this.getDevice().getSupported32BitAbis());
deviceEntity.setSupported64BitAbis(this.getDevice().getSupported64BitAbis()); deviceEntity.setSupported64BitAbis(this.getDevice().getSupported64BitAbis());
return deviceEntity;
connectMessage.setDevice(deviceEntity);
return connectMessage;
} }
} }

查看文件

@ -76,21 +76,19 @@ public class RequestManager {
if (!applicationRepository.existsByAppId(appId)) return new HttpResult<>(201, "appId不存在", null); if (!applicationRepository.existsByAppId(appId)) return new HttpResult<>(201, "appId不存在", null);
if (!userRepository.existsByAppIdAndUserId(appId, userId)) if (!userRepository.existsByAppIdAndUserId(appId, userId))
return new HttpResult<>(201, "当前用户不存在", null); return new HttpResult<>(201, "当前用户不存在", null);
switch (path) { return switch (path) {
case Group_create_v1: case Group_create_v1 -> (HttpResult<T>) this.groupCreate(data instanceof String ?
return (HttpResult<T>) this.groupCreate(data instanceof String ? GsonImplHelp.get().toObject((String) data, GroupCreateData.class)
GsonImplHelp.get().toObject((String) data, GroupCreateData.class) : (GroupCreateData) data, appId, userId);
: (GroupCreateData) data, appId, userId); case MsgHistoryForCount -> {
case MsgHistoryForCount: if (null == data) yield new HttpResult<>(201, "参数为空", null);
if(null == data) return new HttpResult<>(201, "参数为空", null); yield (HttpResult<T>) this.getHistoryForCount(data instanceof String ?
return (HttpResult<T>) this.getHistoryForCount(data instanceof String ?
GsonImplHelp.get().toObject((String) data, MsgHistoryForCountData.class) GsonImplHelp.get().toObject((String) data, MsgHistoryForCountData.class)
: (MsgHistoryForCountData) data, appId, userId); : (MsgHistoryForCountData) data, appId, userId);
case ConversationList: }
return (HttpResult<T>) this.getConversation(appId, userId); case ConversationList -> (HttpResult<T>) this.getConversation(appId, userId);
default: default -> new HttpResult<>(201, "路径错误" + path, null);
return new HttpResult<>(201, "路径错误" + path, null); };
}
} else return new HttpResult<>(201, "参数错误", null); } else return new HttpResult<>(201, "参数错误", null);