From a7e5a471fbc16371a593269300f35b7952eb32ee Mon Sep 17 00:00:00 2001 From: Hamm Date: Sat, 3 Aug 2024 19:29:56 +0800 Subject: [PATCH 01/10] =?UTF-8?q?feat(Repository):=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=B8=AA=E9=BB=98=E8=AE=A4=E5=8A=A0=E5=86=99?= =?UTF-8?q?=E9=94=81=E6=9F=A5=E8=AF=A2=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- .../java/cn/hamm/airpower/root/RootRepository.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/root/RootRepository.java b/airpower-core/src/main/java/cn/hamm/airpower/root/RootRepository.java index 6bf1f9e..7d6bbfc 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/root/RootRepository.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/root/RootRepository.java @@ -1,8 +1,11 @@ package cn.hamm.airpower.root; +import jakarta.persistence.LockModeType; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Lock; import org.springframework.data.repository.NoRepositoryBean; +import org.springframework.transaction.annotation.Transactional; /** *

数据源接口

@@ -12,4 +15,13 @@ import org.springframework.data.repository.NoRepositoryBean; */ @NoRepositoryBean public interface RootRepository> extends JpaRepository, JpaSpecificationExecutor { + /** + *

加写锁查询

+ * + * @param id ID + * @return 实体 + */ + @Transactional(rollbackFor = Exception.class) + @Lock(LockModeType.PESSIMISTIC_WRITE) + E getWidthLockById(Long id); } \ No newline at end of file -- Gitee From d89dc942cf6f3a327ca75682ef6e4514ee867041 Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 00:31:08 +0800 Subject: [PATCH 02/10] =?UTF-8?q?fix(Utils):=20=E4=BC=98=E5=8C=96Dictionar?= =?UTF-8?q?yUtil=E4=B8=AD=E5=8F=8D=E5=B0=84=E5=B7=A5=E5=85=B7=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- .../src/main/java/cn/hamm/airpower/util/DictionaryUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/DictionaryUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/DictionaryUtil.java index 331eed3..3abda73 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/DictionaryUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/DictionaryUtil.java @@ -81,13 +81,14 @@ public class DictionaryUtil { @NotNull Class clazz, IFunction... lambdas ) { List> mapList = new ArrayList<>(); + ReflectUtil reflectUtil = Utils.getReflectUtil(); for (D obj : clazz.getEnumConstants()) { //取出所有枚举类型 Map item = new HashMap<>(lambdas.length); for (IFunction lambda : lambdas) { // 依次取出参数的值 try { - item.put(StringUtils.uncapitalize(Utils.getReflectUtil().getLambdaFunctionName(lambda)), lambda.apply(obj)); + item.put(StringUtils.uncapitalize(reflectUtil.getLambdaFunctionName(lambda)), lambda.apply(obj)); } catch (Exception exception) { log.error(exception.getMessage(), exception); } -- Gitee From a4133490562ee799da3d3175ed18274704ff4e88 Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 00:32:33 +0800 Subject: [PATCH 03/10] =?UTF-8?q?fix(Utils):=20=E6=9B=B4=E6=96=B0=E4=BA=86?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=B3=A8=E9=87=8A=E4=BB=A5=E6=B8=85=E6=99=B0?= =?UTF-8?q?=E5=9C=B0=E6=8F=8F=E8=BF=B0=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- .../cn/hamm/airpower/util/DictionaryUtil.java | 4 ++-- .../cn/hamm/airpower/util/ReflectUtil.java | 22 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/DictionaryUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/DictionaryUtil.java index 3abda73..03f3fc6 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/DictionaryUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/DictionaryUtil.java @@ -62,7 +62,7 @@ public class DictionaryUtil { *

获取指定枚举类的 {@code ListMap} 数据

* * @param clazz 枚举类 - * @return ListMap + * @return 枚举选项列表 */ public final @NotNull List> getDictionaryList(@NotNull Class clazz) { return getDictionaryList(clazz, IDictionary::getKey, IDictionary::getLabel); @@ -74,7 +74,7 @@ public class DictionaryUtil { * @param clazz 枚举字典类 * @param lambdas 需要获取的方法表达式 * @param 字典类型 - * @return ListMap + * @return 枚举选项列表 */ @SafeVarargs public final @NotNull List> getDictionaryList( diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/ReflectUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/ReflectUtil.java index 4c394df..36051d9 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/ReflectUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/ReflectUtil.java @@ -37,6 +37,17 @@ import java.util.stream.Collectors; @Slf4j @Component public class ReflectUtil { + /** + *

缓存字段列表

+ */ + private final static ConcurrentHashMap, List> FIELD_LIST_MAP = new ConcurrentHashMap<>(); + /** + *

缓存属性列表

+ * + * @apiNote 声明属性列表 + */ + private final static ConcurrentHashMap DECLARED_FIELD_LIST_MAP = new ConcurrentHashMap<>(); + /** *

获取对象指定属性的值

* @@ -245,17 +256,6 @@ public class ReflectUtil { return isModel(clazz.getSuperclass()); } - /** - *

缓存字段列表

- */ - private final static ConcurrentHashMap, List> FIELD_LIST_MAP = new ConcurrentHashMap<>(); - /** - *

缓存属性列表

- * - * @apiNote 声明属性列表 - */ - private final static ConcurrentHashMap DECLARED_FIELD_LIST_MAP = new ConcurrentHashMap<>(); - /** *

获取指定类的字段列表

* -- Gitee From 99e94e90bf2c97a156fb0e80b0b5fd6a5f16c9cb Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 00:42:14 +0800 Subject: [PATCH 04/10] =?UTF-8?q?fix(Core):=20=E4=BC=98=E5=8C=96=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E4=BB=A4=E7=89=8C=E5=92=8C=E6=9D=83=E9=99=90=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构了请求拦截器中的访问令牌获取和权限校验逻辑,以提高代码的可读性和维护性。具体改动包括: - 引入AccessUtil工具类以统一访问操作处理。 - 优化了accessToken和accessTokenFromParam的获取方式,通过明确使用ServiceConfig对象,使配置项的使用更加直观。 - 调整了用户权限校验方法中的权限标识符获取方式,减少重复代码,增强代码逻辑的清晰度。同时,还对项目中的其他几个类进行了小幅调整,优化了注释和方法调用方式,以提升代码整体的整洁度和可维护性。 Signed-off-by: Hamm --- .../cn/hamm/airpower/annotation/Desensitize.java | 2 ++ .../cn/hamm/airpower/annotation/ExcelColumn.java | 7 ++++++- .../main/java/cn/hamm/airpower/config/Constant.java | 2 +- .../interceptor/AbstractRequestInterceptor.java | 12 ++++++++---- .../interceptor/ResponseBodyInterceptor.java | 10 ++++++---- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/annotation/Desensitize.java b/airpower-core/src/main/java/cn/hamm/airpower/annotation/Desensitize.java index 03ed10a..33687c2 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/annotation/Desensitize.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/annotation/Desensitize.java @@ -32,6 +32,8 @@ public @interface Desensitize { /** *

脱敏符号

+ * + * @apiNote 如字段标记脱敏且提交的数据包含脱敏符号,则该类数据不更新到数据库 */ String symbol() default "*"; diff --git a/airpower-core/src/main/java/cn/hamm/airpower/annotation/ExcelColumn.java b/airpower-core/src/main/java/cn/hamm/airpower/annotation/ExcelColumn.java index 5b1a35a..09e206a 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/annotation/ExcelColumn.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/annotation/ExcelColumn.java @@ -1,6 +1,7 @@ package cn.hamm.airpower.annotation; import cn.hamm.airpower.validate.dictionary.Dictionary; + import java.lang.annotation.*; /** @@ -14,10 +15,13 @@ import java.lang.annotation.*; @Documented public @interface ExcelColumn { /** - *

类型

+ *

列数据类型

*/ Type value() default Type.TEXT; + /** + *

列数据类型

+ */ enum Type { /** *

普通文本

@@ -36,6 +40,7 @@ public @interface ExcelColumn { /** *

字典

+ * * @apiNote 请确保同时标记了 @{@link Dictionary} */ DICTIONARY, diff --git a/airpower-core/src/main/java/cn/hamm/airpower/config/Constant.java b/airpower-core/src/main/java/cn/hamm/airpower/config/Constant.java index 2ad02e9..6964ac4 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/config/Constant.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/config/Constant.java @@ -29,7 +29,7 @@ public class Constant { public static final String ID = "id"; /** - *

一分钟 {@code 60}

+ *

一分钟 {@code 60} 秒

*/ public static final int SECOND_PER_MINUTE = 60; diff --git a/airpower-core/src/main/java/cn/hamm/airpower/interceptor/AbstractRequestInterceptor.java b/airpower-core/src/main/java/cn/hamm/airpower/interceptor/AbstractRequestInterceptor.java index 16250cd..052da8b 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/interceptor/AbstractRequestInterceptor.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/interceptor/AbstractRequestInterceptor.java @@ -3,9 +3,11 @@ package cn.hamm.airpower.interceptor; import cn.hamm.airpower.config.Configs; import cn.hamm.airpower.config.Constant; import cn.hamm.airpower.config.MessageConstant; +import cn.hamm.airpower.config.ServiceConfig; import cn.hamm.airpower.enums.ServiceError; import cn.hamm.airpower.interceptor.document.ApiDocument; import cn.hamm.airpower.model.Access; +import cn.hamm.airpower.util.AccessUtil; import cn.hamm.airpower.util.Utils; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -87,16 +89,18 @@ public abstract class AbstractRequestInterceptor implements HandlerInterceptor { */ private void handleRequest(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, Class clazz, Method method) { interceptRequest(request, response, clazz, method); - Access access = Utils.getAccessUtil().getWhatNeedAccess(clazz, method); + AccessUtil accessUtil = Utils.getAccessUtil(); + Access access = accessUtil.getWhatNeedAccess(clazz, method); if (!access.isLogin()) { // 不需要登录 直接返回有权限 return; } //需要登录 - String accessToken = request.getHeader(Configs.getServiceConfig().getAuthorizeHeader()); + ServiceConfig serviceConfig = Configs.getServiceConfig(); + String accessToken = request.getHeader(serviceConfig.getAuthorizeHeader()); // 优先使用 Get 参数传入的身份 - String accessTokenFromParam = request.getParameter(Configs.getServiceConfig().getAuthorizeHeader()); + String accessTokenFromParam = request.getParameter(serviceConfig.getAuthorizeHeader()); if (StringUtils.hasText(accessTokenFromParam)) { accessToken = accessTokenFromParam; } @@ -105,7 +109,7 @@ public abstract class AbstractRequestInterceptor implements HandlerInterceptor { //需要RBAC if (access.isAuthorize()) { //验证用户是否有接口的访问权限 - checkUserPermission(userId, Utils.getAccessUtil().getPermissionIdentity(clazz, method), request); + checkUserPermission(userId, accessUtil.getPermissionIdentity(clazz, method), request); } } diff --git a/airpower-core/src/main/java/cn/hamm/airpower/interceptor/ResponseBodyInterceptor.java b/airpower-core/src/main/java/cn/hamm/airpower/interceptor/ResponseBodyInterceptor.java index 1536393..2b6d4f1 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/interceptor/ResponseBodyInterceptor.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/interceptor/ResponseBodyInterceptor.java @@ -5,6 +5,7 @@ import cn.hamm.airpower.annotation.Filter; import cn.hamm.airpower.model.Json; import cn.hamm.airpower.model.query.QueryPageResponse; import cn.hamm.airpower.root.RootModel; +import cn.hamm.airpower.util.ReflectUtil; import cn.hamm.airpower.util.Utils; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.Contract; @@ -94,8 +95,9 @@ public class ResponseBodyInterceptor implements ResponseBodyAdvice { return result; } - Filter filter = Utils.getReflectUtil().getAnnotation(Filter.class, method); - DesensitizeExclude desensitizeExclude = Utils.getReflectUtil().getAnnotation(DesensitizeExclude.class, method); + ReflectUtil reflectUtil = Utils.getReflectUtil(); + Filter filter = reflectUtil.getAnnotation(Filter.class, method); + DesensitizeExclude desensitizeExclude = reflectUtil.getAnnotation(DesensitizeExclude.class, method); if (json.getData() instanceof QueryPageResponse) { QueryPageResponse queryPageResponse = (QueryPageResponse) json.getData(); // 如果 data 分页对象 @@ -111,13 +113,13 @@ public class ResponseBodyInterceptor implements ResponseBodyAdvice { collection.stream() .toList() .forEach(item -> { - if (Utils.getReflectUtil().isModel(item.getClass())) { + if (reflectUtil.isModel(item.getClass())) { ((M) item).filterAndDesensitize(filter, Objects.isNull(desensitizeExclude)); } }); return json.setData(collection); } - if (Utils.getReflectUtil().isModel(dataCls)) { + if (reflectUtil.isModel(dataCls)) { // 如果 data 是 Model //noinspection unchecked return json.setData(((M) json.getData()).filterAndDesensitize(filter, Objects.isNull(desensitizeExclude))); -- Gitee From 033c89f2344226646bcc5eba18e8ea9848fee30a Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 00:50:15 +0800 Subject: [PATCH 05/10] =?UTF-8?q?fix(Utils):=20=E9=87=8D=E6=9E=84AccessUti?= =?UTF-8?q?l=E4=BB=A5=E4=BD=BF=E7=94=A8=E5=AE=9E=E7=94=A8=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- .../cn/hamm/airpower/util/AccessUtil.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/AccessUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/AccessUtil.java index a0b4fcf..ca09d54 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/AccessUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/AccessUtil.java @@ -113,6 +113,8 @@ public class AccessUtil { Resource[] resources = resourcePatternResolver.getResources(pattern); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); + ReflectUtil reflectUtil = Utils.getReflectUtil(); + DictionaryUtil dictionaryUtil = Utils.getDictionaryUtil(); for (Resource resource : resources) { // 用于读取类信息 MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); @@ -120,13 +122,13 @@ public class AccessUtil { String className = metadataReader.getClassMetadata().getClassName(); Class clazz = Class.forName(className); - ApiController apiController = Utils.getReflectUtil().getAnnotation(ApiController.class, clazz); + ApiController apiController = reflectUtil.getAnnotation(ApiController.class, clazz); if (Objects.isNull(apiController)) { // 不是rest控制器或者是指定的几个白名单控制器 continue; } - String customClassName = Utils.getReflectUtil().getDescription(clazz); + String customClassName = reflectUtil.getDescription(clazz); String identity = clazz.getSimpleName().replaceAll(Constant.CONTROLLER_SUFFIX, Constant.EMPTY_STRING); P permission = permissionClass.getConstructor().newInstance(); @@ -139,12 +141,12 @@ public class AccessUtil { Method[] methods = clazz.getMethods(); // 取出控制器类上的Extends注解 如自己没标 则使用父类的 - Extends extendsApi = Utils.getReflectUtil().getAnnotation(Extends.class, clazz); + Extends extendsApi = reflectUtil.getAnnotation(Extends.class, clazz); for (Method method : methods) { if (Objects.nonNull(extendsApi)) { try { - Api current = Utils.getDictionaryUtil().getDictionary(Api.class, Api::getMethodName, method.getName()); + Api current = dictionaryUtil.getDictionary(Api.class, Api::getMethodName, method.getName()); if (checkApiExcluded(current, extendsApi)) { continue; } @@ -155,7 +157,7 @@ public class AccessUtil { if (apiPath.equals(subIdentity)) { continue; } - String customMethodName = Utils.getReflectUtil().getDescription(method); + String customMethodName = reflectUtil.getDescription(method); Access accessConfig = Utils.getAccessUtil().getWhatNeedAccess(clazz, method); if (!accessConfig.isLogin() || !accessConfig.isAuthorize()) { // 这里可以选择是否不读取这些接口的权限,但前端可能需要 @@ -182,10 +184,10 @@ public class AccessUtil { */ private boolean checkApiExcluded(Api api, @NotNull Extends extend) { List excludeList = Arrays.asList(extend.exclude()); - List includeList = Arrays.asList(extend.value()); if (excludeList.contains(api)) { return true; } + List includeList = Arrays.asList(extend.value()); if (includeList.isEmpty()) { return false; } @@ -200,9 +202,10 @@ public class AccessUtil { * @return 权限标识 */ private @NotNull String getMethodPermissionIdentity(Method method, String apiPath) { - RequestMapping requestMapping = Utils.getReflectUtil().getAnnotation(RequestMapping.class, method); - PostMapping postMapping = Utils.getReflectUtil().getAnnotation(PostMapping.class, method); - GetMapping getMapping = Utils.getReflectUtil().getAnnotation(GetMapping.class, method); + ReflectUtil reflectUtil = Utils.getReflectUtil(); + RequestMapping requestMapping = reflectUtil.getAnnotation(RequestMapping.class, method); + PostMapping postMapping = reflectUtil.getAnnotation(PostMapping.class, method); + GetMapping getMapping = reflectUtil.getAnnotation(GetMapping.class, method); if (Objects.isNull(requestMapping) && Objects.isNull(postMapping) && Objects.isNull(getMapping)) { return apiPath; -- Gitee From 56200f2e4d7217c29be9f01f36ec76312777f8af Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 00:53:57 +0800 Subject: [PATCH 06/10] =?UTF-8?q?refactor(util):=20=E9=87=8D=E6=9E=84Cooki?= =?UTF-8?q?eUtil=E5=B9=B6=E4=BF=AE=E6=AD=A3AesUtil=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- .../src/main/java/cn/hamm/airpower/util/AesUtil.java | 4 ++-- .../src/main/java/cn/hamm/airpower/util/CookieUtil.java | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/AesUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/AesUtil.java index 5e559ed..95df5a8 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/AesUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/AesUtil.java @@ -71,9 +71,9 @@ public class AesUtil { } /** - *

获取随机密钥

+ *

获取随机向量

* - * @return 随机密钥 + * @return 随机向量 */ @Contract(" -> new") public static @NotNull String getRandomIv() { diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/CookieUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/CookieUtil.java index b913449..8c0f8f8 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/CookieUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/CookieUtil.java @@ -23,9 +23,10 @@ public class CookieUtil { */ public final @NotNull Cookie getCookie(String key, String value) { Cookie cookie = new Cookie(key, value); - cookie.setHttpOnly(Configs.getCookieConfig().isCookieHttpOnly()); - cookie.setMaxAge(Configs.getCookieConfig().getCookieMaxAge()); - cookie.setSecure(Configs.getCookieConfig().isCookieSecurity()); + CookieConfig cookieConfig = Configs.getCookieConfig(); + cookie.setHttpOnly(cookieConfig.isCookieHttpOnly()); + cookie.setMaxAge(cookieConfig.getCookieMaxAge()); + cookie.setSecure(cookieConfig.isCookieSecurity()); cookie.setPath(cookie.getPath()); return cookie; } -- Gitee From c33a20d2280ceae9aa45fe7a556f0d3d04a51c0f Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 00:55:46 +0800 Subject: [PATCH 07/10] =?UTF-8?q?fix(Utils):=20=E4=BC=98=E5=8C=96MqttUtil?= =?UTF-8?q?=E4=B8=AD=E5=88=9B=E5=BB=BA=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=92=8C?= =?UTF-8?q?=E9=80=89=E9=A1=B9=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- .../src/main/java/cn/hamm/airpower/util/MqttUtil.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/MqttUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/MqttUtil.java index 7755708..fea05a9 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/MqttUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/MqttUtil.java @@ -2,6 +2,7 @@ package cn.hamm.airpower.util; import cn.hamm.airpower.config.Configs; import cn.hamm.airpower.config.MessageConstant; +import cn.hamm.airpower.config.MqttConfig; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; @@ -37,8 +38,9 @@ public class MqttUtil { * @throws MqttException 异常 */ public @NotNull MqttClient createClient(String id) throws MqttException { + MqttConfig mqttConfig = Configs.getMqttConfig(); return new MqttClient( - "tcp://" + Configs.getMqttConfig().getHost() + ":" + Configs.getMqttConfig().getPort(), + "tcp://" + mqttConfig.getHost() + ":" + mqttConfig.getPort(), id, new MemoryPersistence() ); @@ -52,8 +54,9 @@ public class MqttUtil { public MqttConnectOptions createOption() { MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(false); - options.setUserName(Configs.getMqttConfig().getUser()); - options.setPassword(Configs.getMqttConfig().getPass().toCharArray()); + MqttConfig mqttConfig = Configs.getMqttConfig(); + options.setUserName(mqttConfig.getUser()); + options.setPassword(mqttConfig.getPass().toCharArray()); options.setConnectionTimeout(30); options.setKeepAliveInterval(10); return options; -- Gitee From 8640a3d934a40926243d83ff78d3b04203fb3e70 Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 01:00:55 +0800 Subject: [PATCH 08/10] =?UTF-8?q?refactor(utils):=20=E9=87=8D=E6=9E=84Requ?= =?UTF-8?q?estUtil=E4=B8=AD=E7=9A=84getIpAddress=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- .../cn/hamm/airpower/util/ReflectUtil.java | 1 + .../cn/hamm/airpower/util/RequestUtil.java | 19 ++++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/ReflectUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/ReflectUtil.java index 36051d9..220a94b 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/ReflectUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/ReflectUtil.java @@ -41,6 +41,7 @@ public class ReflectUtil { *

缓存字段列表

*/ private final static ConcurrentHashMap, List> FIELD_LIST_MAP = new ConcurrentHashMap<>(); + /** *

缓存属性列表

* diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/RequestUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/RequestUtil.java index c238fc7..210866d 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/RequestUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/RequestUtil.java @@ -13,6 +13,7 @@ import org.springframework.stereotype.Component; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.List; import java.util.Objects; /** @@ -57,18 +58,14 @@ public class RequestUtil { public final String getIpAddress(HttpServletRequest request) { String ipAddress; try { - ipAddress = request.getHeader("x-forwarded-for"); - if (isValidAddress(ipAddress)) { - return ipAddress; - } - ipAddress = request.getHeader("Proxy-Client-IP"); - if (isValidAddress(ipAddress)) { - return ipAddress; - } - ipAddress = request.getHeader("WL-Proxy-Client-IP"); - if (isValidAddress(ipAddress)) { - return ipAddress; + List ipHeaders = List.of("x-forwarded-for", "Proxy-Client-IP", "WL-Proxy-Client-IP"); + for (String ipHeader : ipHeaders) { + ipAddress = request.getHeader(ipHeader); + if (isValidAddress(ipAddress)) { + return ipAddress; + } } + ipAddress = request.getRemoteAddr(); if (Constant.LOCAL_IP_ADDRESS.equals(ipAddress)) { // 根据网卡取本机配置的IP -- Gitee From 00689775b36276ddf2643679020133eb48cc05bb Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 01:08:04 +0800 Subject: [PATCH 09/10] =?UTF-8?q?refactor(core):=20=E9=87=8D=E6=9E=84webso?= =?UTF-8?q?cket=E9=85=8D=E7=BD=AE=E5=92=8C=E6=B6=88=E6=81=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- .../java/cn/hamm/airpower/AbstractWebConfig.java | 7 ++++--- .../main/java/cn/hamm/airpower/util/TreeUtil.java | 7 +++---- .../cn/hamm/airpower/websocket/WebSocketHandler.java | 12 ++++++++---- .../cn/hamm/airpower/websocket/WebsocketUtil.java | 6 ++++-- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/AbstractWebConfig.java b/airpower-core/src/main/java/cn/hamm/airpower/AbstractWebConfig.java index 0e76876..cc8caca 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/AbstractWebConfig.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/AbstractWebConfig.java @@ -109,14 +109,15 @@ public abstract class AbstractWebConfig implements WebMvcConfigurer, WebSocketCo */ @Override public final void registerWebSocketHandlers(@NotNull WebSocketHandlerRegistry registry) { - if (Configs.getWebsocketConfig().getSupport().equals(WebSocketSupport.NO)) { + WebSocketConfig websocketConfig = Configs.getWebsocketConfig(); + if (websocketConfig.getSupport().equals(WebSocketSupport.NO)) { return; } - final String channelPrefix = Configs.getWebsocketConfig().getChannelPrefix(); + final String channelPrefix = websocketConfig.getChannelPrefix(); if (Objects.isNull(channelPrefix) || !StringUtils.hasText(channelPrefix)) { throw new ServiceException("没有配置 airpower.websocket.channelPrefix, 无法启动WebSocket服务"); } - registry.addHandler(getWebsocketHandler(), Configs.getWebsocketConfig().getPath()) + registry.addHandler(getWebsocketHandler(), websocketConfig.getPath()) .setAllowedOrigins(webSocketConfig.getAllowedOrigins()); } } \ No newline at end of file diff --git a/airpower-core/src/main/java/cn/hamm/airpower/util/TreeUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/util/TreeUtil.java index 0f37dd8..9b30c36 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/util/TreeUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/util/TreeUtil.java @@ -36,10 +36,9 @@ public class TreeUtil { private > List buildTreeList(@NotNull List list, Long parentId) { return list.stream() .filter(item -> parentId.equals(item.getParentId())) - .peek(item -> { - List children = buildTreeList(list, item.getId()); - item.setChildren(children); - }) + .peek(item -> item.setChildren( + buildTreeList(list, item.getId()) + )) .toList(); } } diff --git a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketHandler.java b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketHandler.java index 6d281e9..b703437 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketHandler.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketHandler.java @@ -2,8 +2,10 @@ package cn.hamm.airpower.websocket; import cn.hamm.airpower.config.Configs; import cn.hamm.airpower.config.Constant; +import cn.hamm.airpower.config.WebSocketConfig; import cn.hamm.airpower.exception.ServiceException; import cn.hamm.airpower.model.Json; +import cn.hamm.airpower.util.MqttUtil; import cn.hamm.airpower.util.Utils; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.*; @@ -71,9 +73,10 @@ public class WebSocketHandler extends TextWebSocketHandler implements MessageLis @Override protected final void handleTextMessage(@NonNull WebSocketSession session, @NotNull TextMessage textMessage) { final String message = textMessage.getPayload(); - if (Configs.getWebsocketConfig().getPing().equalsIgnoreCase(message)) { + WebSocketConfig websocketConfig = Configs.getWebsocketConfig(); + if (websocketConfig.getPing().equalsIgnoreCase(message)) { try { - session.sendMessage(new TextMessage(Configs.getWebsocketConfig().getPong())); + session.sendMessage(new TextMessage(websocketConfig.getPong())); } catch (IOException e) { log.error("发送Websocket消息失败: {}", e.getMessage()); } @@ -179,7 +182,8 @@ public class WebSocketHandler extends TextWebSocketHandler implements MessageLis * @param userId 用户 {@code ID} */ private void startMqttListener(@NotNull WebSocketSession session, long userId) { - try (MqttClient mqttClient = Utils.getMqttUtil().createClient()) { + MqttUtil mqttUtil = Utils.getMqttUtil(); + try (MqttClient mqttClient = mqttUtil.createClient()) { mqttClient.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable throwable) { @@ -197,7 +201,7 @@ public class WebSocketHandler extends TextWebSocketHandler implements MessageLis } }); - mqttClient.connect(Utils.getMqttUtil().createOption()); + mqttClient.connect(mqttUtil.createOption()); final String personalChannel = CHANNEL_USER_PREFIX + userId; String[] topics = {CHANNEL_ALL, personalChannel}; mqttClient.subscribe(topics); diff --git a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketUtil.java index 995c0b7..191ee88 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketUtil.java @@ -2,6 +2,7 @@ package cn.hamm.airpower.websocket; import cn.hamm.airpower.config.Configs; import cn.hamm.airpower.config.Constant; +import cn.hamm.airpower.config.WebSocketConfig; import cn.hamm.airpower.exception.ServiceException; import cn.hamm.airpower.model.Json; import cn.hamm.airpower.util.Utils; @@ -46,7 +47,8 @@ public class WebsocketUtil { * @param payload 负载 */ public final void publishToChannel(String channel, WebSocketPayload payload) { - final String channelPrefix = Configs.getWebsocketConfig().getChannelPrefix(); + WebSocketConfig websocketConfig = Configs.getWebsocketConfig(); + final String channelPrefix = websocketConfig.getChannelPrefix(); if (Objects.isNull(channelPrefix) || !StringUtils.hasText(channelPrefix)) { throw new ServiceException("没有配置 airpower.websocket.channelPrefix, 无法启动WebSocket服务"); } @@ -54,7 +56,7 @@ public class WebsocketUtil { final WebSocketEvent event = WebSocketEvent.create(payload); log.info("发布消息到频道 {} : {}", targetChannel, Json.toString(event)); try { - switch (Configs.getWebsocketConfig().getSupport()) { + switch (websocketConfig.getSupport()) { case REDIS -> Utils.getRedisUtil().publish(targetChannel, Json.toString(event)); case MQTT -> Utils.getMqttUtil().publish(targetChannel, Json.toString(event)); default -> throw new RuntimeException("WebSocket暂不支持"); -- Gitee From 42b6b0bac453f3a264226f5e7759cac0cfa32f6d Mon Sep 17 00:00:00 2001 From: Hamm Date: Sun, 4 Aug 2024 02:02:01 +0800 Subject: [PATCH 10/10] =?UTF-8?q?release(v2.2.5):=20=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E4=BA=86`v2.2.5`=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hamm --- airpower-core/pom.xml | 4 ++-- pom.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/airpower-core/pom.xml b/airpower-core/pom.xml index 0806dde..e14a6bb 100644 --- a/airpower-core/pom.xml +++ b/airpower-core/pom.xml @@ -5,10 +5,10 @@ cn.hamm airpower - 2.2.4 + 2.2.5 airpower-core - 2.2.4 + 2.2.5 airpower-core AirPower is a fast backend development tool based on SpringBoot3 and JPA. It's the core of AirPower. diff --git a/pom.xml b/pom.xml index ccb419f..4629fb4 100644 --- a/pom.xml +++ b/pom.xml @@ -4,11 +4,11 @@ 4.0.0 cn.hamm airpower - 2.2.4 + 2.2.5 airpower AirPower is a fast backend development tool based on SpringBoot3 and JPA. - 2.2.4 + 2.2.5 UTF-8 UTF-8 UTF-8 -- Gitee