From f9228090314c2423b515cb7ef1194876c0a14a5b Mon Sep 17 00:00:00 2001 From: Wang Chen Chen <932560435@qq.com> Date: Sun, 14 Jan 2024 15:25:51 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=20MyBatis-Plus=20=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89ID=E7=94=9F=E6=88=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../molly/monitor/entity/LmsLoginLog.java | 7 +-- .../molly/monitor/entity/LmsOperLog.java | 7 +-- .../com/xaaef/molly/perms/entity/PmsUser.java | 2 +- .../molly/perms/entity/PmsUserSocial.java | 2 +- .../service/impl/PmsUserServiceImpl.java | 2 - .../java/com/xaaef/molly/NoSpringTests.java | 9 ++++ .../service/impl/UserLoginServiceImpl.java | 7 ++- .../xaaef/molly/tenant/MybatisPlusConfig.java | 23 +++++++- .../molly/web/log/BindingResultAspect.java | 53 ------------------- .../xaaef/molly/web/log/OperateLogAspect.java | 4 +- 10 files changed, 44 insertions(+), 72 deletions(-) delete mode 100644 server/plugins/web-config/src/main/java/com/xaaef/molly/web/log/BindingResultAspect.java diff --git a/server/molly-monitor/src/main/java/com/xaaef/molly/monitor/entity/LmsLoginLog.java b/server/molly-monitor/src/main/java/com/xaaef/molly/monitor/entity/LmsLoginLog.java index c27cbfd..b05bc23 100644 --- a/server/molly-monitor/src/main/java/com/xaaef/molly/monitor/entity/LmsLoginLog.java +++ b/server/molly-monitor/src/main/java/com/xaaef/molly/monitor/entity/LmsLoginLog.java @@ -1,9 +1,6 @@ package com.xaaef.molly.monitor.entity; -import com.baomidou.mybatisplus.annotation.FieldFill; -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.annotation.*; import com.fasterxml.jackson.annotation.JsonIgnore; import io.swagger.v3.oas.annotations.media.Schema; import lombok.*; @@ -34,7 +31,7 @@ public class LmsLoginLog implements java.io.Serializable { /** * ID */ - @TableId + @TableId(type = IdType.ASSIGN_ID) @Schema(description = "ID") private String id; diff --git a/server/molly-monitor/src/main/java/com/xaaef/molly/monitor/entity/LmsOperLog.java b/server/molly-monitor/src/main/java/com/xaaef/molly/monitor/entity/LmsOperLog.java index 08199c7..1053a80 100644 --- a/server/molly-monitor/src/main/java/com/xaaef/molly/monitor/entity/LmsOperLog.java +++ b/server/molly-monitor/src/main/java/com/xaaef/molly/monitor/entity/LmsOperLog.java @@ -1,9 +1,6 @@ package com.xaaef.molly.monitor.entity; -import com.baomidou.mybatisplus.annotation.FieldFill; -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.annotation.*; import io.swagger.v3.oas.annotations.media.Schema; import lombok.*; import lombok.experimental.Accessors; @@ -33,7 +30,7 @@ public class LmsOperLog implements java.io.Serializable { /** * ID */ - @TableId + @TableId(type = IdType.ASSIGN_ID) @Schema(description = "ID") private String id; diff --git a/server/molly-pms/src/main/java/com/xaaef/molly/perms/entity/PmsUser.java b/server/molly-pms/src/main/java/com/xaaef/molly/perms/entity/PmsUser.java index 2523b8e..f40fb58 100644 --- a/server/molly-pms/src/main/java/com/xaaef/molly/perms/entity/PmsUser.java +++ b/server/molly-pms/src/main/java/com/xaaef/molly/perms/entity/PmsUser.java @@ -41,7 +41,7 @@ public class PmsUser extends BaseEntity { * 用户ID */ @Schema(description = "用户ID") - @TableId(type = IdType.AUTO) + @TableId(type = IdType.ASSIGN_ID) @NotNull(message = "用户ID,必须填写", groups = {ValidUpdate.class}) private Long userId; diff --git a/server/molly-pms/src/main/java/com/xaaef/molly/perms/entity/PmsUserSocial.java b/server/molly-pms/src/main/java/com/xaaef/molly/perms/entity/PmsUserSocial.java index ef9b78a..b30ba1e 100644 --- a/server/molly-pms/src/main/java/com/xaaef/molly/perms/entity/PmsUserSocial.java +++ b/server/molly-pms/src/main/java/com/xaaef/molly/perms/entity/PmsUserSocial.java @@ -36,7 +36,7 @@ public class PmsUserSocial extends BaseEntity { * id */ @Schema(description = "社交账号ID") - @TableId(type = IdType.AUTO) + @TableId(type = IdType.ASSIGN_ID) @NotNull(message = "社交账号ID,必须填写", groups = {ValidUpdate.class}) private Long socialId; diff --git a/server/molly-pms/src/main/java/com/xaaef/molly/perms/service/impl/PmsUserServiceImpl.java b/server/molly-pms/src/main/java/com/xaaef/molly/perms/service/impl/PmsUserServiceImpl.java index 19ffd73..415afaa 100644 --- a/server/molly-pms/src/main/java/com/xaaef/molly/perms/service/impl/PmsUserServiceImpl.java +++ b/server/molly-pms/src/main/java/com/xaaef/molly/perms/service/impl/PmsUserServiceImpl.java @@ -15,7 +15,6 @@ import com.xaaef.molly.auth.service.UserLoginService; import com.xaaef.molly.common.enums.AdminFlag; import com.xaaef.molly.common.enums.StatusEnum; import com.xaaef.molly.common.enums.UserType; -import com.xaaef.molly.common.util.IdUtils; import com.xaaef.molly.internal.api.ApiCmsProjectService; import com.xaaef.molly.internal.api.ApiSysConfigService; import com.xaaef.molly.internal.api.ApiSysMenuService; @@ -157,7 +156,6 @@ public class PmsUserServiceImpl extends BaseServiceImpl entity.setStatus(StatusEnum.NORMAL.getCode()); } - entity.setUserId(IdUtils.getStandaloneId()); var ok = super.save(entity); if (entity.getRoles() != null) { diff --git a/server/molly-service/src/test/java/com/xaaef/molly/NoSpringTests.java b/server/molly-service/src/test/java/com/xaaef/molly/NoSpringTests.java index 4c1c649..8441232 100644 --- a/server/molly-service/src/test/java/com/xaaef/molly/NoSpringTests.java +++ b/server/molly-service/src/test/java/com/xaaef/molly/NoSpringTests.java @@ -15,6 +15,7 @@ import com.xaaef.molly.auth.jwt.JwtSecurityUtils; import com.xaaef.molly.common.consts.ConfigDataConst; import com.xaaef.molly.common.consts.JwtConst; import com.xaaef.molly.common.consts.RegexConst; +import com.xaaef.molly.common.util.IdUtils; import com.xaaef.molly.common.util.JsonUtils; import com.xaaef.molly.common.util.TenantUtils; import com.xaaef.molly.common.util.WrapExcelUtils; @@ -257,4 +258,12 @@ public class NoSpringTests { } + @Test + public void test17() { + for (int i = 0; i < 10; i++) { + System.out.println(IdUtils.getStandaloneId()); + } + } + + } diff --git a/server/plugins/auth-jwt/src/main/java/com/xaaef/molly/auth/service/impl/UserLoginServiceImpl.java b/server/plugins/auth-jwt/src/main/java/com/xaaef/molly/auth/service/impl/UserLoginServiceImpl.java index de0232b..5222294 100644 --- a/server/plugins/auth-jwt/src/main/java/com/xaaef/molly/auth/service/impl/UserLoginServiceImpl.java +++ b/server/plugins/auth-jwt/src/main/java/com/xaaef/molly/auth/service/impl/UserLoginServiceImpl.java @@ -1,7 +1,6 @@ package com.xaaef.molly.auth.service.impl; import cn.hutool.core.date.LocalDateTimeUtil; -import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import com.xaaef.molly.auth.enums.GrantType; import com.xaaef.molly.auth.exception.JwtAuthException; @@ -17,6 +16,7 @@ import com.xaaef.molly.common.domain.CustomRequestInfo; import com.xaaef.molly.common.enums.AdminFlag; import com.xaaef.molly.common.enums.StatusEnum; import com.xaaef.molly.common.enums.UserType; +import com.xaaef.molly.common.util.IdUtils; import com.xaaef.molly.common.util.ServletUtils; import com.xaaef.molly.internal.api.*; import com.xaaef.molly.internal.dto.LoginLogDTO; @@ -113,7 +113,7 @@ public class UserLoginServiceImpl implements UserLoginService { target.setLoginTime(LocalDateTime.now()); target.setTenantId(currentTenant.getTenantId()); // 生成一个随机ID 跟当前用户关联 - target.setLoginId(IdUtil.getSnowflakeNextIdStr()); + target.setLoginId(IdUtils.getStandaloneStrId()); // 判断当前登录的用户类型。系统用户 还是 租户用户 String defaultTenantId = tenantService.getByDefaultTenantId(); @@ -150,6 +150,9 @@ public class UserLoginServiceImpl implements UserLoginService { var props = tokenService.getProps(); + // 将当前用户设置为登录 + JwtSecurityUtils.setLoginUser(target); + return JwtTokenValue.builder() .header(props.getTokenHeader()) .accessToken(tokenService.createJwtStr(target.getLoginId())) diff --git a/server/plugins/mbp-tenant/src/main/java/com/xaaef/molly/tenant/MybatisPlusConfig.java b/server/plugins/mbp-tenant/src/main/java/com/xaaef/molly/tenant/MybatisPlusConfig.java index c54b5a5..3e25327 100644 --- a/server/plugins/mbp-tenant/src/main/java/com/xaaef/molly/tenant/MybatisPlusConfig.java +++ b/server/plugins/mbp-tenant/src/main/java/com/xaaef/molly/tenant/MybatisPlusConfig.java @@ -1,12 +1,14 @@ package com.xaaef.molly.tenant; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; import com.xaaef.molly.auth.jwt.JwtSecurityUtils; +import com.xaaef.molly.common.util.IdUtils; import com.xaaef.molly.common.util.JsonUtils; import com.xaaef.molly.common.util.TenantUtils; import com.xaaef.molly.tenant.props.MultiTenantProperties; @@ -88,6 +90,26 @@ public class MybatisPlusConfig { } + /** + * 自定义 ID 生成器 + * + * @author WangChenChen + * @version 1.0.1 + * @date 2024/1/14 14:32 + */ + @Bean + public IdentifierGenerator idGenerator() { + return entity -> IdUtils.getStandaloneId(); + } + + + /** + * 参数 自动 填充 + * + * @author WangChenChen + * @version 1.0.1 + * @date 2024/1/14 14:32 + */ @Slf4j @Component @AllArgsConstructor @@ -111,7 +133,6 @@ public class MybatisPlusConfig { } } - @Override public void updateFill(MetaObject metaObject) { if (JwtSecurityUtils.isAuthenticated()) { diff --git a/server/plugins/web-config/src/main/java/com/xaaef/molly/web/log/BindingResultAspect.java b/server/plugins/web-config/src/main/java/com/xaaef/molly/web/log/BindingResultAspect.java deleted file mode 100644 index e2231dc..0000000 --- a/server/plugins/web-config/src/main/java/com/xaaef/molly/web/log/BindingResultAspect.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.xaaef.molly.web.log; - -import com.xaaef.molly.common.util.JsonResult; -import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.springframework.stereotype.Component; -import org.springframework.validation.BindingResult; - -/** - *

- * Hibernate-Validator 错误结果处理切面 - *

- * - * @author Wang Chen Chen <932560435@qq.com> - * @version 2.0 - * @date 2019/4/18 11:45 - */ - -@Slf4j -@Aspect -@Component -public class BindingResultAspect { - - @Pointcut("execution(public * com.xaaef.molly.*.controller..*.*(..))") - public void BindingResult1() { - } - - @Pointcut("execution(public * com.xaaef.molly.controller..*.*(..))") - public void BindingResult2() { - } - - @Around("BindingResult1() || BindingResult2()") - public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { - var args = joinPoint.getArgs(); - for (Object arg : args) { - if (arg instanceof BindingResult result) { - if (result.hasErrors()) { - var fieldError = result.getFieldError(); - if (fieldError != null) { - return JsonResult.fail(fieldError.getDefaultMessage()); - } else { - return JsonResult.fail("请求参数错误!"); - } - } - } - } - return joinPoint.proceed(); - } - -} diff --git a/server/plugins/web-config/src/main/java/com/xaaef/molly/web/log/OperateLogAspect.java b/server/plugins/web-config/src/main/java/com/xaaef/molly/web/log/OperateLogAspect.java index 363b077..e3c2216 100644 --- a/server/plugins/web-config/src/main/java/com/xaaef/molly/web/log/OperateLogAspect.java +++ b/server/plugins/web-config/src/main/java/com/xaaef/molly/web/log/OperateLogAspect.java @@ -1,8 +1,8 @@ package com.xaaef.molly.web.log; import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.util.IdUtil; import com.xaaef.molly.auth.jwt.JwtSecurityUtils; +import com.xaaef.molly.common.util.IdUtils; import com.xaaef.molly.common.util.JsonResult; import com.xaaef.molly.common.util.ServletUtils; import com.xaaef.molly.common.util.TenantUtils; @@ -150,7 +150,7 @@ public class OperateLogAspect { operLog.setTenantId(userInfo.getTenantId()); } operLog.setCreateTime(LocalDateTime.now()); - operLog.setId(IdUtil.getSnowflakeNextIdStr()); + operLog.setId(IdUtils.getStandaloneStrId()); logStorage.asyncOperateSave(operLog); }