wangliang 4 жил өмнө
parent
commit
553ee38203

+ 3 - 3
themis-backend/src/main/java/com/qmth/themis/backend/api/TBUserController.java

@@ -215,10 +215,10 @@ public class TBUserController {
         mqDtoService.assembleSendOneWayMsg(mqDtoLog);
         //mq发送消息end
         //测试
-        String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
+//        String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
         Map<String, Object> map = new HashMap<>();
-//        map.put(SystemConstant.ACCESS_TOKEN, token);
-        map.put(SystemConstant.ACCESS_TOKEN, test);
+        map.put(SystemConstant.ACCESS_TOKEN, token);
+//        map.put(SystemConstant.ACCESS_TOKEN, test);
         map.put(SystemConstant.ACCOUNT, user);
         map.put(SystemConstant.SESSION_ID, sessionId);
         map.put("roleCodes", authDto.getRoleCodes());

+ 54 - 54
themis-backend/src/main/java/com/qmth/themis/backend/interceptor/AuthInterceptor.java

@@ -62,71 +62,71 @@ public class AuthInterceptor implements HandlerInterceptor {
         String time = ServletUtil.getRequestTime();
         log.info("platform:{},deviceId:{},authorization:{},method:{},time:{}", platform, deviceId, authorization, method, time);
         Long userId = null;
-//        Long timestamp = Long.parseLong(ServletUtil.getRequestTime(request));
-//        if (!SystemConstant.expire(timestamp.longValue())) {
-//            final SignatureInfo info = SignatureInfo
-//                    .parse(method, url, timestamp, ServletUtil.getRequestAuthorization(request));
-        //测试
-        final SignatureInfo info = SignatureInfo
-                .parse(authorization);
-        if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
-            String sessionId = info.getInvoker();
-            TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
-            if (Objects.isNull(tbSession)) {
-                throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
-            } else {
-                if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
-                        && platform.name().equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
-                    userId = Long.parseLong(tbSession.getIdentity());
-                    Long expireTime = tbSession.getExpireTime();
-                    //手机端的token时长为一个月,所以会出现缓存没有的情况
-                    if (expireTime <= System.currentTimeMillis()) {//先判断时间是否过期
-                        throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
-                    }
-                    TBUser tbUser = (TBUser) redisUtil.getUser(userId);
-                    if (Objects.isNull(tbUser)) {
-                        tbUser = tbUserService.getById(userId);
-                        redisUtil.setUser(tbUser.getId(), tbUser);
-                    }
+        Long timestamp = Long.parseLong(time);
+        if (!SystemConstant.expire(timestamp.longValue())) {
+            final SignatureInfo info = SignatureInfo
+                    .parse(method.toLowerCase(), url, timestamp, authorization);
+            //测试
+//        final SignatureInfo info = SignatureInfo
+//                .parse(authorization);
+            if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
+                String sessionId = info.getInvoker();
+                TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
+                if (Objects.isNull(tbSession)) {
+                    throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
+                } else {
+                    if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
+                            && platform.name().equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
+                        userId = Long.parseLong(tbSession.getIdentity());
+                        Long expireTime = tbSession.getExpireTime();
+                        //手机端的token时长为一个月,所以会出现缓存没有的情况
+                        if (expireTime <= System.currentTimeMillis()) {//先判断时间是否过期
+                            throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
+                        }
+                        TBUser tbUser = (TBUser) redisUtil.getUser(userId);
+                        if (Objects.isNull(tbUser)) {
+                            tbUser = tbUserService.getById(userId);
+                            redisUtil.setUser(tbUser.getId(), tbUser);
+                        }
 
-                    request.setAttribute(SystemConstant.SESSION, tbSession);
-                    request.setAttribute(SystemConstant.ACCOUNT, tbUser);
+                        request.setAttribute(SystemConstant.SESSION, tbSession);
+                        request.setAttribute(SystemConstant.ACCOUNT, tbUser);
 
-                    AuthDto authDto = (AuthDto) redisUtil.get(SystemConstant.userOauth + "::" + userId);
-                    //验证权限
-                    if (Objects.isNull(authDto)) {
-                        authDto = cacheService.addAccountCache(userId);
-                    }
-                    request.setAttribute(SystemConstant.ORG, authDto.getTbOrg());
-                    //系统管理员拥有所有权限
+                        AuthDto authDto = (AuthDto) redisUtil.get(SystemConstant.userOauth + "::" + userId);
+                        //验证权限
+                        if (Objects.isNull(authDto)) {
+                            authDto = cacheService.addAccountCache(userId);
+                        }
+                        request.setAttribute(SystemConstant.ORG, authDto.getTbOrg());
+                        //系统管理员拥有所有权限
 //                    if (authDto.getRoleCodes().contains(RoleEnum.SUPER_ADMIN.name())) {
 //                        return true;
 //                    }
-                    //系统公用接口不拦截
-                    List<String> sysUrls = dictionaryConfig.systemUrlDomain().getUrls();
-                    int sysCount = (int) sysUrls.stream().filter(s -> {
-                        return s.equalsIgnoreCase(url);
-                    }).count();
-                    if (sysCount > 0) {
-                        return true;
-                    }
-                    Set<String> urls = authDto.getUrls();
-                    int count = (int) urls.stream().filter(s -> {
-                        return s.equalsIgnoreCase(url);
-                    }).count();
-                    if (count == 0) {
-                        throw new BusinessException(ExceptionResultEnum.UN_AUTHORIZATION);
+                        //系统公用接口不拦截
+                        List<String> sysUrls = dictionaryConfig.systemUrlDomain().getUrls();
+                        int sysCount = (int) sysUrls.stream().filter(s -> {
+                            return s.equalsIgnoreCase(url);
+                        }).count();
+                        if (sysCount > 0) {
+                            return true;
+                        }
+                        Set<String> urls = authDto.getUrls();
+                        int count = (int) urls.stream().filter(s -> {
+                            return s.equalsIgnoreCase(url);
+                        }).count();
+                        if (count == 0) {
+                            throw new BusinessException(ExceptionResultEnum.UN_AUTHORIZATION);
+                        }
+                    } else {
+                        throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
                     }
-                } else {
-                    throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
                 }
+            } else {
+                throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
             }
         } else {
             throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
         }
-//        } else {
-//            throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
-//        }
         response.setStatus(ExceptionResultEnum.SUCCESS.getCode());
         return true;
     }

+ 40 - 34
themis-backend/src/main/java/com/qmth/themis/backend/websocket/WebSocketAdminServer.java

@@ -55,6 +55,7 @@ public class WebSocketAdminServer
     private RedisUtil redisUtil;
     private Long updateTime = null;
     private Map<String, Object> tranMap = null;
+    private String url = "/ws/admin";
 
     /**
      * 连接建立成功调用的方法
@@ -78,43 +79,48 @@ public class WebSocketAdminServer
         this.Authorization = String.valueOf(mapParameter.get("Authorization").get(0));
         this.time = Long.parseLong(String.valueOf(mapParameter.get("time").get(0)));
         this.userId = Long.parseLong(String.valueOf(mapParameter.get("userId").get(0)));
-        String method = SystemConstant.GET;
-        final SignatureInfo info = SignatureInfo
-                .parse(Authorization);
-        if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
-            String sessionId = info.getInvoker();
-            redisUtil = SpringContextHolder.getBean(RedisUtil.class);
-            TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
-            if (Objects.isNull(tbSession)) {
-                throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
-            } else {
-                if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
-                        && platform.equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
-                    this.session = session;
-                    session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
-                    this.sessionId = tbSession.getId();
-                    if (webSocketMap.containsKey(this.userId)) {
-                        webSocketMap.remove(this.userId);
-                        webSocketMap.put(this.userId, this);
+//        final SignatureInfo info = SignatureInfo
+//                .parse(Authorization);
+        if (!SystemConstant.expire(this.time.longValue())) {
+            final SignatureInfo info = SignatureInfo
+                    .parse(SystemConstant.GET, url, this.time, this.Authorization);
+            if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
+                String sessionId = info.getInvoker();
+                redisUtil = SpringContextHolder.getBean(RedisUtil.class);
+                TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
+                if (Objects.isNull(tbSession)) {
+                    throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
+                } else {
+                    if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
+                            && platform.equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
+                        this.session = session;
+                        session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
+                        this.sessionId = tbSession.getId();
+                        if (webSocketMap.containsKey(this.userId)) {
+                            webSocketMap.remove(this.userId);
+                            webSocketMap.put(this.userId, this);
+                        } else {
+                            webSocketMap.put(this.userId, this);
+                            addOnlineCount();
+                        }
+                        //发送恢复网络mq消息
+                        log.info("用户连接:{},当前在线人数为:{}", this.sessionId, getOnlineCount());
+                        InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
+                        this.ip = addr.toString().replace("/", "").split(":")[0];
+//                    this.sendMessage("ip[" + this.ip + "]连接成功");
+                        log.info("ip[:{}]连接成功", this.ip);
+                        tranMap = new HashMap<>();
+                        tranMap.put("userId", this.userId);
+                        tranMap.put("deviceId", this.deviceId);
+                        tranMap.put("ip", this.ip);
+                        this.updateTime = System.currentTimeMillis();
+                        tranMap.put("updateTime", this.updateTime);
                     } else {
-                        webSocketMap.put(this.userId, this);
-                        addOnlineCount();
+                        throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
                     }
-                    //发送恢复网络mq消息
-                    log.info("用户连接:{},当前在线人数为:{}", this.sessionId, getOnlineCount());
-                    InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
-                    this.ip = addr.toString().replace("/", "").split(":")[0];
-//                    this.sendMessage("ip[" + this.ip + "]连接成功");
-                    log.info("ip[:{}]连接成功", this.ip);
-                    tranMap = new HashMap<>();
-                    tranMap.put("userId", this.userId);
-                    tranMap.put("deviceId", this.deviceId);
-                    tranMap.put("ip", this.ip);
-                    this.updateTime = System.currentTimeMillis();
-                    tranMap.put("updateTime", this.updateTime);
-                } else {
-                    throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
                 }
+            } else {
+                throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
             }
         } else {
             throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);

+ 7 - 5
themis-business/src/main/java/com/qmth/themis/business/service/impl/TEMobileServiceImpl.java

@@ -18,13 +18,14 @@ import com.qmth.themis.business.enums.*;
 import com.qmth.themis.business.service.MqDtoService;
 import com.qmth.themis.business.service.TEExamStudentService;
 import com.qmth.themis.business.service.TEMobileService;
-import com.qmth.themis.business.util.*;
+import com.qmth.themis.business.util.RedisUtil;
+import com.qmth.themis.business.util.ServletUtil;
+import com.qmth.themis.business.util.SessionUtil;
+import com.qmth.themis.business.util.TencentYunUtil;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.enums.Platform;
 import com.qmth.themis.common.enums.Source;
 import com.qmth.themis.common.exception.BusinessException;
-import com.qmth.themis.common.signature.SignatureInfo;
-import com.qmth.themis.common.signature.SignatureType;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.springframework.stereotype.Service;
 
@@ -95,8 +96,9 @@ public class TEMobileServiceImpl implements TEMobileService {
         mqDtoService.assembleSendOneWayMsg(mqDtoLog);
         //mq发送消息end
         //测试
-        String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
-        ret.setAccessToken(test);
+//        String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
+//        ret.setAccessToken(test);
+        ret.setAccessToken(token);
         return ret;
     }
 

+ 58 - 59
themis-common/src/main/java/com/qmth/themis/common/signature/SignatureInfo.java

@@ -104,19 +104,6 @@ public class SignatureInfo {
         return Base64Util.encode(ShaUtils.sha1(StringUtils.join(values, PARAM_JOINER)));
     }
 
-//    /**
-//     * 基于解析好的签名对象,使用传入的保密信息进行签名内容验证
-//     *
-//     * @param secret
-//     * @return
-//     */
-//    public boolean validate(String secret) {
-//        if (method != null && uri != null && timestamp >= 0 && secret != null && ciphertext != null) {
-//            return encrypt(method, uri, String.valueOf(timestamp), secret).equals(ciphertext);
-//        }
-//        return false;
-//    }
-
     /**
      * 基于解析好的签名对象,使用传入的保密信息进行签名内容验证
      *
@@ -124,35 +111,48 @@ public class SignatureInfo {
      * @return
      */
     public boolean validate(String secret) {
-        if (secret != null && ciphertext != null) {
-            return encrypt(secret).equals(ciphertext);
+        if (method != null && uri != null && timestamp >= 0 && secret != null && ciphertext != null) {
+            return encrypt(method, uri, String.valueOf(timestamp), secret).equals(ciphertext);
         }
         return false;
     }
 
 //    /**
-//     * 根据标准参数构造最终的签名字符串
+//     * 基于解析好的签名对象,使用传入的保密信息进行签名内容验证(测试用)
 //     *
-//     * @param type
-//     * @param method
-//     * @param uri
-//     * @param timestamp
-//     * @param invoker
 //     * @param secret
 //     * @return
 //     */
-//    public static String build(SignatureType type, String method, String uri, long timestamp, String invoker, String secret) {
-//        if (type == null || method == null || uri == null || timestamp <= 0 || invoker == null || secret == null) {
-//            return "";
+//    public boolean validate(String secret) {
+//        if (secret != null && ciphertext != null) {
+//            return encrypt(secret).equals(ciphertext);
 //        }
-//        return MessageFormat.format(PATTERN, type.getName(), invoker, FIELD_JOINER,
-//                encrypt(method.toLowerCase(), uri, String.valueOf(timestamp), secret));
+//        return false;
 //    }
 
     /**
      * 根据标准参数构造最终的签名字符串
      *
      * @param type
+     * @param method
+     * @param uri
+     * @param timestamp
+     * @param invoker
+     * @param secret
+     * @return
+     */
+    public static String build(SignatureType type, String method, String uri, long timestamp, String invoker, String secret) {
+        if (type == null || method == null || uri == null || timestamp <= 0 || invoker == null || secret == null) {
+            return "";
+        }
+        return MessageFormat.format(PATTERN, type.getName(), invoker, FIELD_JOINER,
+                encrypt(method.toLowerCase(), uri, String.valueOf(timestamp), secret));
+    }
+
+    /**
+     * 根据标准参数构造最终的签名字符串(测试用)
+     *
+     * @param type
      * @param invoker
      * @param secret
      * @return
@@ -165,46 +165,45 @@ public class SignatureInfo {
                 encrypt(secret));
     }
 
-//    /**
-//     * 根据当前接口的的基本信息、header中的时间戳与签名字符串,尝试解析并构造签名数据对象
-//     *
-//     * @param method
-//     * @param uri
-//     * @param timestamp
-//     * @param signature
-//     * @return
-//     */
-//    public static SignatureInfo parse(String method, String uri, long timestamp, String signature) {
-//        if (method == null || uri == null || timestamp <= 0 || signature == null) {
-//            return null;
-//        }
-//        String[] values = StringUtils.split(signature);
-//        if (values != null && values.length == 2) {
-//            SignatureType type = typeMap.get(values[0]);
-//            if (type != null) {
-//                String[] array = StringUtils.split(values[1], FIELD_JOINER);
-//                if (array != null && array.length == 2) {
-//                    SignatureInfo info = new SignatureInfo();
-//                    info.setType(type);
-//                    info.setMethod(method.toLowerCase());
-//                    info.setUri(uri);
-//                    info.setTimestamp(timestamp);
-//                    info.setInvoker(array[0]);
-//                    info.setCiphertext(array[1]);
-//                    return info;
-//                }
-//            }
-//        }
-//        return null;
-//    }
-
     /**
      * 根据当前接口的的基本信息、header中的时间戳与签名字符串,尝试解析并构造签名数据对象
      *
+     * @param method
+     * @param uri
      * @param timestamp
      * @param signature
      * @return
      */
+    public static SignatureInfo parse(String method, String uri, long timestamp, String signature) {
+        if (method == null || uri == null || timestamp <= 0 || signature == null) {
+            return null;
+        }
+        String[] values = StringUtils.split(signature);
+        if (values != null && values.length == 2) {
+            SignatureType type = typeMap.get(values[0]);
+            if (type != null) {
+                String[] array = StringUtils.split(values[1], FIELD_JOINER);
+                if (array != null && array.length == 2) {
+                    SignatureInfo info = new SignatureInfo();
+                    info.setType(type);
+                    info.setMethod(method.toLowerCase());
+                    info.setUri(uri);
+                    info.setTimestamp(timestamp);
+                    info.setInvoker(array[0]);
+                    info.setCiphertext(array[1]);
+                    return info;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * 根据当前接口的的基本信息、header中的时间戳与签名字符串,尝试解析并构造签名数据对象(测试用)
+     *
+     * @param signature
+     * @return
+     */
     public static SignatureInfo parse(String signature) {
         if (signature == null) {
             return null;

+ 3 - 3
themis-exam/src/main/java/com/qmth/themis/exam/api/TEStudentController.java

@@ -223,7 +223,7 @@ public class TEStudentController {
         mqDtoService.assembleSendOneWayMsg(mqDtoLog);
         //mq发送消息end
         //测试
-        String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
+//        String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
         Map<String, Object> map = new HashMap<>();
         //获取未完考试
         if (Objects.isNull(ExamingDataCacheUtil.getUnFinishedRecordId(teStudent.getId()))) {
@@ -266,9 +266,9 @@ public class TEStudentController {
         }
         //获取全局考试配置
         TEConfig teConfig = teConfigService.getGlobalConfig();
-//        map.put(SystemConstant.ACCESS_TOKEN, token);
+        map.put(SystemConstant.ACCESS_TOKEN, token);
         map.put(SystemConstant.GLOBAL, teConfig);
-        map.put(SystemConstant.ACCESS_TOKEN, test);
+//        map.put(SystemConstant.ACCESS_TOKEN, test);
         map.put(SystemConstant.STUDENT_ACCOUNT, teStudent);
         map.put(SystemConstant.SESSION_ID, sessionId);
         return ResultUtil.ok(map);

+ 56 - 56
themis-exam/src/main/java/com/qmth/themis/exam/interceptor/AuthInterceptor.java

@@ -64,73 +64,73 @@ public class AuthInterceptor implements HandlerInterceptor {
         String time = ServletUtil.getRequestTime();
         log.info("platform:{},deviceId:{},authorization:{},method:{},time:{}", platform, deviceId, authorization, method, time);
         Long userId = null;
-//        Long timestamp = Long.parseLong(ServletUtil.getRequestTime(request));
-//        if (!SystemConstant.expire(timestamp.longValue())) {
-//            final SignatureInfo info = SignatureInfo
-//                    .parse(method, url, timestamp, ServletUtil.getRequestAuthorization(request));
-        //测试
-        final SignatureInfo info = SignatureInfo
-                .parse(authorization);
-        if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
-            String sessionId = info.getInvoker();
-            TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
-            if (Objects.isNull(tbSession)) {
-                throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
-            } else {
-                if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
-                        && platform.name().equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
-                    userId = Long.parseLong(tbSession.getIdentity());
-                    Long expireTime = tbSession.getExpireTime();
-                    //手机端的token时长为一个月,所以会出现缓存没有的情况
-                    if (expireTime <= System.currentTimeMillis()) {//先判断时间是否过期
-                        throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
-                    }
-                    TEStudentCacheDto teStudentCacheDto = (TEStudentCacheDto) redisUtil.getStudent(userId);
-                    if (Objects.isNull(teStudentCacheDto)) {
-                        TEStudent teStudent = teStudentService.getById(userId);
-                        Gson gson = new Gson();
-                        teStudentCacheDto = gson.fromJson(gson.toJson(teStudent), TEStudentCacheDto.class);
-                        redisUtil.setStudent(teStudent.getId(), teStudentCacheDto);
-                    }
+        Long timestamp = Long.parseLong(time);
+        if (!SystemConstant.expire(timestamp.longValue())) {
+            final SignatureInfo info = SignatureInfo
+                    .parse(method.toLowerCase(), url, timestamp, authorization);
+            //测试
+//        final SignatureInfo info = SignatureInfo
+//                .parse(authorization);
+            if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
+                String sessionId = info.getInvoker();
+                TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
+                if (Objects.isNull(tbSession)) {
+                    throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
+                } else {
+                    if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
+                            && platform.name().equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
+                        userId = Long.parseLong(tbSession.getIdentity());
+                        Long expireTime = tbSession.getExpireTime();
+                        //手机端的token时长为一个月,所以会出现缓存没有的情况
+                        if (expireTime <= System.currentTimeMillis()) {//先判断时间是否过期
+                            throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
+                        }
+                        TEStudentCacheDto teStudentCacheDto = (TEStudentCacheDto) redisUtil.getStudent(userId);
+                        if (Objects.isNull(teStudentCacheDto)) {
+                            TEStudent teStudent = teStudentService.getById(userId);
+                            Gson gson = new Gson();
+                            teStudentCacheDto = gson.fromJson(gson.toJson(teStudent), TEStudentCacheDto.class);
+                            redisUtil.setStudent(teStudent.getId(), teStudentCacheDto);
+                        }
 
-                    request.setAttribute(SystemConstant.SESSION, tbSession);
-                    request.setAttribute(SystemConstant.STUDENT_ACCOUNT, teStudentCacheDto);
+                        request.setAttribute(SystemConstant.SESSION, tbSession);
+                        request.setAttribute(SystemConstant.STUDENT_ACCOUNT, teStudentCacheDto);
 
-                    AuthDto authDto = (AuthDto) redisUtil.get(SystemConstant.studentOauth + "::" + userId);
-                    //验证权限
-                    if (Objects.isNull(authDto)) {
-                        authDto = cacheService.addStudentCache(userId);
-                    }
-                    request.setAttribute(SystemConstant.ORG, authDto.getTbOrg());
-                    //系统管理员拥有所有权限
+                        AuthDto authDto = (AuthDto) redisUtil.get(SystemConstant.studentOauth + "::" + userId);
+                        //验证权限
+                        if (Objects.isNull(authDto)) {
+                            authDto = cacheService.addStudentCache(userId);
+                        }
+                        request.setAttribute(SystemConstant.ORG, authDto.getTbOrg());
+                        //系统管理员拥有所有权限
 //                    if (authDto.getRoleCodes().contains(RoleEnum.SUPER_ADMIN.name())) {
 //                        return true;
 //                    }
-                    //系统公用接口不拦截
-                    List<String> sysUrls = dictionaryConfig.systemUrlDomain().getUrls();
-                    int sysCount = (int) sysUrls.stream().filter(s -> {
-                        return s.equalsIgnoreCase(url);
-                    }).count();
-                    if (sysCount > 0) {
-                        return true;
-                    }
-                    Set<String> urls = authDto.getUrls();
-                    int count = (int) urls.stream().filter(s -> {
-                        return s.equalsIgnoreCase(url);
-                    }).count();
-                    if (count == 0) {
-                        throw new BusinessException(ExceptionResultEnum.UN_AUTHORIZATION);
+                        //系统公用接口不拦截
+                        List<String> sysUrls = dictionaryConfig.systemUrlDomain().getUrls();
+                        int sysCount = (int) sysUrls.stream().filter(s -> {
+                            return s.equalsIgnoreCase(url);
+                        }).count();
+                        if (sysCount > 0) {
+                            return true;
+                        }
+                        Set<String> urls = authDto.getUrls();
+                        int count = (int) urls.stream().filter(s -> {
+                            return s.equalsIgnoreCase(url);
+                        }).count();
+                        if (count == 0) {
+                            throw new BusinessException(ExceptionResultEnum.UN_AUTHORIZATION);
+                        }
+                    } else {
+                        throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
                     }
-                } else {
-                    throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
                 }
+            } else {
+                throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
             }
         } else {
             throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
         }
-//        } else {
-//            throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
-//        }
         response.setStatus(ExceptionResultEnum.SUCCESS.getCode());
         return true;
     }

+ 39 - 33
themis-exam/src/main/java/com/qmth/themis/exam/websocket/WebSocketMobileServer.java

@@ -64,6 +64,7 @@ public class WebSocketMobileServer implements Concurrently {
     private Long updateTime = null;
     private Map<String, Object> tranMap = null;
     private MonitorVideoSourceEnum source = null;
+    private String url = "/ws/mobile";
 
     /**
      * 连接建立成功调用的方法
@@ -88,43 +89,48 @@ public class WebSocketMobileServer implements Concurrently {
         this.time = Long.parseLong(String.valueOf(mapParameter.get("time").get(0)));
         this.recordId = Long.parseLong(String.valueOf(mapParameter.get("recordId").get(0)));
         this.source = MonitorVideoSourceEnum.valueOf(mapParameter.get("source").get(0));
-        String method = SystemConstant.GET;
-        final SignatureInfo info = SignatureInfo
-                .parse(Authorization);
-        if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
-            String sessionId = info.getInvoker();
-            redisUtil = SpringContextHolder.getBean(RedisUtil.class);
-            TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
-            if (Objects.isNull(tbSession)) {
-                throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
-            } else {
-                if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
-                        && platform.equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
-                    this.session = session;
-                    session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
-                    this.sessionId = tbSession.getId();
-                    if (webSocketMap.containsKey(this.recordId + "-" + this.source.name())) {
-                        webSocketMap.remove(this.recordId + "-" + this.source.name());
-                        webSocketMap.put(this.recordId + "-" + this.source.name(), this);
-                    } else {
-                        webSocketMap.put(this.recordId + "-" + this.source.name(), this);
+//        final SignatureInfo info = SignatureInfo
+//                .parse(Authorization);
+        if (!SystemConstant.expire(this.time.longValue())) {
+            final SignatureInfo info = SignatureInfo
+                    .parse(SystemConstant.GET, url, this.time, this.Authorization);
+            if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
+                String sessionId = info.getInvoker();
+                redisUtil = SpringContextHolder.getBean(RedisUtil.class);
+                TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
+                if (Objects.isNull(tbSession)) {
+                    throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
+                } else {
+                    if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
+                            && platform.equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
+                        this.session = session;
+                        session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
+                        this.sessionId = tbSession.getId();
+                        if (webSocketMap.containsKey(this.recordId + "-" + this.source.name())) {
+                            webSocketMap.remove(this.recordId + "-" + this.source.name());
+                            webSocketMap.put(this.recordId + "-" + this.source.name(), this);
+                        } else {
+                            webSocketMap.put(this.recordId + "-" + this.source.name(), this);
 //                        addOnlineCount();
-                    }
+                        }
 //                    log.info("用户连接:{},当前在线人数为:{}", this.sessionId, getOnlineCount());
-                    log.info("用户连接:{}", this.sessionId);
-                    InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
-                    this.ip = addr.toString().replace("/", "").split(":")[0];
+                        log.info("用户连接:{}", this.sessionId);
+                        InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
+                        this.ip = addr.toString().replace("/", "").split(":")[0];
 //                    this.sendMessage("ip[" + this.ip + "]连接成功");
-                    log.info("ip[:{}]连接成功", this.ip);
-                    tranMap = new HashMap<>();
-                    tranMap.put("recordId", this.recordId);
-                    tranMap.put("deviceId", this.deviceId);
-                    tranMap.put("ip", this.ip);
-                    this.updateTime = System.currentTimeMillis();
-                    tranMap.put("updateTime", this.updateTime);
-                } else {
-                    throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
+                        log.info("ip[:{}]连接成功", this.ip);
+                        tranMap = new HashMap<>();
+                        tranMap.put("recordId", this.recordId);
+                        tranMap.put("deviceId", this.deviceId);
+                        tranMap.put("ip", this.ip);
+                        this.updateTime = System.currentTimeMillis();
+                        tranMap.put("updateTime", this.updateTime);
+                    } else {
+                        throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
+                    }
                 }
+            } else {
+                throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
             }
         } else {
             throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);

+ 48 - 42
themis-exam/src/main/java/com/qmth/themis/exam/websocket/WebSocketOeServer.java

@@ -63,6 +63,7 @@ public class WebSocketOeServer implements Concurrently {
     private RedisUtil redisUtil;
     private Long updateTime = null;
     private Map<String, Object> tranMap = null;
+    private String url = "/ws/oe";
 
     /**
      * 连接建立成功调用的方法
@@ -86,51 +87,56 @@ public class WebSocketOeServer implements Concurrently {
         this.Authorization = String.valueOf(mapParameter.get("Authorization").get(0));
         this.time = Long.parseLong(String.valueOf(mapParameter.get("time").get(0)));
         this.recordId = Long.parseLong(String.valueOf(mapParameter.get("recordId").get(0)));
-        String method = SystemConstant.GET;
-        final SignatureInfo info = SignatureInfo
-                .parse(Authorization);
-        if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
-            String sessionId = info.getInvoker();
-            redisUtil = SpringContextHolder.getBean(RedisUtil.class);
-            TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
-            if (Objects.isNull(tbSession)) {
-                throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
-            } else {
-                if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
-                        && platform.equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
-                    this.session = session;
-                    session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
-                    this.sessionId = tbSession.getId();
-                    if (webSocketMap.containsKey(this.recordId)) {
-                        webSocketMap.remove(this.recordId);
-                        webSocketMap.put(this.recordId, this);
+//        final SignatureInfo info = SignatureInfo
+//                .parse(Authorization);
+        if (!SystemConstant.expire(this.time.longValue())) {
+            final SignatureInfo info = SignatureInfo
+                    .parse(SystemConstant.GET, url, this.time, this.Authorization);
+            if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
+                String sessionId = info.getInvoker();
+                redisUtil = SpringContextHolder.getBean(RedisUtil.class);
+                TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
+                if (Objects.isNull(tbSession)) {
+                    throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
+                } else {
+                    if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime()
+                            && platform.equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
+                        this.session = session;
+                        session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
+                        this.sessionId = tbSession.getId();
+                        if (webSocketMap.containsKey(this.recordId)) {
+                            webSocketMap.remove(this.recordId);
+                            webSocketMap.put(this.recordId, this);
+                        } else {
+                            webSocketMap.put(this.recordId, this);
+                            addOnlineCount();
+                        }
+                        log.info("用户连接:{},当前在线人数为:{}", this.sessionId, getOnlineCount());
+                        InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
+                        this.ip = addr.toString().replace("/", "").split(":")[0];
+//                    this.sendMessage("ip[" + this.ip + "]连接成功");
+                        log.info("ip[:{}]连接成功", this.ip);
+                        ExamRecordCacheUtil.setClientWebsocketStatus(recordId, WebsocketStatusEnum.ON_LINE, false);
+                        ExamRecordCacheUtil.setClientCurrentIp(recordId, this.ip, false);
+                        ExamRecordCacheUtil.setClientWebsocketId(recordId, this.session.getId(), false);
+                        Long clientLastSyncTime = System.currentTimeMillis();
+                        ExamRecordCacheUtil.setClientLastSyncTime(recordId, clientLastSyncTime, false);
+                        String[] columns = new String[]{ExamRecordFieldEnum.client_websocket_status.name(), ExamRecordFieldEnum.client_current_ip.name(), ExamRecordFieldEnum.client_websocket_id.name(), ExamRecordFieldEnum.client_last_sync_time.name()};
+                        Object[] values = new Object[]{WebsocketStatusEnum.ON_LINE, this.ip, this.session.getId(), clientLastSyncTime};
+                        TOeExamRecordService tOeExamRecordService = SpringContextHolder.getBean(TOeExamRecordService.class);
+                        tOeExamRecordService.dataUpdatesMq(recordId, columns, values);
+                        tranMap = new HashMap<>();
+                        tranMap.put("recordId", this.recordId);
+                        tranMap.put("deviceId", this.deviceId);
+                        tranMap.put("ip", this.ip);
+                        this.updateTime = System.currentTimeMillis();
+                        tranMap.put("updateTime", this.updateTime);
                     } else {
-                        webSocketMap.put(this.recordId, this);
-                        addOnlineCount();
+                        throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
                     }
-                    log.info("用户连接:{},当前在线人数为:{}", this.sessionId, getOnlineCount());
-                    InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
-                    this.ip = addr.toString().replace("/", "").split(":")[0];
-//                    this.sendMessage("ip[" + this.ip + "]连接成功");
-                    log.info("ip[:{}]连接成功", this.ip);
-                    ExamRecordCacheUtil.setClientWebsocketStatus(recordId, WebsocketStatusEnum.ON_LINE, false);
-                    ExamRecordCacheUtil.setClientCurrentIp(recordId, this.ip, false);
-                    ExamRecordCacheUtil.setClientWebsocketId(recordId, this.session.getId(), false);
-                    Long clientLastSyncTime = System.currentTimeMillis();
-                    ExamRecordCacheUtil.setClientLastSyncTime(recordId, clientLastSyncTime, false);
-                    String[] columns = new String[]{ExamRecordFieldEnum.client_websocket_status.name(), ExamRecordFieldEnum.client_current_ip.name(), ExamRecordFieldEnum.client_websocket_id.name(), ExamRecordFieldEnum.client_last_sync_time.name()};
-                    Object[] values = new Object[]{WebsocketStatusEnum.ON_LINE, this.ip, this.session.getId(), clientLastSyncTime};
-                    TOeExamRecordService tOeExamRecordService = SpringContextHolder.getBean(TOeExamRecordService.class);
-                    tOeExamRecordService.dataUpdatesMq(recordId, columns, values);
-                    tranMap = new HashMap<>();
-                    tranMap.put("recordId", this.recordId);
-                    tranMap.put("deviceId", this.deviceId);
-                    tranMap.put("ip", this.ip);
-                    this.updateTime = System.currentTimeMillis();
-                    tranMap.put("updateTime", this.updateTime);
-                } else {
-                    throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
                 }
+            } else {
+                throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
             }
         } else {
             throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);