refactor: 修改 framework 逻辑.

This commit is contained in:
lijiahang
2023-12-29 14:13:32 +08:00
parent a1e372148a
commit e580fcee87
16 changed files with 416 additions and 180 deletions

View File

@@ -3,7 +3,7 @@ package com.orion.ops.framework.common.entity;
import java.io.Serializable;
/**
* 请求留痕
* 请求留痕信息
*
* @author Jiahang Li
* @version 1.0.0
@@ -11,6 +11,27 @@ import java.io.Serializable;
*/
public interface RequestIdentity extends Serializable {
/**
* 获取请求地址
*
* @return address
*/
String getAddress();
/**
* 获取请求位置
*
* @return location
*/
String getLocation();
/**
* 获取请求 userAgent
*
* @return userAgent
*/
String getUserAgent();
/**
* 设置请求地址
*

View File

@@ -0,0 +1,30 @@
package com.orion.ops.framework.common.entity;
import lombok.Data;
/**
* 请求留痕模型
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/12/29 11:57
*/
@Data
public class RequestIdentityModel implements RequestIdentity {
/**
* 请求地址
*/
private String address;
/**
* 请求位置
*/
private String location;
/**
* userAgent
*/
private String userAgent;
}

View File

@@ -1,5 +1,6 @@
package com.orion.ops.framework.common.utils;
import com.orion.lang.utils.Exceptions;
import com.orion.ops.framework.common.crypto.ValueCrypto;
/**
@@ -141,6 +142,10 @@ public class CryptoUtils {
}
public static void setDelegate(ValueCrypto delegate) {
if (CryptoUtils.delegate != null) {
// unmodified
throw Exceptions.state();
}
CryptoUtils.delegate = delegate;
}

View File

@@ -1,5 +1,6 @@
package com.orion.ops.framework.common.utils;
import com.orion.lang.utils.Exceptions;
import com.orion.ops.framework.common.file.FileClient;
import java.io.InputStream;
@@ -130,6 +131,10 @@ public class FileClientUtils {
}
public static void setDelegate(FileClient delegate) {
if (FileClientUtils.delegate != null) {
// unmodified
throw Exceptions.state();
}
FileClientUtils.delegate = delegate;
}

View File

@@ -6,6 +6,7 @@ import com.orion.ops.framework.biz.operator.log.core.aspect.OperatorLogAspect;
import com.orion.ops.framework.biz.operator.log.core.config.OperatorLogConfig;
import com.orion.ops.framework.biz.operator.log.core.service.OperatorLogFrameworkService;
import com.orion.ops.framework.biz.operator.log.core.service.OperatorLogFrameworkServiceDelegate;
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogFiller;
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogs;
import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
import com.orion.ops.framework.common.json.filter.FieldDesensitizeFilter;
@@ -67,8 +68,10 @@ public class OrionOperatorLogAutoConfiguration {
// 脱敏字段注解过滤器
desensitizeValueFilter
};
// 设置过滤器到工具类中
// 设置参数到工具类中
OperatorLogs.setSerializeFilters(serializeFilters);
OperatorLogFiller.setSerializeFilters(serializeFilters);
OperatorLogFiller.setOperatorLogConfig(operatorLogConfig);
return new OperatorLogAspect(operatorLogConfig, service, serializeFilters);
}

View File

@@ -1,26 +1,20 @@
package com.orion.ops.framework.biz.operator.log.core.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.orion.lang.define.thread.ExecutorBuilder;
import com.orion.lang.utils.Arrays1;
import com.orion.lang.utils.Refs;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.json.matcher.ReplacementFormatters;
import com.orion.ops.framework.biz.operator.log.core.annotation.IgnoreParameter;
import com.orion.ops.framework.biz.operator.log.core.annotation.OperatorLog;
import com.orion.ops.framework.biz.operator.log.core.config.OperatorLogConfig;
import com.orion.ops.framework.biz.operator.log.core.enums.ReturnType;
import com.orion.ops.framework.biz.operator.log.core.factory.OperatorTypeHolder;
import com.orion.ops.framework.biz.operator.log.core.model.OperatorLogModel;
import com.orion.ops.framework.biz.operator.log.core.model.OperatorType;
import com.orion.ops.framework.biz.operator.log.core.service.OperatorLogFrameworkService;
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogFiller;
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogs;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.framework.common.meta.TraceIdHolder;
import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.framework.common.security.SecurityHolder;
import com.orion.ops.framework.common.utils.Requests;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
@@ -35,7 +29,9 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
/**
@@ -178,24 +174,26 @@ public class OperatorLogAspect {
if (user == null) {
return;
}
// 获取请求信息
Map<String, Object> extra = OperatorLogs.get();
if (!OperatorLogs.isSave(extra)) {
// 检查是否保存
if (!OperatorLogs.isSave()) {
return;
}
OperatorLogModel model = new OperatorLogModel();
// 填充使用时间
this.fillUseTime(model, start);
// 填充用户信息
this.fillUserInfo(model, user);
// 填充请求信息
this.fillRequest(model);
// 填充结果信息
this.fillResult(model, o, ret, exception);
// 填充拓展信息
this.fillExtra(model, extra);
// 填充日志
this.fillLogInfo(model, extra, type);
// 填充请求
Map<String, Object> extra = OperatorLogs.get();
OperatorLogModel model = OperatorLogFiller.create()
// 填充使用时间
.fillUsedTime(start)
// 填充用户信息
.fillUserInfo(user)
// 填充请求信息
.fillRequest()
// 填充结果信息
.fillResult(o.ret(), ret, exception)
// 填充拓展信息
.fillExtra(extra)
// 填充日志
.fillLogInfo(extra, type)
.get();
// 插入日志
this.asyncSaveLog(model);
} catch (Exception e) {
@@ -217,96 +215,6 @@ public class OperatorLogAspect {
return securityHolder.getLoginUser();
}
/**
* 填充使用时间
*
* @param model model
* @param start start
*/
private void fillUseTime(OperatorLogModel model, long start) {
long end = System.currentTimeMillis();
model.setDuration((int) (end - start));
model.setStartTime(new Date(start));
model.setEndTime(new Date(end));
}
/**
* 填充用户信息
*
* @param model model
* @param user user
*/
private void fillUserInfo(OperatorLogModel model, LoginUser user) {
model.setUserId(user.getId());
model.setUsername(user.getUsername());
}
/**
* 填充请求留痕信息
*
* @param model model
*/
private void fillRequest(OperatorLogModel model) {
model.setTraceId(TraceIdHolder.get());
// 填充请求信息
Requests.fillIdentity(model);
Optional.ofNullable(model.getUserAgent())
.map(s -> Strings.retain(s, operatorLogConfig.getUserAgentLength()))
.ifPresent(model::setUserAgent);
}
/**
* 填充结果
*
* @param model model
* @param exception exception
*/
private void fillResult(OperatorLogModel model, OperatorLog o, Object ret, Throwable exception) {
if (exception == null) {
model.setResult(BooleanBit.TRUE.getValue());
ReturnType retType = o.ret();
if (ret != null) {
if (ReturnType.JSON.equals(retType)) {
// 脱敏
model.setReturnValue(JSON.toJSONString(ret, serializeFilters));
} else if (ReturnType.TO_STRING.equals(retType)) {
model.setReturnValue(Refs.json(Objects.toString(ret)));
}
}
} else {
model.setResult(BooleanBit.FALSE.getValue());
// 错误信息
String errorMessage = Strings.retain(exception.getMessage(), operatorLogConfig.getErrorMessageLength());
model.setErrorMessage(errorMessage);
}
}
/**
* 填充拓展信息
*
* @param model model
* @param extra extra
*/
private void fillExtra(OperatorLogModel model, Map<String, Object> extra) {
if (extra != null) {
model.setExtra(JSON.toJSONString(extra));
}
}
/**
* 填充日志信息
*
* @param model model
* @param extra extra
* @param type type
*/
private void fillLogInfo(OperatorLogModel model, Map<String, Object> extra, OperatorType type) {
model.setRiskLevel(type.getRiskLevel().name());
model.setModule(type.getModule());
model.setType(type.getType());
model.setLogInfo(ReplacementFormatters.format(type.getTemplate(), extra));
}
/**
* 异步保存日志
*

View File

@@ -0,0 +1,248 @@
package com.orion.ops.framework.biz.operator.log.core.uitls;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.orion.lang.able.Gettable;
import com.orion.lang.utils.Exceptions;
import com.orion.lang.utils.Refs;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.json.matcher.ReplacementFormatters;
import com.orion.ops.framework.biz.operator.log.core.config.OperatorLogConfig;
import com.orion.ops.framework.biz.operator.log.core.enums.ReturnType;
import com.orion.ops.framework.biz.operator.log.core.model.OperatorLogModel;
import com.orion.ops.framework.biz.operator.log.core.model.OperatorType;
import com.orion.ops.framework.common.entity.RequestIdentity;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.framework.common.meta.TraceIdHolder;
import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.framework.common.utils.Requests;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
* 操作日志填充器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/12/29 11:01
*/
public class OperatorLogFiller implements Gettable<OperatorLogModel> {
private static SerializeFilter[] serializeFilters;
private static OperatorLogConfig operatorLogConfig;
private final OperatorLogModel model;
public OperatorLogFiller(OperatorLogModel model) {
this.model = model;
}
/**
* 创建填充器
*
* @return filler
*/
public static OperatorLogFiller create() {
return new OperatorLogFiller(new OperatorLogModel());
}
/**
* 创建填充器
*
* @param model model
* @return filler
*/
public static OperatorLogFiller create(OperatorLogModel model) {
return new OperatorLogFiller(model);
}
/**
* 填充使用时间
*
* @param start start
* @return this
*/
public OperatorLogFiller fillUsedTime(long start) {
long end = System.currentTimeMillis();
model.setDuration((int) (end - start));
model.setStartTime(new Date(start));
model.setEndTime(new Date(end));
return this;
}
/**
* 填充用户信息
*
* @param userId userId
* @param username username
* @return this
*/
public OperatorLogFiller fillUserInfo(Long userId, String username) {
model.setUserId(userId);
model.setUsername(username);
return this;
}
/**
* 填充用户信息
*
* @param user user
* @return this
*/
public OperatorLogFiller fillUserInfo(LoginUser user) {
model.setUserId(user.getId());
model.setUsername(user.getUsername());
return this;
}
/**
* 填充 traceId
*
* @param traceId traceId
* @return this
*/
public OperatorLogFiller fillTraceId(String traceId) {
model.setTraceId(traceId);
return this;
}
/**
* 填充请求留痕信息
*
* @param identity identity
* @return this
*/
public OperatorLogFiller fillIdentity(RequestIdentity identity) {
model.setAddress(identity.getAddress());
model.setLocation(identity.getLocation());
model.setUserAgent(identity.getUserAgent());
// 填充请求信息
Optional.ofNullable(model.getUserAgent())
.map(s -> Strings.retain(s, operatorLogConfig.getUserAgentLength()))
.ifPresent(model::setUserAgent);
return this;
}
/**
* 填充请求留痕信息
*
* @return this
*/
public OperatorLogFiller fillRequest() {
model.setTraceId(TraceIdHolder.get());
// 填充请求信息
Requests.fillIdentity(model);
return this.fillIdentity(model);
}
/**
* 填充结果
*
* @param ret ret
* @return this
*/
public OperatorLogFiller fillResult(Object ret) {
return this.fillResult(ReturnType.JSON, ret, null);
}
/**
* 填充结果
*
* @param exception exception
* @return this
*/
public OperatorLogFiller fillException(Throwable exception) {
return this.fillResult(null, null, exception);
}
/**
* 填充结果
*
* @param retType retType
* @param ret ret
* @param exception exception
* @return this
*/
public OperatorLogFiller fillResult(ReturnType retType, Object ret, Throwable exception) {
if (exception == null) {
model.setResult(BooleanBit.TRUE.getValue());
if (ret != null) {
if (ReturnType.JSON.equals(retType)) {
// 脱敏
String returnValue = JSON.toJSONString(ret, serializeFilters);
if (JSON.isValidObject(returnValue)) {
// json object 对象
model.setReturnValue(Refs.json(JSON.parseObject(returnValue)));
} else if (JSON.isValidArray(returnValue)) {
// json array
model.setReturnValue(Refs.json(JSON.parseArray(returnValue)));
} else {
// 普通文本
model.setReturnValue(Refs.json(returnValue));
}
} else if (ReturnType.TO_STRING.equals(retType)) {
// 文本
model.setReturnValue(Refs.json(Objects.toString(ret)));
}
}
} else {
model.setResult(BooleanBit.FALSE.getValue());
// 错误信息
String errorMessage = Strings.retain(exception.getMessage(), operatorLogConfig.getErrorMessageLength());
model.setErrorMessage(errorMessage);
}
return this;
}
/**
* 填充拓展信息
*
* @param extra extra
* @return this
*/
public OperatorLogFiller fillExtra(Map<String, Object> extra) {
model.setExtra(JSON.toJSONString(extra));
return this;
}
/**
* 填充日志信息
*
* @param extra extra
* @param type type
* @return this
*/
public OperatorLogFiller fillLogInfo(Map<String, Object> extra, OperatorType type) {
model.setRiskLevel(type.getRiskLevel().name());
model.setModule(type.getModule());
model.setType(type.getType());
model.setLogInfo(ReplacementFormatters.format(type.getTemplate(), extra));
return this;
}
@Override
public OperatorLogModel get() {
return model;
}
public static void setSerializeFilters(SerializeFilter[] serializeFilters) {
if (OperatorLogFiller.serializeFilters != null) {
// unmodified
throw Exceptions.state();
}
OperatorLogFiller.serializeFilters = serializeFilters;
}
public static void setOperatorLogConfig(OperatorLogConfig operatorLogConfig) {
if (OperatorLogFiller.operatorLogConfig != null) {
// unmodified
throw Exceptions.state();
}
OperatorLogFiller.operatorLogConfig = operatorLogConfig;
}
}

View File

@@ -2,10 +2,11 @@ package com.orion.ops.framework.biz.operator.log.core.uitls;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.orion.lang.utils.Exceptions;
import com.orion.lang.utils.collect.Maps;
import com.orion.ops.framework.common.constant.ExtraFieldConst;
import com.orion.ops.framework.common.security.LoginUser;
import java.util.HashMap;
import java.util.Map;
/**
@@ -24,7 +25,7 @@ public class OperatorLogs implements ExtraFieldConst {
/**
* 拓展信息
*/
private static final ThreadLocal<Map<String, Object>> EXTRA_HOLDER = new ThreadLocal<>();
private static final ThreadLocal<Map<String, Object>> EXTRA_HOLDER = ThreadLocal.withInitial(Maps::newMap);
/**
* 当前用户 优先于登录用户
@@ -41,7 +42,7 @@ public class OperatorLogs implements ExtraFieldConst {
* @param value value
*/
public static void add(String key, Object value) {
initMap().put(key, value);
EXTRA_HOLDER.get().put(key, value);
}
/**
@@ -51,7 +52,7 @@ public class OperatorLogs implements ExtraFieldConst {
* @param value value
*/
public static void addJson(String key, Object value) {
initMap().put(key, JSON.parseObject(JSON.toJSONString(value, serializeFilters)));
EXTRA_HOLDER.get().put(key, JSON.parseObject(JSON.toJSONString(value, serializeFilters)));
}
/**
@@ -60,7 +61,7 @@ public class OperatorLogs implements ExtraFieldConst {
* @param map map
*/
public static void add(Map<String, ?> map) {
initMap().putAll(map);
EXTRA_HOLDER.get().putAll(map);
}
/**
@@ -74,10 +75,10 @@ public class OperatorLogs implements ExtraFieldConst {
return;
}
if (obj instanceof Map) {
add((Map<String, ?>) obj);
return;
EXTRA_HOLDER.get().putAll((Map<String, ?>) obj);
} else {
EXTRA_HOLDER.get().putAll(JSON.parseObject(JSON.toJSONString(obj, serializeFilters)));
}
initMap().putAll(JSON.parseObject(JSON.toJSONString(obj, serializeFilters)));
}
/**
@@ -93,11 +94,22 @@ public class OperatorLogs implements ExtraFieldConst {
* @param save save
*/
public static void setSave(boolean save) {
if (!save) {
initMap().put(UN_SAVE_FLAG, UN_SAVE_FLAG);
if (save) {
EXTRA_HOLDER.get().remove(UN_SAVE_FLAG);
} else {
EXTRA_HOLDER.get().put(UN_SAVE_FLAG, UN_SAVE_FLAG);
}
}
/**
* 设置是否保存
*
* @return save
*/
public static boolean isSave() {
return !UN_SAVE_FLAG.equals(EXTRA_HOLDER.get().get(UN_SAVE_FLAG));
}
/**
* 获取参数
*
@@ -107,24 +119,6 @@ public class OperatorLogs implements ExtraFieldConst {
return EXTRA_HOLDER.get();
}
/**
* 清空
*/
public static void clear() {
EXTRA_HOLDER.remove();
USER_HOLDER.remove();
}
/**
* 设置是否保存
*
* @param map map
* @return save
*/
public static boolean isSave(Map<String, Object> map) {
return map == null || !map.containsKey(UN_SAVE_FLAG);
}
/**
* 设置用户信息
*
@@ -144,20 +138,18 @@ public class OperatorLogs implements ExtraFieldConst {
}
/**
* 初始化
*
* @return map
* 清空
*/
private static Map<String, Object> initMap() {
Map<String, Object> map = EXTRA_HOLDER.get();
if (map == null) {
map = new HashMap<>();
EXTRA_HOLDER.set(map);
}
return map;
public static void clear() {
EXTRA_HOLDER.remove();
USER_HOLDER.remove();
}
public static void setSerializeFilters(SerializeFilter[] serializeFilters) {
if (OperatorLogs.serializeFilters != null) {
// unmodified
throw Exceptions.state();
}
OperatorLogs.serializeFilters = serializeFilters;
}

View File

@@ -1,5 +1,6 @@
package com.orion.ops.framework.mybatis.core.utils;
import com.orion.lang.utils.Exceptions;
import com.orion.ops.framework.common.security.SecurityHolder;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
@@ -69,6 +70,10 @@ public class DomainFillUtils {
}
public static void setSecurityHolder(SecurityHolder securityHolder) {
if (DomainFillUtils.securityHolder != null) {
// unmodified
throw Exceptions.state();
}
DomainFillUtils.securityHolder = securityHolder;
}

View File

@@ -2,6 +2,7 @@ package com.orion.ops.framework.redis.core.utils;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.utils.Arrays1;
import com.orion.lang.utils.Exceptions;
import com.orion.lang.utils.io.Streams;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
@@ -165,6 +166,10 @@ public class RedisUtils {
}
public static void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
if (RedisUtils.redisTemplate != null) {
// unmodified
throw Exceptions.state();
}
RedisUtils.redisTemplate = redisTemplate;
}

View File

@@ -18,7 +18,7 @@ public abstract class CryptoProcessor<Config extends CryptoConfig> implements Va
this.config = config;
// 设置为默认加密器
if (config.isPrimary()) {
PrimaryValueCrypto.delegate = this;
PrimaryValueCrypto.setDelegate(this);
CryptoUtils.setDelegate(this);
}
}

View File

@@ -1,5 +1,6 @@
package com.orion.ops.framework.security.core.crypto;
import com.orion.lang.utils.Exceptions;
import com.orion.ops.framework.common.crypto.ValueCrypto;
/**
@@ -11,7 +12,7 @@ import com.orion.ops.framework.common.crypto.ValueCrypto;
*/
public class PrimaryValueCrypto implements ValueCrypto {
protected static ValueCrypto delegate;
private static ValueCrypto delegate;
@Override
public void init() {
@@ -27,4 +28,12 @@ public class PrimaryValueCrypto implements ValueCrypto {
return delegate.decrypt(text);
}
protected static void setDelegate(ValueCrypto delegate) {
if (PrimaryValueCrypto.delegate != null) {
// unmodified
throw Exceptions.state();
}
PrimaryValueCrypto.delegate = delegate;
}
}

View File

@@ -25,7 +25,7 @@ public abstract class AbstractFileClient<Config extends FileClientConfig> implem
this.config = config;
// 设置默认文件客户端
if (config.isPrimary()) {
PrimaryFileClient.delegate = this;
PrimaryFileClient.setDelegate(this);
FileClientUtils.setDelegate(this);
}
}

View File

@@ -1,5 +1,6 @@
package com.orion.ops.framework.storage.core.client;
import com.orion.lang.utils.Exceptions;
import com.orion.ops.framework.common.file.FileClient;
import java.io.InputStream;
@@ -13,7 +14,7 @@ import java.io.InputStream;
*/
public class PrimaryFileClient implements FileClient {
protected static FileClient delegate;
private static FileClient delegate;
@Override
public String upload(String path, byte[] content) throws Exception {
@@ -60,4 +61,12 @@ public class PrimaryFileClient implements FileClient {
return delegate.getContentInputStream(path);
}
public static void setDelegate(FileClient delegate) {
if (PrimaryFileClient.delegate != null) {
// unmodified
throw Exceptions.state();
}
PrimaryFileClient.delegate = delegate;
}
}

View File

@@ -3,10 +3,7 @@ package com.orion.ops.module.infra.convert;
import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.module.infra.entity.domain.SystemUserDO;
import com.orion.ops.module.infra.entity.dto.UserInfoDTO;
import com.orion.ops.module.infra.entity.request.user.SystemUserCreateRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserQueryRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateStatusRequest;
import com.orion.ops.module.infra.entity.request.user.*;
import com.orion.ops.module.infra.entity.vo.SystemUserVO;
import com.orion.ops.module.infra.entity.vo.UserCollectInfoVO;
import org.mapstruct.Mapper;
@@ -40,6 +37,8 @@ public interface SystemUserConvert {
List<SystemUserVO> to(List<SystemUserDO> list);
LoginUser toLoginUser(UserLoginRequest request);
LoginUser toLoginUser(SystemUserDO domain);
UserInfoDTO toUserInfo(SystemUserDO domain);

View File

@@ -1,7 +1,6 @@
package com.orion.ops.module.infra.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.define.wrapper.Pair;
import com.orion.lang.utils.Exceptions;
import com.orion.lang.utils.collect.Lists;
@@ -66,13 +65,18 @@ public class AuthenticationServiceImpl implements AuthenticationService {
@Override
public UserLoginVO login(UserLoginRequest request, HttpServletRequest servletRequest) {
// 设置日志上下文的用户 否则登录失败不会记录日志
OperatorLogs.setUser(SystemUserConvert.MAPPER.toLoginUser(request));
// 登录前检查
this.preCheckLogin(request);
// 获取登录用户
LambdaQueryWrapper<SystemUserDO> wrapper = systemUserDAO.wrapper()
.eq(SystemUserDO::getUsername, request.getUsername());
SystemUserDO user = systemUserDAO.of(wrapper).getOne();
// 设置日志上下文
SystemUserDO user = systemUserDAO.of()
.createWrapper()
.eq(SystemUserDO::getUsername, request.getUsername())
.then()
.getOne();
Valid.notNull(user, ErrorMessage.USERNAME_PASSWORD_ERROR);
// 重新设置日志上下文
OperatorLogs.setUser(SystemUserConvert.MAPPER.toLoginUser(user));
// 检查密码
boolean passwordCorrect = this.checkPassword(request, user);
@@ -225,17 +229,13 @@ public class AuthenticationServiceImpl implements AuthenticationService {
@SuppressWarnings("ALL")
private boolean checkPassword(UserLoginRequest request, SystemUserDO user) {
// 密码正确
if (user != null && user.getPassword().equals(Signatures.md5(request.getPassword()))) {
if (user.getPassword().equals(Signatures.md5(request.getPassword()))) {
return true;
}
// 刷新登录失败缓存
String failedCountKey = UserCacheKeyDefine.LOGIN_FAILED_COUNT.format(request.getUsername());
Long failedLoginCount = redisTemplate.opsForValue().increment(failedCountKey);
RedisUtils.setExpire(failedCountKey, UserCacheKeyDefine.LOGIN_FAILED_COUNT);
// 用户不存在
if (user == null) {
return false;
}
// 锁定用户
if (failedLoginCount >= maxFailedLoginCount) {
// 更新用户表
@@ -243,17 +243,14 @@ public class AuthenticationServiceImpl implements AuthenticationService {
updateUser.setId(user.getId());
updateUser.setStatus(UserStatusEnum.LOCKED.getStatus());
systemUserDAO.updateById(updateUser);
// 更新缓存
// 修改缓存状态
String userInfoKey = UserCacheKeyDefine.USER_INFO.format(user.getId());
String userInfoCache = redisTemplate.opsForValue().get(userInfoKey);
// 缓存不存在
if (userInfoCache == null) {
return false;
if (userInfoCache != null) {
LoginUser loginUser = JSON.parseObject(userInfoCache, LoginUser.class);
loginUser.setStatus(UserStatusEnum.LOCKED.getStatus());
RedisStrings.setJson(userInfoKey, UserCacheKeyDefine.USER_INFO, loginUser);
}
// 修改缓存状态
LoginUser loginUser = JSON.parseObject(userInfoCache, LoginUser.class);
loginUser.setStatus(UserStatusEnum.LOCKED.getStatus());
RedisStrings.setJson(userInfoKey, UserCacheKeyDefine.USER_INFO, loginUser);
}
return false;
}