|
|
|
@ -31,8 +31,7 @@ import com.ruoyi.system.service.ISysUserService;
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class SysLoginService
|
|
|
|
|
{
|
|
|
|
|
public class SysLoginService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private SysPasswordService passwordService;
|
|
|
|
|
|
|
|
|
@ -48,40 +47,39 @@ public class SysLoginService
|
|
|
|
|
/**
|
|
|
|
|
* 登录
|
|
|
|
|
*/
|
|
|
|
|
public SysUser login(String username, String password)
|
|
|
|
|
{
|
|
|
|
|
public SysUser login(String username, String password, boolean rememberMe) {
|
|
|
|
|
/**
|
|
|
|
|
* 普通登录
|
|
|
|
|
*/
|
|
|
|
|
if (!rememberMe) {
|
|
|
|
|
// 验证码校验
|
|
|
|
|
if (ShiroConstants.CAPTCHA_ERROR.equals(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA)))
|
|
|
|
|
{
|
|
|
|
|
if (ShiroConstants.CAPTCHA_ERROR.equals(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA))) {
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
|
|
|
throw new CaptchaException();
|
|
|
|
|
}
|
|
|
|
|
// 用户名或密码为空 错误
|
|
|
|
|
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(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 (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
|
|
|
|
|| password.length() > UserConstants.PASSWORD_MAX_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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 用户名不在指定范围内 错误
|
|
|
|
|
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
|
|
|
|
|| username.length() > UserConstants.USERNAME_MAX_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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IP黑名单校验
|
|
|
|
|
String blackStr = configService.selectConfigByKey("sys.login.blackIPList");
|
|
|
|
|
if (IpUtils.isMatchedIp(blackStr, ShiroUtils.getIp()))
|
|
|
|
|
{
|
|
|
|
|
if (IpUtils.isMatchedIp(blackStr, ShiroUtils.getIp())) {
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("login.blocked")));
|
|
|
|
|
throw new BlackListException();
|
|
|
|
|
}
|
|
|
|
@ -101,14 +99,12 @@ public class SysLoginService
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
if (user == null) {
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists")));
|
|
|
|
|
throw new UserNotExistsException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
|
|
|
|
|
{
|
|
|
|
|
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.delete")));
|
|
|
|
|
throw new UserDeleteException();
|
|
|
|
|
}
|
|
|
|
@ -116,20 +112,21 @@ public class SysLoginService
|
|
|
|
|
/**
|
|
|
|
|
* 用户审核状态检测
|
|
|
|
|
*/
|
|
|
|
|
if (!ExamineState.ENABLE.getCode().equals(user.getExamine()))
|
|
|
|
|
{
|
|
|
|
|
if (!ExamineState.ENABLE.getCode().equals(user.getExamine())) {
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.examine.disabled")));
|
|
|
|
|
throw new UserDisableException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
|
|
|
|
|
{
|
|
|
|
|
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.blocked", user.getRemark())));
|
|
|
|
|
throw new UserBlockedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 普通登录
|
|
|
|
|
*/
|
|
|
|
|
if (!rememberMe) {
|
|
|
|
|
passwordService.validate(user, password);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
|
|
setRolePermission(user);
|
|
|
|
|
recordLoginInfo(user.getUserId());
|
|
|
|
@ -161,14 +158,11 @@ public class SysLoginService
|
|
|
|
|
*
|
|
|
|
|
* @param user 用户信息
|
|
|
|
|
*/
|
|
|
|
|
public void setRolePermission(SysUser user)
|
|
|
|
|
{
|
|
|
|
|
public void setRolePermission(SysUser user) {
|
|
|
|
|
List<SysRole> roles = user.getRoles();
|
|
|
|
|
if (!roles.isEmpty() && roles.size() > 1)
|
|
|
|
|
{
|
|
|
|
|
if (!roles.isEmpty() && roles.size() > 1) {
|
|
|
|
|
// 多角色设置permissions属性,以便数据权限匹配权限
|
|
|
|
|
for (SysRole role : roles)
|
|
|
|
|
{
|
|
|
|
|
for (SysRole role : roles) {
|
|
|
|
|
Set<String> rolePerms = menuService.selectPermsByRoleId(role.getRoleId());
|
|
|
|
|
role.setPermissions(rolePerms);
|
|
|
|
|
}
|
|
|
|
@ -180,8 +174,7 @@ public class SysLoginService
|
|
|
|
|
*
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
*/
|
|
|
|
|
public void recordLoginInfo(Long userId)
|
|
|
|
|
{
|
|
|
|
|
public void recordLoginInfo(Long userId) {
|
|
|
|
|
SysUser user = new SysUser();
|
|
|
|
|
user.setUserId(userId);
|
|
|
|
|
user.setLoginIp(ShiroUtils.getIp());
|
|
|
|
|