Lius
2023-12-07 4337640b7ab86136baed2fd6aa959e4828a75cea
lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/controller/LoginController.java
@@ -4,6 +4,7 @@
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -14,6 +15,7 @@
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.jeecg.common.util.encryption.EncryptedString;
@@ -29,7 +31,6 @@
import org.jeecg.modules.system.util.RandImageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -103,7 +104,7 @@
         return result;
      }*/
      //update-end-author:taoyan date:20190828 for:校验验证码
      //1. 校验用户是否有效
      //update-begin-author:wangshuai date:20200601 for: 登录代码验证用户是否注销bug,if条件永远为false
      LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
@@ -119,10 +120,51 @@
      String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
      String syspassword = sysUser.getPassword();
      if (!syspassword.equals(userpassword)) {
         result.error500("用户名或密码错误");
         // 获取用户失败最大次数
         //Integer maxAttempts = CommonConstant.MAX_ATTEMPTS;
         List<DictModel> dictModels = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_MAX_ATTEMPTS);
         if (dictModels != null && !dictModels.isEmpty()) {
            Integer maxAttempts = Integer.valueOf(dictModels.get(0).getValue());
            if (redisUtil.hasKey(CommonConstant.PREFIX_LOGIN_COUNT + username)) {
               Integer loginCount = (Integer) redisUtil.get(CommonConstant.PREFIX_LOGIN_COUNT + username) + 1;
               if (loginCount < maxAttempts) {
                  result.error500("用户名或密码错误,请重新尝试。剩余尝试次数:" + (maxAttempts - loginCount));
                  redisUtil.set(CommonConstant.PREFIX_LOGIN_COUNT + username, loginCount + 1, 300);
               } else if (loginCount.equals(maxAttempts)) {
                  // 超过最大登录次数 锁定用户
                  //sysUserService.update(new SysUser().setStatus(CommonConstant.USER_FREEZE), new UpdateWrapper<SysUser>().lambda().eq(SysUser::getUsername, username));
                  // 获取用户失败锁定时长
                  List<DictModel> dictModelList = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_USER_FREEZE);
                  if (dictModelList != null && !dictModelList.isEmpty()) {
                     Integer userFreezeTime = Integer.valueOf(dictModelList.get(0).getValue());
                     redisUtil.set(CommonConstant.PREFIX_LOGIN_COUNT + username, loginCount + 1, userFreezeTime * 60);
                     result.error500("用户名或密码错误,已到达最大尝试次数,请稍后尝试!");
                  } else {
                     //修改数据库锁定用户
                     sysUserService.update(new SysUser().setStatus(CommonConstant.USER_FREEZE), new UpdateWrapper<SysUser>().lambda().eq(SysUser::getUsername, username));
                     //删除redis信息
                     redisUtil.del(CommonConstant.PREFIX_LOGIN_COUNT + username);
                     result.error500("用户名或密码错误,已到达最大尝试次数,请联系管理员解锁!");
                  }
               } else {
                  result.error500("您的账户已锁定,请稍后尝试!");
               }
            } else {
               redisUtil.set(CommonConstant.PREFIX_LOGIN_COUNT + username, 1, 300);
               result.error500("用户名或密码错误,请重新尝试。剩余尝试次数:" + (maxAttempts - 1));
            }
         }
         //result.error500("用户名或密码错误");
         return result;
      } else {
         if (redisUtil.hasKey(CommonConstant.PREFIX_LOGIN_COUNT + username)) {
            redisUtil.del(CommonConstant.PREFIX_LOGIN_COUNT + username);
         }
      }
      //用户登录信息
      userInfo(sysUser, result);
      //update-begin--Author:liusq  Date:20210126  for:登录成功,删除redis中的验证码
@@ -161,7 +203,7 @@
         }
         //update-begin---author:liusq ---date:2022-06-29  for:接口返回值修改,同步修改这里的判断逻辑-----------
         //update-end---author:scott ---date::2022-06-20  for:vue3前端,支持自定义首页--------------
         obj.put("userInfo",sysUser);
         obj.put("sysAllDictItems", sysDictService.queryAllDictItems());
         result.setResult(obj);
@@ -170,7 +212,7 @@
      return result;
   }
   /**
    * 退出登录
    * @param request
@@ -204,7 +246,7 @@
          return Result.error("Token无效!");
       }
   }
   /**
    * 获取访问量
    * @return
@@ -235,7 +277,7 @@
      result.success("登录成功");
      return result;
   }
   /**
    * 获取访问量
    * @return
@@ -256,8 +298,8 @@
      result.setResult(oConvertUtils.toLowerCasePageList(list));
      return result;
   }
   /**
    * 登陆成功选择用户当前部门
    * @param user
@@ -282,7 +324,7 @@
   /**
    * 短信登录接口
    *
    *
    * @param jsonObject
    * @return
    */
