|
|
|
@ -1,6 +1,12 @@
|
|
|
|
|
package com.ruoyi.framework.web.service;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
|
import com.ruoyi.common.enums.UserStatus;
|
|
|
|
|
import com.ruoyi.common.utils.*;
|
|
|
|
|
import com.ruoyi.system.domain.SysLogininfor;
|
|
|
|
|
import com.ruoyi.system.service.ISysLogininforService;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
|
@ -19,9 +25,6 @@ import com.ruoyi.common.exception.user.CaptchaException;
|
|
|
|
|
import com.ruoyi.common.exception.user.CaptchaExpireException;
|
|
|
|
|
import com.ruoyi.common.exception.user.UserNotExistsException;
|
|
|
|
|
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
|
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
|
import com.ruoyi.common.utils.ip.IpUtils;
|
|
|
|
|
import com.ruoyi.framework.manager.AsyncManager;
|
|
|
|
|
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
|
|
|
@ -29,6 +32,9 @@ import com.ruoyi.framework.security.context.AuthenticationContextHolder;
|
|
|
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
|
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录校验方法
|
|
|
|
|
*
|
|
|
|
@ -52,6 +58,15 @@ public class SysLoginService
|
|
|
|
|
@Autowired
|
|
|
|
|
private ISysConfigService configService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private ISysLogininforService logininforService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private SysPermissionService permissionService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private SysPasswordService sysPasswordService;
|
|
|
|
|
/**
|
|
|
|
|
* 登录验证
|
|
|
|
|
*
|
|
|
|
@ -61,7 +76,7 @@ public class SysLoginService
|
|
|
|
|
* @param uuid 唯一标识
|
|
|
|
|
* @return 结果
|
|
|
|
|
*/
|
|
|
|
|
public String login(String username, String password, String code, String uuid)
|
|
|
|
|
public Map<String, Object> login(String username, String password, String code, String uuid,String userType)
|
|
|
|
|
{
|
|
|
|
|
// 验证码校验
|
|
|
|
|
validateCaptcha(username, code, uuid);
|
|
|
|
@ -95,7 +110,30 @@ public class SysLoginService
|
|
|
|
|
}
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
|
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
|
|
recordLoginInfo(loginUser.getUserId());
|
|
|
|
|
// recordLoginInfo(loginUser.getUserId());
|
|
|
|
|
if (StringUtils.isNull(loginUser) || StringUtils.isNull(loginUser.getUserId())) {
|
|
|
|
|
addRecord(username, Constants.LOGIN_FAIL, "登录用户不存在");
|
|
|
|
|
throw new ServiceException("登录用户:" + username + " 不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SysUser user = loginUser.getUser();
|
|
|
|
|
// 判断用户类型
|
|
|
|
|
if (!"admin".equals(username)){
|
|
|
|
|
if (!userType.equals(user.getUserType())){
|
|
|
|
|
throw new ServiceException("该用户类型错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
|
|
|
|
addRecord(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
|
|
|
|
|
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
|
|
|
|
|
}
|
|
|
|
|
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
|
|
addRecord(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
|
|
|
|
|
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
|
|
|
|
|
}
|
|
|
|
|
// sysPasswordService.validate(user);
|
|
|
|
|
addRecord(username, Constants.LOGIN_SUCCESS, "登录成功");
|
|
|
|
|
// 生成token
|
|
|
|
|
return tokenService.createToken(loginUser);
|
|
|
|
|
}
|
|
|
|
@ -137,32 +175,30 @@ public class SysLoginService
|
|
|
|
|
public void loginPreCheck(String username, String password)
|
|
|
|
|
{
|
|
|
|
|
// 用户名或密码为空 错误
|
|
|
|
|
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
|
|
|
|
|
{
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
|
|
|
|
|
throw new UserNotExistsException();
|
|
|
|
|
if (StringUtils.isAnyBlank(username, password)) {
|
|
|
|
|
this.addRecord(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
|
|
|
|
|
throw new ServiceException("用户/密码必须填写");
|
|
|
|
|
}
|
|
|
|
|
// 密码如果不在指定范围内 错误
|
|
|
|
|
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
|
|
|
|
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH)
|
|
|
|
|
{
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
|
|
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
|
|
|
|
this.addRecord(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
|
|
|
|
|
throw new ServiceException("用户密码不在指定范围");
|
|
|
|
|
}
|
|
|
|
|
// 用户名不在指定范围内 错误
|
|
|
|
|
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
|
|
|
|
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
|
|
|
|
|
{
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
|
|
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
|
|
|
|
this.addRecord(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
|
|
|
|
|
throw new ServiceException("用户名不在指定范围");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IP黑名单校验
|
|
|
|
|
String blackStr = configService.selectConfigByKey("sys.login.blackIPList");
|
|
|
|
|
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))
|
|
|
|
|
{
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("login.blocked")));
|
|
|
|
|
throw new BlackListException();
|
|
|
|
|
}
|
|
|
|
|
// String blackStr = configService.selectConfigByKey("sys.login.blackIPList");
|
|
|
|
|
// if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))
|
|
|
|
|
// {
|
|
|
|
|
// AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("login.blocked")));
|
|
|
|
|
// throw new BlackListException();
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -178,4 +214,26 @@ public class SysLoginService
|
|
|
|
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
|
|
|
|
userService.updateUserProfile(sysUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 记录登录信息
|
|
|
|
|
*
|
|
|
|
|
* @param username 用户名
|
|
|
|
|
* @param status 状态
|
|
|
|
|
* @param message 消息内容
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public void addRecord(String username, String status, String message) {
|
|
|
|
|
SysLogininfor logininfor = new SysLogininfor();
|
|
|
|
|
logininfor.setUserName(username);
|
|
|
|
|
logininfor.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
|
|
|
|
logininfor.setMsg(message);
|
|
|
|
|
// 日志状态
|
|
|
|
|
if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
|
|
|
|
|
logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
|
|
|
|
|
} else if (Constants.LOGIN_FAIL.equals(status)) {
|
|
|
|
|
logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
|
|
|
|
|
}
|
|
|
|
|
logininforService.insertLogininfor(logininfor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|