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