@@ -298,12 +340,12 @@
         result.setSuccess(false);
         return result;
      }
      //update-begin-author:taoyan date:2022-9-13 for: VUEN-2245 【漏洞】发现新漏洞待处理20220906
      String redisKey = CommonConstant.PHONE_REDIS_KEY_PRE+mobile;
      Object object = redisUtil.get(redisKey);
      //update-end-author:taoyan date:2022-9-13 for: VUEN-2245 【漏洞】发现新漏洞待处理20220906
      if (object != null) {
         result.setMessage("验证码10分钟内,仍然有效!");
         result.setSuccess(false);
@@ -337,7 +379,7 @@
               }
               return result;
            }
            /**
             * smsmode 短信模板方式  0 .登录模板、1.注册模板、2.忘记密码模板
             */
@@ -355,12 +397,12 @@
            result.setSuccess(false);
            return result;
         }
         //update-begin-author:taoyan date:2022-9-13 for: VUEN-2245 【漏洞】发现新漏洞待处理20220906
         //验证码10分钟内有效
         redisUtil.set(redisKey, captcha, 600);
         //update-end-author:taoyan date:2022-9-13 for: VUEN-2245 【漏洞】发现新漏洞待处理20220906
         //update-begin--Author:scott  Date:20190812 for:issues#391
         //result.setResult(captcha);
         //update-end--Author:scott  Date:20190812 for:issues#391
@@ -373,11 +415,11 @@
      }
      return result;
   }
   /**
    * 手机号登录接口
    *
    *
    * @param jsonObject
    * @return
    */
@@ -386,14 +428,14 @@
   public Result<JSONObject> phoneLogin(@RequestBody JSONObject jsonObject) {
      Result<JSONObject> result = new Result<JSONObject>();
      String phone = jsonObject.getString("mobile");
      //校验用户有效性
      SysUser sysUser = sysUserService.getUserByPhone(phone);
      result = sysUserService.checkUserIsEffective(sysUser);
      if(!result.isSuccess()) {
         return result;
      }
      String smscode = jsonObject.getString("captcha");
      //update-begin-author:taoyan date:2022-9-13 for: VUEN-2245 【漏洞】发现新漏洞待处理20220906
@@ -429,9 +471,16 @@
      // 生成token
      String token = JwtUtil.sign(username, syspassword);
      // 获取token缓存有效时间
      Integer expireTime = CommonConstant.TOKEN_EXPIRE_TIME;
      List<DictModel> dictModels = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_TOKEN_EXPIRE_TIME);
      if (dictModels != null && !dictModels.isEmpty()) {
         expireTime = Integer.valueOf(dictModels.get(0).getValue());
      }
      // 设置token缓存有效时间
      redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
      redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
      redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token, (long) (expireTime + 1) * 60 * 60);
      //redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, );
      obj.put("token", token);
      // update-begin--Author:sunjianlei Date:20210802 for:获取用户租户信息
@@ -505,13 +554,13 @@
         String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
         //存到redis中
         String lowerCaseCode = code.toLowerCase();
         //update-begin-author:taoyan date:2022-9-13 for: VUEN-2245 【漏洞】发现新漏洞待处理20220906
         // 加入密钥作为混淆,避免简单的拼接,被外部利用,用户自定义该密钥即可
         String origin = lowerCaseCode+key+jeecgBaseConfig.getSignatureSecret();
         String realKey = Md5Util.md5Encode(origin, "utf-8");
         //update-end-author:taoyan date:2022-9-13 for: VUEN-2245 【漏洞】发现新漏洞待处理20220906
         redisUtil.set(realKey, lowerCaseCode, 60);
         log.info("获取验证码,Redis key = {},checkCode = {}", realKey, code);
         //返回前端
@@ -535,7 +584,7 @@
      sysPermissionService.switchVue3Menu();
      return res;
   }
   /**
    * app登录
    * @param sysLoginModel
@@ -547,14 +596,14 @@
      Result<JSONObject> result = new Result<JSONObject>();
      String username = sysLoginModel.getUsername();
      String password = sysLoginModel.getPassword();
      //1. 校验用户是否有效
      SysUser sysUser = sysUserService.getUserByName(username);
      result = sysUserService.checkUserIsEffective(sysUser);
      if(!result.isSuccess()) {
         return result;
      }
      //2. 校验用户名或密码是否正确
      String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
      String syspassword = sysUser.getPassword();
@@ -562,7 +611,7 @@
         result.error500("用户名或密码错误");
         return result;
      }
      String orgCode = sysUser.getOrgCode();
      if(oConvertUtils.isEmpty(orgCode)) {
         //如果当前用户无选择部门 查看部门关联信息
@@ -581,7 +630,7 @@
      JSONObject obj = new JSONObject();
      //用户登录信息
      obj.put("userInfo", sysUser);
      // 生成token
      String token = JwtUtil.sign(username, syspassword);
      // 设置超时时间