whycxzp
2022-03-22 c7966221c3cef9fc77ec81aa9acba23e0bcc8d7d
src/main/java/com/whyc/service/LoginService.java
@@ -3,6 +3,7 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.constant.UserConstant;
import com.whyc.constant.UserOperation;
import com.whyc.constant.YamlProperties;
import com.whyc.dto.Response;
import com.whyc.mapper.PageParamMapper;
@@ -12,6 +13,7 @@
import com.whyc.pojo.UserClient;
import com.whyc.pojo.UserInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.RSAUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
@@ -122,17 +124,40 @@
        }
        UsernamePasswordToken userToken = new UsernamePasswordToken(userName, dataArr[0]);
        Subject subject = SecurityUtils.getSubject();
        ServletContext servletContext = request.getServletContext();
        Enumeration<String> attributeNames = servletContext.getAttributeNames();
        try {
            subject.login(userToken);
        }catch (Exception e){
            String message = e.getMessage();
            if(message.contains("did not match the expected credentials")){
                //密码错误,记录次数
                //内存中查找该用户中的登录失败次数
                int loginFailTimes = 0;
                List<String> loginFailAttributeList = new LinkedList<>();
                while (attributeNames.hasMoreElements()){
                    String attributeName = attributeNames.nextElement();
                    if(attributeName.contains(userName+"_login_fail_times_")){
                        loginFailTimes++;
                        loginFailAttributeList.add(attributeName);
                    }
                }
                //查询账号密码错误限制次数
                PageParam loginFailTimesLimit = pageParamMapper.findByCategoryId(9).get(0);
                if((++loginFailTimes)==loginFailTimesLimit.getStatus()){
                    //达到限制次数,锁定账号
                    //userService.lock(subject.getUId());
                    //清除登录错误次数统计
                    loginFailAttributeList.forEach(servletContext::removeAttribute);
                }else {
                    servletContext.setAttribute(userName + "_login_fail_times_"+System.currentTimeMillis(), 0);
                }
                CommonUtil.record(0, UserOperation.TYPE_LOGIN_FAIL.getType(), UserOperation.TYPE_LOGIN_FAIL.getTypeName());
                return response.set(1,false,"密码错误");
            }
            return response.set(1,false,message);
        }
        ServletContext servletContext = request.getServletContext();
        Enumeration<String> attributeNames = servletContext.getAttributeNames();
        QueryWrapper<UserInf> queryWrapper = Wrappers.query();
        queryWrapper.select("uId","status","visit_ip","visit_time","password_update_time","last_login_time").eq("uName",userName);
@@ -144,11 +169,20 @@
            //查询账号状态
            if(userInf.getStatus()!=1){
                switch (userInf.getStatus()){
                    case 0: response.setMsg("当前账号的状态异常,无法登录. 异常信息为: "+ UserConstant.ACCOUNT_STATUS_CANCEL.getLabel());break;
                    case 2: response.setMsg("当前账号的状态异常,无法登录. 异常信息为: "+ UserConstant.ACCOUNT_STATUS_HIBERNATE.getLabel());break;
                    case 3: response.setMsg("当前账号的状态异常,无法登录. 异常信息为: "+ UserConstant.ACCOUNT_STATUS_LOCK.getLabel());break;
                    case 4: response.setMsg("当前账号的状态异常,无法登录. 异常信息为: "+ UserConstant.ACCOUNT_STATUS_LOCK_FAIL.getLabel());break;
                    default:response.setMsg("当前账号的状态异常,无法登录. 异常信息为: 无");
                    case 0:
                        response.setMsg("当前账号的状态异常,无法登录. 异常信息为: " + UserConstant.ACCOUNT_STATUS_CANCEL.getLabel());
                        break;
                    case 2:
                        response.setMsg("当前账号的状态异常,无法登录. 异常信息为: " + UserConstant.ACCOUNT_STATUS_HIBERNATE.getLabel());
                        break;
                    case 3:
                        response.setMsg("当前账号的状态异常,无法登录. 异常信息为: " + UserConstant.ACCOUNT_STATUS_LOCK.getLabel());
                        break;
                    case 4:
                        response.setMsg("当前账号的状态异常,无法登录. 异常信息为: " + UserConstant.ACCOUNT_STATUS_LOCK_FAIL.getLabel());
                        break;
                    default:
                        response.setMsg("当前账号的状态异常,无法登录. 异常信息为: 无");
                }
                return  response.set(1,false);
            }
@@ -206,8 +240,7 @@
                    response.setData(false);
                    response.setMsg("首次登录,请先修改初始化口令");
                    return response;
                }
                else if(passwordUpdateTime.compareTo(now.getTime()) < 0){
                } else if (passwordUpdateTime.compareTo(now.getTime()) < 0) {
                    response.setCode(2);
                    response.setData(false);
                    response.setMsg("超过3个月没有修改口令,请修改口令后重新登录");
@@ -234,31 +267,10 @@
            dataList.add(subject.getPrincipal());
            int permitGroupId = permitGroupUserService.getPermitGroupId(userInf.getUId());
            dataList.add(permitGroupId);
            CommonUtil.record(((UserInf) subject.getPrincipal()).getUId(), UserOperation.TYPE_LOGIN.getType(), UserOperation.TYPE_LOGIN.getTypeName());
            return new Response<>().setII(1,true,dataList,"登录成功");
        }
        //密码错误,记录次数
        //内存中查找该用户中的登录失败次数
        int loginFailTimes = 0;
        List<String> loginFailAttributeList = new LinkedList<>();
        while (attributeNames.hasMoreElements()){
            String attributeName = attributeNames.nextElement();
            if(attributeName.contains(userName+"_login_fail_times_")){
                loginFailTimes++;
                loginFailAttributeList.add(attributeName);
            }
        }
        //查询账号密码错误限制次数
        PageParam loginFailTimesLimit = pageParamMapper.findByCategoryId(9).get(0);
        if((++loginFailTimes)==loginFailTimesLimit.getStatus()){
            //达到限制次数,锁定账号
            userService.lock(userInf.getUId());
            //清除登录错误次数统计
            loginFailAttributeList.forEach(servletContext::removeAttribute);
        }else {
            servletContext.setAttribute(userName + "_login_fail_times_"+System.currentTimeMillis(), 0);
        }
        return new Response<>().set(1,false,"密码错误");
        return new Response().set(1,false,"认证未通过");
    }
    public Response loginWithUKey(String userName, String password, HttpServletRequest request) {