zero 11 місяців тому
батько
коміт
d9d4d8e871

+ 33 - 42
PCS/src/main/java/cn/cslg/permission/service/LoginService.java

@@ -349,21 +349,9 @@ public class LoginService extends ServiceImpl<PersonnelMapper, Personnel> {
         }
     }
 
-    /**
-     * 登录加密
-     *
-     * @param vo
-     * @return
-     * @throws Exception
-     */
-    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
-    public String loginByEncryption(EncryptionLoginDTO vo) throws Exception {
-        final String username = vo.getUsername();
-        final String password = vo.getPassword();
-        String machineCode = vo.getMachineCode();
-        final String sign = vo.getSign();
-        final String appKey = vo.getAppKey();
-        long currentTimeMillis = vo.getCurrentTimeMillis() / 1000;
+    //校验登录时参数
+    public void checkRational(String machineCode, String sign, String appKey, long timeMillis) {
+        long currentTimeMillis = timeMillis / 1000;
         long currentTimeSecond = System.currentTimeMillis() / 1000;
         final long second = currentTimeSecond - currentTimeMillis;
         if (second > 30) {
@@ -377,6 +365,20 @@ public class LoginService extends ServiceImpl<PersonnelMapper, Personnel> {
         if (Boolean.TRUE.equals(StringUtils.isEmpty(machineCode))) {
             throw new XiaoShiException(ExceptionEnum.THE_MACHINE_CODE_IS_NULL);
         }
+    }
+    /**
+     * 登录加密
+     *
+     * @param vo
+     * @return
+     * @throws Exception
+     */
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
+    public String loginByEncryption(EncryptionLoginDTO vo) throws Exception {
+        final String username = vo.getUsername();
+        final String password = vo.getPassword();
+        String machineCode = vo.getMachineCode();
+        this.checkRational(machineCode, vo.getSign(), vo.getAppKey(), vo.getCurrentTimeMillis());
         //用用户名查询人员信息
         LambdaQueryWrapper<Personnel> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(Personnel::getPersonnelPhone, username)
@@ -470,6 +472,20 @@ public class LoginService extends ServiceImpl<PersonnelMapper, Personnel> {
         }
     }
 
+    public void checkedRational(String sign, String appKey, long TimeMillis) {
+        long currentTimeMillis = TimeMillis / 1000;
+        long currentTimeSecond = System.currentTimeMillis() / 1000;
+        final long second = currentTimeSecond - currentTimeMillis;
+        if (second > 30) {
+            throw new XiaoShiException(ExceptionEnum.THE_REQUEST_TIME_OVERTIME);
+        }
+        String appSecret = appKey + currentTimeMillis;
+        String md5Sign = SecureUtil.md5(appSecret);
+        if (Boolean.TRUE.equals(StringUtils.isEmpty(sign)) || !sign.equals(md5Sign)) {
+            throw new XiaoShiException(ExceptionEnum.THE_SIGN_IS_NOT_SAME);
+        }
+    }
+
     /**
      * 功能模块化代码加密
      *
@@ -484,17 +500,7 @@ public class LoginService extends ServiceImpl<PersonnelMapper, Personnel> {
         if (Boolean.TRUE.equals(StringUtils.isEmpty(version))) {
             version = "1.0.0.0";
         }
-        long currentTimeMillis = vo.getCurrentTimeMillis() / 1000;
-        long currentTimeSecond = System.currentTimeMillis() / 1000;
-        long second = currentTimeSecond - currentTimeMillis;
-        if (second > 30) {
-            return Response.error(ResponseEnum.THE_REQUEST_TIME_OVERTIME);
-        }
-        String appSecret = appKey + currentTimeMillis;
-        String md5Sign = SecureUtil.md5(appSecret);
-        if (Boolean.TRUE.equals(StringUtils.isEmpty(sign)) || !sign.equals(md5Sign)) {
-            return Response.error(ResponseEnum.THE_SIGN_IS_NOT_SAME);
-        }
+        this.checkedRational(sign, appKey, vo.getCurrentTimeMillis());
         Integer userId = StpUtil.getLoginIdAsInt();
         Personnel personnel = this.getById(userId);
         if (ObjectUtils.isEmpty(personnel)) {
@@ -600,22 +606,7 @@ public class LoginService extends ServiceImpl<PersonnelMapper, Personnel> {
     @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
     public PhoneLoginVO loginByPhone(PhoneLoginDTO dto) throws Exception {
         String machineCode = dto.getMachineCode();
-        final String sign = dto.getSign();
-        final String appKey = dto.getAppKey();
-        long currentTimeMillis = dto.getCurrentTimeMillis() / 1000;
-        long currentTimeSecond = System.currentTimeMillis() / 1000;
-        final long second = currentTimeSecond - currentTimeMillis;
-        if (second > 30) {
-            throw new XiaoShiException(ExceptionEnum.THE_REQUEST_TIME_OVERTIME);
-        }
-        String appSecret = appKey + currentTimeMillis;
-        String md5Sign = SecureUtil.md5(appSecret);
-        if (Boolean.TRUE.equals(StringUtils.isEmpty(sign)) || !sign.equals(md5Sign)) {
-            throw new XiaoShiException(ExceptionEnum.THE_SIGN_IS_NOT_SAME);
-        }
-        if (Boolean.TRUE.equals(StringUtils.isEmpty(machineCode))) {
-            throw new XiaoShiException(ExceptionEnum.THE_MACHINE_CODE_IS_NULL);
-        }
+        this.checkRational(machineCode, dto.getSign(), dto.getAppKey(), dto.getCurrentTimeMillis());
         //获取缓存中验证码
         String code = redisUtil.get(LoginCacheKeyUtil.getLoginCaptcha(dto.getPhoneNum()));
         if (Boolean.TRUE.equals(StringUtils.isEmpty(code))) {