diff --git a/win-framework/win-spring-boot-starter-web/pom.xml b/win-framework/win-spring-boot-starter-web/pom.xml index 29d5404..8712ed3 100644 --- a/win-framework/win-spring-boot-starter-web/pom.xml +++ b/win-framework/win-spring-boot-starter-web/pom.xml @@ -60,6 +60,10 @@ org.jsoup jsoup + + org.springframework.data + spring-data-redis + diff --git a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java index 77fe7c7..979d72d 100644 --- a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java +++ b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java @@ -152,7 +152,7 @@ public class WinWebAutoConfiguration implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { -// registry.addInterceptor(new ModuleAuthenInterceptor()).addPathPatterns("/**");// 模块权限拦截器 张斌 2024-05-15 10:29 + registry.addInterceptor(new ModuleAuthenInterceptor()).addPathPatterns("/**");// 模块权限拦截器 张斌 2024-05-15 10:29 // 注册拦截器 MyI18nInterceptor myHandlerInterceptor = new MyI18nInterceptor(); diff --git a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/filter/ModuleAuthenInterceptor.java b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/filter/ModuleAuthenInterceptor.java index ab9b902..50261a4 100644 --- a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/filter/ModuleAuthenInterceptor.java +++ b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/filter/ModuleAuthenInterceptor.java @@ -2,30 +2,50 @@ package com.win.framework.web.core.filter; import cn.hutool.crypto.digest.MD5; import cn.hutool.json.JSONObject; +import com.win.framework.common.exception.enums.GlobalErrorCodeConstants; +import com.win.framework.common.pojo.CommonResult; import com.win.framework.web.core.util.ModuleAuthenUtils; +import lombok.Cleanup; import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.OutputStream; +import java.io.PrintWriter; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @Slf4j public class ModuleAuthenInterceptor implements HandlerInterceptor { private MD5 md5 = MD5.create(); + public int findThirdOccurrence(String str, char c) { + int count = 0; + int index = -1; + while(count < 4) { + index = str.indexOf(c, index + 1); + count++; + } + + return index+1; + } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String moduleName = request.getRequestURI().split("/")[1]; - String endTimeStr = ModuleAuthenUtils.module.getStr(md5.digestHex(moduleName),null); - if(endTimeStr == null || "".equals(endTimeStr)){ - throw new Exception(String.format("没有【%s】模块使用权限,请联系服务商缴费开通",moduleName)); - } - LocalDateTime endTime = LocalDateTime.parse(ModuleAuthenUtils.decrypt(endTimeStr,ModuleAuthenUtils.secretKey), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); - if (endTime == null || endTime.isBefore(LocalDateTime.now())){ - throw new Exception(String.format("【%s】模块权限到期,请联系服务商缴费", moduleName)); + String moduleName = request.getRequestURI().substring(0,findThirdOccurrence(request.getRequestURI(),'/')); + String endTimeStr = ModuleAuthenUtils.module.get(md5.digestHex(moduleName)); + if(endTimeStr != null){ + LocalDateTime endTime = LocalDateTime.parse(ModuleAuthenUtils.decrypt(endTimeStr,ModuleAuthenUtils.secretKey), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + if (endTime == null || endTime.isBefore(LocalDateTime.now())){ + response.setContentType("application/json;charset=utf-8"); + CommonResult result = new CommonResult<>(); + result.setCode(500); + result.setMsg(String.format("【%s】模块权限到期,请联系服务商缴费", moduleName)); + @Cleanup PrintWriter os = response.getWriter(); + os.write(new JSONObject(result).toString()); + return false; + } } return true; } diff --git a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/util/ModuleAuthenUtils.java b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/util/ModuleAuthenUtils.java index 74a8fc5..f0d9095 100644 --- a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/util/ModuleAuthenUtils.java +++ b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/util/ModuleAuthenUtils.java @@ -2,18 +2,19 @@ package com.win.framework.web.core.util; import cn.hutool.core.codec.Base64; import cn.hutool.json.JSONObject; +import org.springframework.data.redis.core.RedisTemplate; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; +import java.util.Map; public class ModuleAuthenUtils { - public static final String LICENCES_MODULES = "system.licences.modules"; - public static final String LICENCES_SECRET_KEY = "system.licences.secretKey"; + public static final String LICENCES_REDIS_KEY = "system.licences"; public static final String LICENCES_UPDATE_MESSAGE = "system.licences.message"; public static final String ALGORITHM = "AES"; public static String secretKey; - public static JSONObject module; + public static Map module; public static String decrypt(String encryptedData,String secretKey) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); @@ -22,10 +23,15 @@ public class ModuleAuthenUtils { return new String(decryptedBytes, StandardCharsets.UTF_8); } public static String encrypt(String data,String secretKey) throws Exception { - SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ModuleAuthenUtils.ALGORITHM); - Cipher cipher = Cipher.getInstance(ModuleAuthenUtils.ALGORITHM); + SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM); + Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); return Base64.encode(encryptedBytes); } + public static void updateLicences(RedisTemplate redisTemplate){ + JSONObject json = new JSONObject(redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_REDIS_KEY)); + ModuleAuthenUtils.secretKey = json.getStr("secretKey"); + ModuleAuthenUtils.module = json.getJSONObject("data").toBean(Map.class); + } } diff --git a/win-module-eam/win-module-eam-api/src/main/java/com/win/module/eam/enums/ErrorCodeConstants.java b/win-module-eam/win-module-eam-api/src/main/java/com/win/module/eam/enums/ErrorCodeConstants.java index ebf65ec..26d7913 100644 --- a/win-module-eam/win-module-eam-api/src/main/java/com/win/module/eam/enums/ErrorCodeConstants.java +++ b/win-module-eam/win-module-eam-api/src/main/java/com/win/module/eam/enums/ErrorCodeConstants.java @@ -167,6 +167,5 @@ public interface ErrorCodeConstants { ErrorCode LOCATION_EXISTS = new ErrorCode(1_000_020_021, "该库位已与备件绑定不可重复绑定"); ErrorCode ITEM_EXISTS = new ErrorCode(1_000_020_021, "该备件已绑定不可重复绑定"); - ErrorCode ENCRYPTION_STR_FORMAT_IS_ERROR = new ErrorCode(1_000_020_064, "授权码字符串格式错误"); } diff --git a/win-module-eam/win-module-eam-biz/pom.xml b/win-module-eam/win-module-eam-biz/pom.xml index 77d6dbd..a501a4f 100644 --- a/win-module-eam/win-module-eam-biz/pom.xml +++ b/win-module-eam/win-module-eam-biz/pom.xml @@ -124,12 +124,6 @@ com.alibaba fastjson - - com.google.zxing - core - 3.3.1 - compile - diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/vo/GenerateLicenceReqVO.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/vo/GenerateLicenceReqVO.java deleted file mode 100644 index 693728d..0000000 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/vo/GenerateLicenceReqVO.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.win.module.eam.controller.licences.vo; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.ToString; - -import javax.validation.constraints.NotNull; - -@Schema(description = "管理后台 - 生成续期码 Request VO") -@Data -@ToString(callSuper = true) -public class GenerateLicenceReqVO { - @NotNull(message = "公司编码不能为空") - @Schema(description = "公司编码") - private String companyCode; - - @NotNull(message = "到期时间不能为空") - @Schema(description = "到期时间") - private String endTime; -} diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/consumer/LicencesConsumer.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/consumer/LicencesConsumer.java deleted file mode 100644 index 88ae477..0000000 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/consumer/LicencesConsumer.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.win.module.eam.mq.consumer; - -import com.alibaba.fastjson.JSONObject; -import com.win.framework.mq.core.stream.AbstractStreamMessageListener; -import com.win.module.eam.mq.message.LicencesMessage; -import com.win.module.eam.service.licences.LicencesService; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; -@Component -public class LicencesConsumer { - @Resource - private LicencesService licencesService; -} diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/message/LicencesMessage.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/message/LicencesMessage.java deleted file mode 100644 index 55d5be7..0000000 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/message/LicencesMessage.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.win.module.eam.mq.message; -import lombok.Data; -import lombok.ToString; - -@Data -@ToString(callSuper = true) -public class LicencesMessage{ - private String secretKey; - private String data; -} \ No newline at end of file diff --git a/win-module-system/win-module-system-api/src/main/java/com/win/module/system/enums/ErrorCodeConstants.java b/win-module-system/win-module-system-api/src/main/java/com/win/module/system/enums/ErrorCodeConstants.java index 4d918bd..77d9ee1 100644 --- a/win-module-system/win-module-system-api/src/main/java/com/win/module/system/enums/ErrorCodeConstants.java +++ b/win-module-system/win-module-system-api/src/main/java/com/win/module/system/enums/ErrorCodeConstants.java @@ -174,6 +174,7 @@ public interface ErrorCodeConstants { ErrorCode CONFIG_NOT_EXISTS = new ErrorCode(1_002_026_002, "系统参数为空,请重试"); ErrorCode CONFIG_IMPORT_LIST_IS_EMPTY = new ErrorCode(1_002_026_002, "导入参数为空,请重试"); ErrorCode DEPT_BUSI_TYPE_NOT_FOUND = new ErrorCode(1_002_026_002, "没有配置该部门的业务类型"); + ErrorCode ENCRYPTION_STR_FORMAT_IS_ERROR = new ErrorCode(1_002_026_002, "授权码字符串格式错误"); } diff --git a/win-module-system/win-module-system-biz/pom.xml b/win-module-system/win-module-system-biz/pom.xml index e59ebf1..86cc3d6 100644 --- a/win-module-system/win-module-system-biz/pom.xml +++ b/win-module-system/win-module-system-biz/pom.xml @@ -111,6 +111,12 @@ org.springframework.boot spring-boot-starter-mail + + com.google.zxing + core + 3.3.1 + compile + diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/LicencesController.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/licences/LicencesController.java similarity index 88% rename from win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/LicencesController.java rename to win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/licences/LicencesController.java index d7bbfa1..2a63d4a 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/LicencesController.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/licences/LicencesController.java @@ -1,8 +1,8 @@ -package com.win.module.eam.controller.licences; +package com.win.module.system.controller.licences; import com.win.framework.common.pojo.CommonResult; -import com.win.module.eam.controller.licences.vo.GenerateLicenceReqVO; -import com.win.module.eam.service.licences.LicencesService; +import com.win.module.system.controller.licences.vo.GenerateLicenceReqVO; +import com.win.module.system.service.licences.LicencesService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; @@ -22,7 +22,7 @@ import static com.win.framework.common.pojo.CommonResult.success; @Tag(name = "接口-续期许可") @RestController -@RequestMapping("/eam/basic/licences") +@RequestMapping("/system/licences") @Validated public class LicencesController { @Resource @@ -53,7 +53,7 @@ public class LicencesController { @PostMapping("/generateLicence") @Operation(summary = "生成续期二维码") @PreAuthorize("@ss.hasPermission('basic:licences:licenceDiscern')") - public void generateLicence(@Valid @RequestBody GenerateLicenceReqVO req,HttpServletResponse response) throws Exception { + public void generateLicence(@Valid @RequestBody GenerateLicenceReqVO req, HttpServletResponse response) throws Exception { licencesService.generateLicence(req,response); } } diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/licences/vo/GenerateLicenceReqVO.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/licences/vo/GenerateLicenceReqVO.java new file mode 100644 index 0000000..79ac338 --- /dev/null +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/licences/vo/GenerateLicenceReqVO.java @@ -0,0 +1,30 @@ +package com.win.module.system.controller.licences.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.ToString; + +import javax.validation.constraints.NotNull; +import java.util.List; + +@Schema(description = "管理后台 - 生成续期码 Request VO") +@Data +@ToString(callSuper = true) +public class GenerateLicenceReqVO { + @NotNull(message = "公司编码不能为空") + @Schema(description = "公司编码") + private String companyCode; + @NotNull(message = "模块授权信息不能为空") + @Schema(description = "模块授权信息") + private List modules; + @Data + public static class Modules{ + @NotNull(message = "模块路径前缀不能为空") + @Schema(description = "模块路径前缀") + private String moduleUri; + @NotNull(message = "到期时间不能为空") + @Schema(description = "到期时间") + private String endTime; + } + +} diff --git a/win-server/src/main/java/com/win/server/MyCommandLineRunner.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java similarity index 61% rename from win-server/src/main/java/com/win/server/MyCommandLineRunner.java rename to win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java index 0a9ba73..4fc2f49 100644 --- a/win-server/src/main/java/com/win/server/MyCommandLineRunner.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java @@ -1,6 +1,5 @@ -package com.win.server; +package com.win.module.system.framework.web.config; -import cn.hutool.json.JSONObject; import com.win.framework.web.core.util.ModuleAuthenUtils; import org.springframework.boot.CommandLineRunner; import org.springframework.data.redis.core.RedisTemplate; @@ -12,10 +11,8 @@ import javax.annotation.Resource; public class MyCommandLineRunner implements CommandLineRunner { @Resource private RedisTemplate redisTemplate; - @Override public void run(String... args) { - ModuleAuthenUtils.secretKey = redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_SECRET_KEY); - ModuleAuthenUtils.module = new JSONObject(redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_MODULES)); + ModuleAuthenUtils.updateLicences(redisTemplate); } } \ No newline at end of file diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/job/ModuleLicencesJob.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/job/ModuleLicencesJob.java new file mode 100644 index 0000000..182cc40 --- /dev/null +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/job/ModuleLicencesJob.java @@ -0,0 +1,26 @@ +package com.win.module.system.job; + +import com.win.framework.web.core.util.ModuleAuthenUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +@Component +@EnableScheduling +@Slf4j +public class ModuleLicencesJob { + @Resource + private RedisTemplate redisTemplate; + @Scheduled(cron = "0 0/1 * * * ?") + public void updateLicences(){ + try{ + ModuleAuthenUtils.updateLicences(redisTemplate); + }catch (Exception e){ + log.error("更新模块证书发生异常",e); + } + } +} diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/consumer/licences/LicencesConsumer.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/consumer/licences/LicencesConsumer.java new file mode 100644 index 0000000..d136221 --- /dev/null +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/consumer/licences/LicencesConsumer.java @@ -0,0 +1,9 @@ +package com.win.module.system.mq.consumer.licences; + +import com.win.framework.mq.core.stream.AbstractStreamMessageListener; +import com.win.module.system.mq.message.licences.LicencesMessage; +import org.springframework.stereotype.Component; + +@Component +public class LicencesConsumer { +} diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/message/licences/LicencesMessage.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/message/licences/LicencesMessage.java new file mode 100644 index 0000000..91f9ecd --- /dev/null +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/message/licences/LicencesMessage.java @@ -0,0 +1,8 @@ +package com.win.module.system.mq.message.licences; + +import com.win.framework.mq.core.stream.AbstractStreamMessage; + +public class LicencesMessage{ + private String secretKey; + private String data; +} diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/producer/licences/LicencesProducer.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/producer/licences/LicencesProducer.java new file mode 100644 index 0000000..c79971f --- /dev/null +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/producer/licences/LicencesProducer.java @@ -0,0 +1,30 @@ +package com.win.module.system.mq.producer.licences; + +import com.win.framework.mq.core.RedisMQTemplate; +import com.win.module.system.mq.message.mail.MailSendMessage; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * 模块权限 Producer + * + * @author 张斌 + * @since 202/5/16 + */ +@Slf4j +@Component +public class LicencesProducer { + + @Resource + private RedisMQTemplate redisMQTemplate; + public void sendMessage(Long sendLogId, String mail, Long accountId, + String nickname, String title, String content) { + MailSendMessage message = new MailSendMessage() + .setLogId(sendLogId).setMail(mail).setAccountId(accountId) + .setNickname(nickname).setTitle(title).setContent(content); + redisMQTemplate.send(message); + } + +} diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesService.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesService.java similarity index 75% rename from win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesService.java rename to win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesService.java index 0dfda8e..768714b 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesService.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesService.java @@ -1,7 +1,6 @@ -package com.win.module.eam.service.licences; +package com.win.module.system.service.licences; -import com.win.module.eam.controller.licences.vo.GenerateLicenceReqVO; -import com.win.module.eam.mq.message.LicencesMessage; +import com.win.module.system.controller.licences.vo.GenerateLicenceReqVO; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java similarity index 56% rename from win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java rename to win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java index f7644b1..0ea62b3 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java @@ -1,4 +1,4 @@ -package com.win.module.eam.service.licences; +package com.win.module.system.service.licences; import cn.hutool.core.codec.Base64; import cn.hutool.crypto.digest.MD5; @@ -6,9 +6,11 @@ import cn.hutool.extra.qrcode.QrCodeUtil; import cn.hutool.extra.qrcode.QrConfig; import cn.hutool.json.JSONObject; import com.win.framework.web.core.util.ModuleAuthenUtils; -import com.win.module.eam.controller.licences.vo.GenerateLicenceReqVO; -import com.win.module.eam.mq.message.LicencesMessage; +import com.win.module.system.controller.licences.vo.GenerateLicenceReqVO; +import com.win.module.system.mq.producer.licences.LicencesProducer; +import lombok.Data; import org.apache.calcite.util.Util; +import org.springframework.data.redis.connection.stream.ObjectRecord; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -18,16 +20,26 @@ import javax.annotation.Resource; import javax.crypto.KeyGenerator; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import static com.win.framework.common.exception.util.ServiceExceptionUtil.exception; -import static com.win.module.eam.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_IS_ERROR; +import static com.win.module.system.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_IS_ERROR; @Service @Validated public class LicencesServiceImpl implements LicencesService { + @Resource + private LicencesProducer licencesProducer; @Resource private RedisTemplate redisTemplate; + private MD5 md5 = MD5.create(); private static final int KEY_SIZE = 128; + @Data + class Licences{ + private String secretKey; + private Map data; + } @Override public void licencesDiscernByCodeImage(MultipartFile file) throws IOException { licenceDiscernByCodeStr(QrCodeUtil.decode(file.getInputStream())); @@ -40,27 +52,21 @@ public class LicencesServiceImpl implements LicencesService { } try { //todo 数据校验 - JSONObject codeJson = new JSONObject(encryptionStr); - String tmpSecretKey = codeJson.getStr("secretKey"); - if (Util.isNullOrEmpty(tmpSecretKey)) { + Licences licences = new JSONObject(encryptionStr).toBean(Licences.class); + if (Util.isNullOrEmpty(licences.getSecretKey())) { throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); } - String dataStr = codeJson.getStr("data"); - if (Util.isNullOrEmpty(dataStr)) { + if (licences.getData() == null || licences.getData().isEmpty()) { throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); } //todo 需迁移至mq订阅逻辑中 + ModuleAuthenUtils.secretKey = licences.getSecretKey(); + ModuleAuthenUtils.module = licences.getData(); - JSONObject tmpJson = new JSONObject(ModuleAuthenUtils.decrypt(dataStr,tmpSecretKey)); - if (tmpJson.isEmpty()) { - throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); - } - ModuleAuthenUtils.module = tmpJson; - - redisTemplate.opsForValue().set(ModuleAuthenUtils.LICENCES_MODULES, ModuleAuthenUtils.module.toString()); - redisTemplate.opsForValue().set(ModuleAuthenUtils.LICENCES_SECRET_KEY, tmpSecretKey); + redisTemplate.opsForValue().set(ModuleAuthenUtils.LICENCES_REDIS_KEY,new JSONObject(licences).toString()); //todo 更新到redis并使用发布订阅通知其他pods拉取过滤路径 - redisTemplate.convertAndSend(ModuleAuthenUtils.LICENCES_UPDATE_MESSAGE, ModuleAuthenUtils.module.toString()); + redisTemplate.opsForStream().add(ObjectRecord.create("stream:queue:licences",1)); +// redisTemplate.convertAndSend(ModuleAuthenUtils.LICENCES_UPDATE_MESSAGE, ModuleAuthenUtils.module.toString()); } catch (Exception e) { throw exception(500, e); } @@ -69,21 +75,25 @@ public class LicencesServiceImpl implements LicencesService { @Override public void generateLicence(GenerateLicenceReqVO req, HttpServletResponse response) throws Exception { //todo 数据准备 - LicencesMessage licencesMessage = new LicencesMessage() {{ + Licences licences = new Licences() {{ KeyGenerator keyGenerator = KeyGenerator.getInstance(ModuleAuthenUtils.ALGORITHM); keyGenerator.init(KEY_SIZE); String secretKey = Base64.encode(keyGenerator.generateKey().getEncoded()); setSecretKey(secretKey); - MD5 md5 = MD5.create(); - JSONObject modules = new JSONObject(){{ - put(md5.digestHex("admin-api"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00", secretKey)); - put(md5.digestHex("mes"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00", secretKey)); - }}; - setData(ModuleAuthenUtils.encrypt(modules.toString(), secretKey)); + setData(new HashMap(){{ + //todo 目前为前端传递的,后期应替换为从数据库读取 + req.getModules().forEach(module -> { + try { + put(md5.digestHex(module.getModuleUri()),ModuleAuthenUtils.encrypt(module.getEndTime(), secretKey)); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + }}); }}; //todo 生成二维码 QrConfig config = new QrConfig(300, 300); config.setErrorCorrection(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.M); // 设置纠错级别 - QrCodeUtil.generate(new JSONObject(licencesMessage).toString(), config, "", response.getOutputStream()); + QrCodeUtil.generate(new JSONObject(licences).toString(), config, "", response.getOutputStream()); } }