后端登录校验,方法抽离,使用Lambda表达式

public LoginUser login(String username, String password)
    {
        // 用户名或密码为空 错误
        this.verifyLogin(username, () -> StringUtils.isAnyBlank(username, password), LoginTypeEnum.USER_PASSWORD_MUST_ENTERED);
        //if (StringUtils.isAnyBlank(username, password))
        //{
        //    recordLogininfor(username, LoginTypeEnum.USER_PASSWORD_MUST_ENTERED);
        //    throw new ServiceException(LoginTypeEnum.USER_PASSWORD_MUST_ENTERED.getDes());
        //}
        // 密码如果不在指定范围内 错误
        this.verifyLogin(username, () -> password.length() < UserConstants.PASSWORD_MIN_LENGTH || password.length() > UserConstants.PASSWORD_MAX_LENGTH, LoginTypeEnum.PASSWORD_NOT_WITHIN_SPECIFIED_RANGE);
        //if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
        //        || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        //{
        //    recordLogininfor(username, LoginTypeEnum.PASSWORD_NOT_WITHIN_SPECIFIED_RANGE);
        //    throw new ServiceException(LoginTypeEnum.PASSWORD_NOT_WITHIN_SPECIFIED_RANGE.getDes());
        //}
        // 用户名不在指定范围内 错误
        this.verifyLogin(username, () -> username.length() < UserConstants.USERNAME_MIN_LENGTH || username.length() > UserConstants.USERNAME_MAX_LENGTH, LoginTypeEnum.USERNAME_NOT_WITHIN_SPECIFIED_RANGE);
        //if (username.length() < UserConstants.USERNAME_MIN_LENGTH
        //        || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        //{
        //    recordLogininfor(username, LoginTypeEnum.USERNAME_NOT_WITHIN_SPECIFIED_RANGE);
        //    throw new ServiceException(LoginTypeEnum.USERNAME_NOT_WITHIN_SPECIFIED_RANGE.getDes());
        //}
        // 查询用户信息
        R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);

        if (R.FAIL == userResult.getCode()) {
            throw new ServiceException(userResult.getMsg());
        }

        this.verifyLogin(username, () -> StringUtils.isNull(userResult.getData()), LoginTypeEnum.USER_NOT_EXIST);
        //if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
        //    recordLogininfor(username, LoginTypeEnum.USER_NOT_EXIST);
        //    throw new ServiceException("登录用户:" + username + " 不存在");
        //}
        LoginUser userInfo = userResult.getData();
        SysUser user = userResult.getData().getSysUser();
        this.verifyLogin(username, () -> UserStatus.DELETED.getCode().equals(user.getDelFlag()), LoginTypeEnum.ACCOUNT_HAS_DELETED);
        //if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
        //    recordLogininfor(username, LoginTypeEnum.ACCOUNT_HAS_DELETED);
        //    throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
        //}
        this.verifyLogin(username, () -> UserStatus.DISABLE.getCode().equals(user.getStatus()), LoginTypeEnum.THE_USER_IS_DISABLED);
        //if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
        //    recordLogininfor(username, LoginTypeEnum.THE_USER_IS_DISABLED);
        //    throw new ServiceException("对不起,您的账号:" + username + " 已停用");
        //}
        this.verifyLogin(username, () -> !SecurityUtils.matchesPassword(password, user.getPassword()), LoginTypeEnum.USER_NOT_EXIST_OR_PASSWORD_INCORRECT);
        //if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
        //    recordLogininfor(username, LoginTypeEnum.USER_NOT_EXIST_OR_PASSWORD_INCORRECT);
        //    throw new ServiceException("用户不存在/密码错误");
        //}
        recordLogininfor(username, LoginTypeEnum.LOGIN_SUCCESS);
        return userInfo;
    }
    private void verifyLogin(String userName, Supplier<Boolean> supplier, LoginTypeEnum loginTypeEnum) {
        Boolean b = supplier.get();
        if (b) {
            this.recordLogininfor(userName, loginTypeEnum);
            throw new RuntimeException(loginTypeEnum.getDes());
        }
    }
/**
     * 记录登录信息
     *
     * @param username 用户名
     * @param loginTypeEnum 登录类型枚举
     * @return
     */
    public void recordLogininfor(String username, LoginTypeEnum loginTypeEnum)
    {
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setUserName(username);
        logininfor.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
        logininfor.setMsg(loginTypeEnum.getDes());
        logininfor.setLoginType(loginTypeEnum.getCode());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (Objects.nonNull(loginUser)) {
            logininfor.setDeptId(loginUser.getSysUser().getDeptId());
        }
        // 日志状态
        if (this.equalsAny(loginTypeEnum, LoginTypeEnum.LOGIN_SUCCESS, LoginTypeEnum.LOGOUT_SUCCESS, LoginTypeEnum.REGISTER_SUCCESS))
        {
            logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
        } else {
            logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
        }
        remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
    }
// 用户名或密码为空 错误
this.verifyLogin(username, () -> StringUtils.isAnyBlank(username, password), LoginTypeEnum.USER_PASSWORD_MUST_ENTERED);

在这里这种写法:() - > StringUtils.isAnyBlank(username, password)就是使用Lambda表达式,将一个函数作为另一个函数的入参,在使用Lambda表达式的时候需要注意,Lambda表达式只能用来简化仅包含一个抽象方法的接口的创建。(1)只能是接口否则报:Target type of a lambda conversion must be an interface(2)只能是含有一个抽象方法的接口,因此在这个方法中:

private void verifyLogin(String userName, Supplier<Boolean> supplier, LoginTypeEnum loginTypeEnum) {
    Boolean b = supplier.get();
    if (b) {
        this.recordLogininfor(userName, loginTypeEnum);
        throw new RuntimeException(loginTypeEnum.getDes());
    }
}

我们定一个了接口Supplier<Boolean> supplier

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值