徐勤民 3 dní pred
rodič
commit
14be1c1c13

+ 10 - 0
src/main/java/cn/org/bjca/trust/java/imserver/common/CommonHelper.java

@@ -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);
+    }
+}

+ 52 - 0
src/main/java/cn/org/bjca/trust/java/imserver/common/JWTHelper.java

@@ -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());
+    }
+}

+ 2 - 2
src/main/java/cn/org/bjca/trust/java/imserver/controller/user/v1/UserV1Controller.java

@@ -33,7 +33,7 @@ public class UserV1Controller {
         if (null != appid && null != userid && null != ostype) {
 
             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);
             if (null == userInfo) {
@@ -56,7 +56,7 @@ public class UserV1Controller {
 
             return new HttpResult<>(200, "成功", new LoginData("114.115.203.60", "18883", userInfo));
         } else {
-            return new HttpResult<>(201, "参数错误", new LoginData());
+            return new HttpResult<>(201, "参数错误", null);
         }
 
     }

+ 8 - 3
src/main/java/cn/org/bjca/trust/java/imserver/im/msg/ConnectMessage.java

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

+ 11 - 13
src/main/java/cn/org/bjca/trust/java/imserver/request/RequestManager.java

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