| | |
| | | import org.springframework.security.core.Authentication;
|
| | | import org.springframework.stereotype.Component;
|
| | | import com.ruoyi.common.constant.Constants;
|
| | | import com.ruoyi.common.exception.CustomException;
|
| | | import com.ruoyi.common.exception.ServiceException;
|
| | | import com.ruoyi.common.exception.user.CaptchaException;
|
| | | import com.ruoyi.common.exception.user.CaptchaExpireException;
|
| | | import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
| | | import com.ruoyi.common.utils.DateUtils;
|
| | | import com.ruoyi.common.utils.MessageUtils;
|
| | | import com.ruoyi.common.utils.ServletUtils;
|
| | | 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;
|
| | |
| | | // 验证码开关
|
| | | if (captchaOnOff)
|
| | | {
|
| | | validateCapcha(username, code, uuid);
|
| | | validateCaptcha(username, code, uuid);
|
| | | }
|
| | | // 用户验证
|
| | | Authentication authentication = null;
|
| | |
| | | else
|
| | | {
|
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
| | | throw new CustomException(e.getMessage());
|
| | | throw new ServiceException(e.getMessage());
|
| | | }
|
| | | }
|
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
| | | LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
| | | recordLoginInfo(loginUser.getUser());
|
| | | recordLoginInfo(loginUser.getUserId());
|
| | | // 生成token
|
| | | return tokenService.createToken(loginUser);
|
| | | }
|
| | |
| | | * @param uuid 唯一标识
|
| | | * @return 结果
|
| | | */
|
| | | public void validateCapcha(String username, String code, String uuid)
|
| | | public void validateCaptcha(String username, String code, String uuid)
|
| | | {
|
| | | String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
| | | String verifyKey = Constants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
|
| | | String captcha = redisCache.getCacheObject(verifyKey);
|
| | | redisCache.deleteObject(verifyKey);
|
| | | if (captcha == null)
|
| | |
| | |
|
| | | /**
|
| | | * 记录登录信息
|
| | | *
|
| | | * @param userId 用户ID
|
| | | */
|
| | | public void recordLoginInfo(SysUser user)
|
| | | public void recordLoginInfo(Long userId)
|
| | | {
|
| | | user.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
| | | user.setLoginDate(DateUtils.getNowDate());
|
| | | userService.updateUserProfile(user);
|
| | | SysUser sysUser = new SysUser();
|
| | | sysUser.setUserId(userId);
|
| | | sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
| | | sysUser.setLoginDate(DateUtils.getNowDate());
|
| | | userService.updateUserProfile(sysUser);
|
| | | }
|
| | | }
|