From e580fcee8795afdf6d2fe78d34519d1a2b96b93c Mon Sep 17 00:00:00 2001 From: lijiahang Date: Fri, 29 Dec 2023 14:13:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=20framework=20?= =?UTF-8?q?=E9=80=BB=E8=BE=91.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/entity/RequestIdentity.java | 23 +- .../common/entity/RequestIdentityModel.java | 30 +++ .../framework/common/utils/CryptoUtils.java | 5 + .../common/utils/FileClientUtils.java | 5 + .../OrionOperatorLogAutoConfiguration.java | 5 +- .../log/core/aspect/OperatorLogAspect.java | 136 ++-------- .../log/core/uitls/OperatorLogFiller.java | 248 ++++++++++++++++++ .../operator/log/core/uitls/OperatorLogs.java | 68 +++-- .../mybatis/core/utils/DomainFillUtils.java | 5 + .../redis/core/utils/RedisUtils.java | 5 + .../security/core/crypto/CryptoProcessor.java | 2 +- .../core/crypto/PrimaryValueCrypto.java | 11 +- .../core/client/AbstractFileClient.java | 2 +- .../core/client/PrimaryFileClient.java | 11 +- .../infra/convert/SystemUserConvert.java | 7 +- .../impl/AuthenticationServiceImpl.java | 33 ++- 16 files changed, 416 insertions(+), 180 deletions(-) create mode 100644 orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/entity/RequestIdentityModel.java create mode 100644 orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogFiller.java diff --git a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/entity/RequestIdentity.java b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/entity/RequestIdentity.java index 82848f0f..93ea56c0 100644 --- a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/entity/RequestIdentity.java +++ b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/entity/RequestIdentity.java @@ -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(); + /** * 设置请求地址 * diff --git a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/entity/RequestIdentityModel.java b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/entity/RequestIdentityModel.java new file mode 100644 index 00000000..14a8bc22 --- /dev/null +++ b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/entity/RequestIdentityModel.java @@ -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; + +} diff --git a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/utils/CryptoUtils.java b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/utils/CryptoUtils.java index 47e96060..12b2a886 100644 --- a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/utils/CryptoUtils.java +++ b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/utils/CryptoUtils.java @@ -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; } diff --git a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/utils/FileClientUtils.java b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/utils/FileClientUtils.java index 64878f79..8dec81bd 100644 --- a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/utils/FileClientUtils.java +++ b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/utils/FileClientUtils.java @@ -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; } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/config/OrionOperatorLogAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/config/OrionOperatorLogAutoConfiguration.java index 00edde77..f88db3bc 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/config/OrionOperatorLogAutoConfiguration.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/config/OrionOperatorLogAutoConfiguration.java @@ -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); } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/aspect/OperatorLogAspect.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/aspect/OperatorLogAspect.java index b07e061f..ec929f19 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/aspect/OperatorLogAspect.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/aspect/OperatorLogAspect.java @@ -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 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 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 extra) { - if (extra != null) { - model.setExtra(JSON.toJSONString(extra)); - } - } - - /** - * 填充日志信息 - * - * @param model model - * @param extra extra - * @param type type - */ - private void fillLogInfo(OperatorLogModel model, Map extra, OperatorType type) { - model.setRiskLevel(type.getRiskLevel().name()); - model.setModule(type.getModule()); - model.setType(type.getType()); - model.setLogInfo(ReplacementFormatters.format(type.getTemplate(), extra)); - } - /** * 异步保存日志 * diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogFiller.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogFiller.java new file mode 100644 index 00000000..2a1a5b27 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogFiller.java @@ -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 { + + 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 extra) { + model.setExtra(JSON.toJSONString(extra)); + return this; + } + + /** + * 填充日志信息 + * + * @param extra extra + * @param type type + * @return this + */ + public OperatorLogFiller fillLogInfo(Map 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; + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogs.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogs.java index c0c5e003..de85d934 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogs.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogs.java @@ -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> EXTRA_HOLDER = new ThreadLocal<>(); + private static final ThreadLocal> 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 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) obj); - return; + EXTRA_HOLDER.get().putAll((Map) 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 map) { - return map == null || !map.containsKey(UN_SAVE_FLAG); - } - /** * 设置用户信息 * @@ -144,20 +138,18 @@ public class OperatorLogs implements ExtraFieldConst { } /** - * 初始化 - * - * @return map + * 清空 */ - private static Map initMap() { - Map 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; } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/utils/DomainFillUtils.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/utils/DomainFillUtils.java index 00b4cb61..9a36ef1a 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/utils/DomainFillUtils.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/utils/DomainFillUtils.java @@ -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; } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisUtils.java b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisUtils.java index bb330b9f..83d05cf4 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisUtils.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisUtils.java @@ -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 redisTemplate) { + if (RedisUtils.redisTemplate != null) { + // unmodified + throw Exceptions.state(); + } RedisUtils.redisTemplate = redisTemplate; } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/crypto/CryptoProcessor.java b/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/crypto/CryptoProcessor.java index b6d8ca40..87f74d8a 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/crypto/CryptoProcessor.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/crypto/CryptoProcessor.java @@ -18,7 +18,7 @@ public abstract class CryptoProcessor implements Va this.config = config; // 设置为默认加密器 if (config.isPrimary()) { - PrimaryValueCrypto.delegate = this; + PrimaryValueCrypto.setDelegate(this); CryptoUtils.setDelegate(this); } } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/crypto/PrimaryValueCrypto.java b/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/crypto/PrimaryValueCrypto.java index c304a535..77bf1412 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/crypto/PrimaryValueCrypto.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/crypto/PrimaryValueCrypto.java @@ -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; + } + } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-storage/src/main/java/com/orion/ops/framework/storage/core/client/AbstractFileClient.java b/orion-ops-framework/orion-ops-spring-boot-starter-storage/src/main/java/com/orion/ops/framework/storage/core/client/AbstractFileClient.java index 6f5fe4fb..89719b23 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-storage/src/main/java/com/orion/ops/framework/storage/core/client/AbstractFileClient.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-storage/src/main/java/com/orion/ops/framework/storage/core/client/AbstractFileClient.java @@ -25,7 +25,7 @@ public abstract class AbstractFileClient implem this.config = config; // 设置默认文件客户端 if (config.isPrimary()) { - PrimaryFileClient.delegate = this; + PrimaryFileClient.setDelegate(this); FileClientUtils.setDelegate(this); } } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-storage/src/main/java/com/orion/ops/framework/storage/core/client/PrimaryFileClient.java b/orion-ops-framework/orion-ops-spring-boot-starter-storage/src/main/java/com/orion/ops/framework/storage/core/client/PrimaryFileClient.java index 77567b1d..ec9fbac9 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-storage/src/main/java/com/orion/ops/framework/storage/core/client/PrimaryFileClient.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-storage/src/main/java/com/orion/ops/framework/storage/core/client/PrimaryFileClient.java @@ -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; + } + } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/convert/SystemUserConvert.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/convert/SystemUserConvert.java index 24aa9047..ff5b7699 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/convert/SystemUserConvert.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/convert/SystemUserConvert.java @@ -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 to(List list); + LoginUser toLoginUser(UserLoginRequest request); + LoginUser toLoginUser(SystemUserDO domain); UserInfoDTO toUserInfo(SystemUserDO domain); diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/AuthenticationServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/AuthenticationServiceImpl.java index 668e65a9..7a6144ed 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/AuthenticationServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/AuthenticationServiceImpl.java @@ -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 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; }