接入登陆日志

This commit is contained in:
YunaiV
2021-01-18 00:23:20 +08:00
parent cbb11986b4
commit 083dac77e1
17 changed files with 236 additions and 52 deletions

View File

@@ -3,16 +3,24 @@ package cn.iocoder.dashboard.modules.system.service.auth.impl;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.dashboard.framework.security.config.SecurityProperties;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
import cn.iocoder.dashboard.modules.system.controller.logger.vo.loginlog.SysLoginLogCreateReqVO;
import cn.iocoder.dashboard.modules.system.convert.auth.SysAuthConvert;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
import cn.iocoder.dashboard.modules.system.dal.redis.dao.auth.SysLoginUserRedisDAO;
import cn.iocoder.dashboard.modules.system.enums.logger.SysLoginLogTypeEnum;
import cn.iocoder.dashboard.modules.system.enums.logger.SysLoginResultEnum;
import cn.iocoder.dashboard.modules.system.service.auth.SysAuthService;
import cn.iocoder.dashboard.modules.system.service.auth.SysTokenService;
import cn.iocoder.dashboard.modules.system.service.common.SysCaptchaService;
import cn.iocoder.dashboard.modules.system.service.logger.SysLoginLogService;
import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
import cn.iocoder.dashboard.modules.system.service.user.SysUserService;
import cn.iocoder.dashboard.util.date.DateUtils;
import cn.iocoder.dashboard.util.servlet.ServletUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
@@ -55,6 +63,10 @@ public class SysAuthServiceImpl implements SysAuthService {
private SysUserService userService;
@Resource
private SysPermissionService permissionService;
@Resource
private SysCaptchaService captchaService;
@Resource
private SysLoginLogService loginLogService;
@Resource
private SysLoginUserRedisDAO loginUserRedisDAO;
@@ -87,7 +99,7 @@ public class SysAuthServiceImpl implements SysAuthService {
@Override
public String login(String username, String password, String captchaUUID, String captchaCode) {
// 判断验证码是否正确
this.verifyCaptcha(captchaUUID, captchaCode);
this.verifyCaptcha(username, captchaUUID, captchaCode);
// 使用账号密码,进行登陆。
LoginUser loginUser = this.login0(username, password);
@@ -102,18 +114,20 @@ public class SysAuthServiceImpl implements SysAuthService {
return tokenService.createToken(sessionId);
}
private void verifyCaptcha(String captchaUUID, String captchaCode) {
// String verifyKey = Constants.CAPTCHA_CODE_KEY + captchaUUID;
// String captcha = redisCache.getCacheObject(verifyKey);
// redisCache.deleteObject(verifyKey);
// if (captcha == null) {
// AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
// throw new CaptchaExpireException();
// }
// if (!code.equalsIgnoreCase(captcha)) {
// AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
// throw new CaptchaException();
// }
private void verifyCaptcha(String username, String captchaUUID, String captchaCode) {
String code = captchaService.getCaptchaCode(captchaUUID);
// 验证码不存在
if (code == null) {
this.createLoginLog(username, SysLoginResultEnum.CAPTCHA_NOT_FOUND);
throw ServiceExceptionUtil.exception(AUTH_LOGIN_CAPTCHA_NOT_FOUND);
}
// 验证码不正确
if (!code.equals(captchaCode)) {
this.createLoginLog(username, SysLoginResultEnum.CAPTCHA_CODE_ERROR);
throw ServiceExceptionUtil.exception(AUTH_LOGIN_CAPTCHA_CODE_ERROR);
}
// 正确,所以要删除下验证码
captchaService.deleteCaptchaCode(captchaUUID);
}
private LoginUser login0(String username, String password) {
@@ -124,22 +138,33 @@ public class SysAuthServiceImpl implements SysAuthService {
// 在其内部,会调用到 loadUserByUsername 方法,获取 User 信息
authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (BadCredentialsException badCredentialsException) {
// TODO 日志优化
// AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
this.createLoginLog(username, SysLoginResultEnum.BAD_CREDENTIALS);
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
} catch (DisabledException disabledException) {
// TODO 日志优化
this.createLoginLog(username, SysLoginResultEnum.USER_DISABLED);
throw exception(AUTH_LOGIN_USER_DISABLED);
} catch (AuthenticationException authenticationException) {
// TODO 日志优化
log.error("[login0][username({}) 发生未知异常]", username, authenticationException);
this.createLoginLog(username, SysLoginResultEnum.UNKNOWN_ERROR);
throw exception(AUTH_LOGIN_FAIL_UNKNOWN);
}
// TODO 需要优化
// AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
// 登陆成功
Assert.notNull(authentication.getPrincipal(), "Principal 不会为空");
this.createLoginLog(username, SysLoginResultEnum.SUCCESS);
return (LoginUser) authentication.getPrincipal();
}
private void createLoginLog(String username, SysLoginResultEnum loginResult) {
SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO();
reqVO.setLogType(SysLoginLogTypeEnum.LOGIN.getType());
reqVO.setTraceId(TracerUtils.getTraceId());
reqVO.setUsername(username);
reqVO.setUserAgent(ServletUtils.getUserAgent());
reqVO.setUserIp(ServletUtils.getClientIP());
reqVO.setResult(loginResult.getResult());
loginLogService.createLoginLog(reqVO);
}
/**
* 获得 User 拥有的角色编号数组
*

View File

@@ -7,6 +7,26 @@ import cn.iocoder.dashboard.modules.system.controller.common.vo.SysCaptchaImageR
*/
public interface SysCaptchaService {
/**
* 获得验证码图片
*
* @return 验证码图片
*/
SysCaptchaImageRespVO getCaptchaImage();
/**
* 获得 uuid 对应的验证码
*
* @param uuid 验证码编号
* @return 验证码
*/
String getCaptchaCode(String uuid);
/**
* 删除 uuid 对应的验证码
*
* @param uuid 验证码编号
*/
void deleteCaptchaCode(String uuid);
}

View File

@@ -35,4 +35,14 @@ public class SysCaptchaServiceImpl implements SysCaptchaService {
return SysCaptchaConvert.INSTANCE.convert(uuid, captcha);
}
@Override
public String getCaptchaCode(String uuid) {
return captchaRedisDAO.get(uuid);
}
@Override
public void deleteCaptchaCode(String uuid) {
captchaRedisDAO.delete(uuid);
}
}

View File

@@ -0,0 +1,17 @@
package cn.iocoder.dashboard.modules.system.service.logger;
import cn.iocoder.dashboard.modules.system.controller.logger.vo.loginlog.SysLoginLogCreateReqVO;
/**
* 登陆日志 Service 接口
*/
public interface SysLoginLogService {
/**
* 创建登陆日志
*
* @param reqVO 日志信息
*/
void createLoginLog(SysLoginLogCreateReqVO reqVO);
}

View File

@@ -0,0 +1,27 @@
package cn.iocoder.dashboard.modules.system.service.logger.impl;
import cn.iocoder.dashboard.modules.system.controller.logger.vo.loginlog.SysLoginLogCreateReqVO;
import cn.iocoder.dashboard.modules.system.convert.logger.SysLoginLogConvert;
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.logger.SysLoginLogMapper;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.logger.SysLoginLogDO;
import cn.iocoder.dashboard.modules.system.service.logger.SysLoginLogService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* 登陆日志 Service 实现
*/
@Service
public class SysLoginLogServiceImpl implements SysLoginLogService {
@Resource
private SysLoginLogMapper loginLogMapper;
@Override
public void createLoginLog(SysLoginLogCreateReqVO reqVO) {
SysLoginLogDO loginLog = SysLoginLogConvert.INSTANCE.convert(reqVO);
loginLogMapper.insert(loginLog);
}
}