mirror of https://gitee.com/stylefeng/roses
【8.1.9】【log】更新记录登录失败的日志的接口
parent
ace49e5272
commit
f56f10ff69
|
@ -23,6 +23,7 @@ import cn.stylefeng.roses.kernel.auth.api.pojo.payload.DefaultJwtPayload;
|
|||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.demo.expander.DemoConfigExpander;
|
||||
import cn.stylefeng.roses.kernel.log.api.LoginLogServiceApi;
|
||||
import cn.stylefeng.roses.kernel.log.api.schedule.AsyncLogManager;
|
||||
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.exception.ScannerException;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.exception.enums.ScannerExceptionEnum;
|
||||
|
@ -152,7 +153,7 @@ public class LoginService {
|
|||
sysUserServiceApi.updateUserLoginInfo(loginUser.getUserId(), ip);
|
||||
|
||||
// 13.登录成功日志
|
||||
loginLogServiceApi.loginSuccess(loginUser.getUserId());
|
||||
loginLogServiceApi.loginSuccess(loginUser.getUserId(), loginUser.getAccount());
|
||||
}
|
||||
|
||||
// 13.1 登录成功,清空用户的错误登录次数
|
||||
|
@ -327,6 +328,9 @@ public class LoginService {
|
|||
// 演示环境,不记录error次数
|
||||
if (!DemoConfigExpander.getDemoEnvFlag()) {
|
||||
loginErrorCountCacheApi.put(loginRequest.getAccount(), loginErrorCount + 1);
|
||||
|
||||
// 记录登录失败日志
|
||||
AsyncLogManager.getInstance().recordLoginLogFail(loginRequest.getAccount());
|
||||
}
|
||||
|
||||
throw new AuthException(AuthExceptionEnum.USERNAME_PASSWORD_ERROR);
|
||||
|
|
|
@ -51,17 +51,15 @@ public interface LoginLogServiceApi {
|
|||
* @author chenjinlong
|
||||
* @since 2021/1/13 11:36
|
||||
*/
|
||||
void loginSuccess(Long userId);
|
||||
void loginSuccess(Long userId, String account);
|
||||
|
||||
/**
|
||||
* 增加登录失败日志
|
||||
* 记录登录失败的日志
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param llgMessage 错误信息
|
||||
* @author chenjinlong
|
||||
* @since 2021/1/13 11:36
|
||||
* @author fengshuonan
|
||||
* @since 2024/7/10 17:30
|
||||
*/
|
||||
void loginFail(Long userId, String llgMessage);
|
||||
void loginFail(String account);
|
||||
|
||||
/**
|
||||
* 增加退出成功日志
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package cn.stylefeng.roses.kernel.log.api.schedule;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.stylefeng.roses.kernel.log.api.LoginLogServiceApi;
|
||||
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 日志执行器,为了解决日志记录的异步问题
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/7/10 17:25
|
||||
*/
|
||||
public class AsyncLogManager {
|
||||
|
||||
// 异步操作记录日志的线程池
|
||||
private final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
|
||||
|
||||
private AsyncLogManager() {
|
||||
}
|
||||
|
||||
public static AsyncLogManager logManager = new AsyncLogManager();
|
||||
|
||||
public static AsyncLogManager getInstance() {
|
||||
return logManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行日志记录任务
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/7/10 17:27
|
||||
*/
|
||||
public void executeLog(TimerTask task) {
|
||||
// 日志记录操作延时
|
||||
int delayTime = 10;
|
||||
executor.schedule(task, delayTime, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步记录登录失败的日志
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/7/10 17:29
|
||||
*/
|
||||
public void recordLoginLogFail(String account) {
|
||||
LoginLogServiceApi loginLogServiceApi = SpringUtil.getBean(LoginLogServiceApi.class);
|
||||
loginLogServiceApi.loginFail(account);
|
||||
}
|
||||
|
||||
}
|
|
@ -89,10 +89,11 @@ public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLo
|
|||
}
|
||||
|
||||
@Override
|
||||
public void loginSuccess(Long userId) {
|
||||
public void loginSuccess(Long userId, String account) {
|
||||
SysLoginLog sysLoginLog = new SysLoginLog();
|
||||
sysLoginLog.setLlgName(LoginLogConstant.LOGIN_IN_LOGINNAME);
|
||||
sysLoginLog.setUserId(userId);
|
||||
sysLoginLog.setAccount(account);
|
||||
sysLoginLog.setLlgIpAddress(HttpServletUtil.getRequestClientIp(HttpServletUtil.getRequest()));
|
||||
sysLoginLog.setLlgSucceed(LoginLogConstant.OPERATION_SUCCESS);
|
||||
sysLoginLog.setLlgMessage(LoginLogConstant.LOGIN_IN_SUCCESS_MESSAGE);
|
||||
|
@ -100,13 +101,13 @@ public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLo
|
|||
}
|
||||
|
||||
@Override
|
||||
public void loginFail(Long userId, String llgMessage) {
|
||||
public void loginFail(String account) {
|
||||
SysLoginLog sysLoginLog = new SysLoginLog();
|
||||
sysLoginLog.setLlgName(LoginLogConstant.LOGIN_IN_LOGINNAME);
|
||||
sysLoginLog.setUserId(userId);
|
||||
sysLoginLog.setLlgIpAddress(HttpServletUtil.getRequestClientIp(HttpServletUtil.getRequest()));
|
||||
sysLoginLog.setLlgSucceed(LoginLogConstant.OPERATION_FAIL);
|
||||
sysLoginLog.setLlgMessage(llgMessage);
|
||||
sysLoginLog.setLlgMessage(LoginLogConstant.LOGIN_IN_SUCCESS_FAIL);
|
||||
sysLoginLog.setLlgIpAddress(HttpServletUtil.getRequestClientIp(HttpServletUtil.getRequest()));
|
||||
sysLoginLog.setAccount(account);
|
||||
this.save(sysLoginLog);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue