123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package com.ruoyi.framework.shiro.service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.springframework.util.StringUtils;
- import com.ruoyi.common.constant.CommonConstant;
- import com.ruoyi.common.constant.ShiroConstants;
- import com.ruoyi.common.constant.UserConstants;
- import com.ruoyi.common.exception.user.CaptchaException;
- import com.ruoyi.common.exception.user.UserBlockedException;
- import com.ruoyi.common.exception.user.UserNotExistsException;
- import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
- import com.ruoyi.common.utils.MessageUtils;
- import com.ruoyi.common.utils.ServletUtils;
- import com.ruoyi.common.utils.SystemLogUtils;
- import com.ruoyi.project.system.user.domain.User;
- import com.ruoyi.project.system.user.service.IUserService;
- /**
- * 登录校验方法
- *
- * @author ruoyi
- */
- @Component
- public class LoginService
- {
- @Autowired
- private PasswordService passwordService;
- @Autowired
- private IUserService userService;
- /**
- * 登录
- */
- public User login(String username, String password)
- {
- // 验证码校验
- if (!StringUtils.isEmpty(ServletUtils.getStrAttribute(ShiroConstants.CURRENT_CAPTCHA)))
- {
- SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"));
- throw new CaptchaException();
- }
- // 用户名或密码为空 错误
- if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
- {
- SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("not.null"));
- throw new UserNotExistsException();
- }
- // 密码如果不在指定范围内 错误
- if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
- || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
- {
- SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.password.not.match"));
- throw new UserPasswordNotMatchException();
- }
- // 用户名不在指定范围内 错误
- if (username.length() < UserConstants.USERNAME_MIN_LENGTH
- || username.length() > UserConstants.USERNAME_MAX_LENGTH)
- {
- SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.password.not.match"));
- throw new UserPasswordNotMatchException();
- }
- // 查询用户信息
- User user = userService.selectUserByLoginName(username);
- if (user == null && maybeMobilePhoneNumber(username))
- {
- user = userService.selectUserByPhoneNumber(username);
- }
-
- if (user == null && maybeEmail(username))
- {
- user = userService.selectUserByEmail(username);
- }
-
- if (user == null)
- {
- SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.not.exists"));
- throw new UserNotExistsException();
- }
- passwordService.validate(user, password);
- if (UserConstants.USER_BLOCKED == user.getStatus())
- {
- SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.blocked", user.getRefuseDes()));
- throw new UserBlockedException(user.getRefuseDes());
- }
- SystemLogUtils.log(username, CommonConstant.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
- return user;
- }
- private boolean maybeEmail(String username)
- {
- if (!username.matches(UserConstants.EMAIL_PATTERN))
- {
- return false;
- }
- return true;
- }
- private boolean maybeMobilePhoneNumber(String username)
- {
- if (!username.matches(UserConstants.MOBILE_PHONE_NUMBER_PATTERN))
- {
- return false;
- }
- return true;
- }
- }
|