2021-09-17 14:48:36 +08:00
|
|
|
package com.ruoyi.system.service;
|
2020-02-13 10:48:51 +08:00
|
|
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
2021-05-25 10:00:36 +08:00
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
2020-07-20 10:41:32 +08:00
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
2021-09-17 14:48:36 +08:00
|
|
|
import com.ruoyi.common.core.service.LogininforService;
|
|
|
|
import com.ruoyi.common.core.service.TokenService;
|
2021-08-17 10:38:07 +08:00
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
2020-02-13 10:48:51 +08:00
|
|
|
import com.ruoyi.common.exception.user.CaptchaException;
|
|
|
|
import com.ruoyi.common.exception.user.CaptchaExpireException;
|
|
|
|
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
2021-05-25 10:00:36 +08:00
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
2020-02-13 10:48:51 +08:00
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
2021-09-07 13:20:24 +08:00
|
|
|
import com.ruoyi.common.utils.RedisUtils;
|
2021-05-25 10:00:36 +08:00
|
|
|
import com.ruoyi.common.utils.ServletUtils;
|
2021-05-25 13:40:18 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
2021-06-12 20:14:50 +08:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2020-02-13 10:48:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 登录校验方法
|
2021-09-26 10:05:29 +08:00
|
|
|
*
|
2020-02-13 10:48:51 +08:00
|
|
|
* @author ruoyi
|
|
|
|
*/
|
|
|
|
@Component
|
|
|
|
public class SysLoginService
|
|
|
|
{
|
|
|
|
@Autowired
|
|
|
|
private TokenService tokenService;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
private AuthenticationManager authenticationManager;
|
|
|
|
|
2021-05-25 11:03:24 +08:00
|
|
|
@Autowired
|
2021-05-25 10:00:36 +08:00
|
|
|
private ISysUserService userService;
|
|
|
|
|
2021-07-13 13:34:10 +08:00
|
|
|
@Autowired
|
|
|
|
private ISysConfigService configService;
|
|
|
|
|
2021-06-12 20:14:50 +08:00
|
|
|
@Autowired
|
2021-09-17 14:48:36 +08:00
|
|
|
private LogininforService asyncService;
|
2021-06-12 20:14:50 +08:00
|
|
|
|
2020-02-13 10:48:51 +08:00
|
|
|
/**
|
|
|
|
* 登录验证
|
2021-09-26 10:05:29 +08:00
|
|
|
*
|
2020-02-13 10:48:51 +08:00
|
|
|
* @param username 用户名
|
|
|
|
* @param password 密码
|
2020-07-17 16:43:47 +08:00
|
|
|
* @param code 验证码
|
2020-02-13 10:48:51 +08:00
|
|
|
* @param uuid 唯一标识
|
|
|
|
* @return 结果
|
|
|
|
*/
|
|
|
|
public String login(String username, String password, String code, String uuid)
|
|
|
|
{
|
2021-07-13 13:38:53 +08:00
|
|
|
HttpServletRequest request = ServletUtils.getRequest();
|
|
|
|
boolean captchaOnOff = configService.selectCaptchaOnOff();
|
2021-07-13 13:34:10 +08:00
|
|
|
// 验证码开关
|
|
|
|
if (captchaOnOff)
|
|
|
|
{
|
2021-07-24 19:03:36 +08:00
|
|
|
validateCaptcha(username, code, uuid, request);
|
2021-07-13 13:34:10 +08:00
|
|
|
}
|
2020-02-13 10:48:51 +08:00
|
|
|
// 用户验证
|
|
|
|
Authentication authentication = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
|
authentication = authenticationManager
|
|
|
|
.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
if (e instanceof BadCredentialsException)
|
|
|
|
{
|
2021-06-12 20:14:50 +08:00
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"), request);
|
2020-02-13 10:48:51 +08:00
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-26 10:05:29 +08:00
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
2021-08-17 10:38:07 +08:00
|
|
|
throw new ServiceException(e.getMessage());
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-12 20:14:50 +08:00
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
2020-02-13 10:48:51 +08:00
|
|
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
2021-09-26 10:05:29 +08:00
|
|
|
recordLoginInfo(loginUser.getUserId());
|
2020-02-13 10:48:51 +08:00
|
|
|
// 生成token
|
|
|
|
return tokenService.createToken(loginUser);
|
|
|
|
}
|
2021-05-25 10:00:36 +08:00
|
|
|
|
2021-07-13 13:34:10 +08:00
|
|
|
/**
|
|
|
|
* 校验验证码
|
2021-09-26 10:05:29 +08:00
|
|
|
*
|
2021-07-13 13:34:10 +08:00
|
|
|
* @param username 用户名
|
|
|
|
* @param code 验证码
|
|
|
|
* @param uuid 唯一标识
|
|
|
|
* @return 结果
|
|
|
|
*/
|
2021-07-24 19:03:36 +08:00
|
|
|
public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
|
2021-07-13 13:34:10 +08:00
|
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
2021-09-07 13:20:24 +08:00
|
|
|
String captcha = RedisUtils.getCacheObject(verifyKey);
|
|
|
|
RedisUtils.deleteObject(verifyKey);
|
2021-07-13 13:34:10 +08:00
|
|
|
if (captcha == null) {
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
|
|
|
|
throw new CaptchaExpireException();
|
|
|
|
}
|
|
|
|
if (!code.equalsIgnoreCase(captcha)) {
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
|
|
|
|
throw new CaptchaException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 10:00:36 +08:00
|
|
|
/**
|
|
|
|
* 记录登录信息
|
2021-09-26 10:05:29 +08:00
|
|
|
*
|
|
|
|
* @param userId 用户ID
|
2021-05-25 10:00:36 +08:00
|
|
|
*/
|
2021-09-26 10:05:29 +08:00
|
|
|
public void recordLoginInfo(Long userId)
|
2021-05-25 10:00:36 +08:00
|
|
|
{
|
2021-09-26 10:05:29 +08:00
|
|
|
SysUser sysUser = new SysUser();
|
|
|
|
sysUser.setUserId(userId);
|
|
|
|
sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
|
|
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
|
|
|
userService.updateUserProfile(sysUser);
|
2021-05-25 10:00:36 +08:00
|
|
|
}
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|