diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/ModuleAuthenInterceptor.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/ModuleAuthenInterceptor.java index bd680f8..8c1d210 100644 --- a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/ModuleAuthenInterceptor.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/ModuleAuthenInterceptor.java @@ -9,6 +9,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; +import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; @@ -17,6 +18,8 @@ import java.time.format.DateTimeFormatter; @Slf4j public class ModuleAuthenInterceptor implements HandlerInterceptor { + @Resource + private ModuleAuthenUtils moduleAuthenUtils; private MD5 md5 = MD5.create(); public int findThirdOccurrence(String str, char c) { int count = 0; @@ -34,7 +37,7 @@ public class ModuleAuthenInterceptor implements HandlerInterceptor { 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")); + 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<>(); diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java index 4161e06..38f960e 100644 --- a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java @@ -10,9 +10,9 @@ import javax.annotation.Resource; @Component public class MyCommandLineRunner implements CommandLineRunner { @Resource - private RedisTemplate redisTemplate; + private ModuleAuthenUtils moduleAuthenUtils; @Override public void run(String... args) { - ModuleAuthenUtils.updateLicences(redisTemplate); + moduleAuthenUtils.updateLicences(); } } \ 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 index 766c3da..ade9d68 100644 --- 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 @@ -14,11 +14,11 @@ import javax.annotation.Resource; @Slf4j public class ModuleLicencesJob { @Resource - private RedisTemplate redisTemplate; + private ModuleAuthenUtils moduleAuthenUtils; @Scheduled(cron = "0 0/1 * * * ?") public void updateLicences(){ try{ - ModuleAuthenUtils.updateLicences(redisTemplate); + moduleAuthenUtils.updateLicences(); }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 index d99b008..eb36050 100644 --- 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 @@ -2,12 +2,17 @@ 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 com.win.module.system.util.ModuleAuthenUtils; import org.springframework.stereotype.Component; +import javax.annotation.Resource; + @Component public class LicencesConsumer extends AbstractStreamMessageListener{ + @Resource + private ModuleAuthenUtils moduleAuthenUtils; @Override public void onMessage(LicencesMessage message) { - System.out.println(message); + moduleAuthenUtils.updateLicences(); } } diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java index 578ed8f..d04cd20 100644 --- a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java @@ -28,6 +28,8 @@ import static com.win.module.system.enums.ErrorCodeConstants.ENCRYPTION_STR_FORM @Service @Validated public class LicencesServiceImpl implements LicencesService { + @Resource + private ModuleAuthenUtils moduleAuthenUtils; @Resource private LicencesProducer licencesProducer; @Resource @@ -82,7 +84,7 @@ public class LicencesServiceImpl implements LicencesService { //todo 目前为前端传递的,后期应替换为从数据库读取 req.getModules().forEach(module -> { try { - put(md5.digestHex(module.getModuleUri()),ModuleAuthenUtils.encrypt(module.getEndTime(), secretKey)); + put(md5.digestHex(module.getModuleUri()),moduleAuthenUtils.encrypt(module.getEndTime(), secretKey)); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/util/ModuleAuthenUtils.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/util/ModuleAuthenUtils.java index ab003c1..e4c3b72 100644 --- a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/util/ModuleAuthenUtils.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/util/ModuleAuthenUtils.java @@ -2,34 +2,43 @@ package com.win.module.system.util; import cn.hutool.core.codec.Base64; import cn.hutool.json.JSONObject; +import com.win.module.system.mq.message.licences.LicencesMessage; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; +import javax.annotation.Resource; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Map; - +@Component public class ModuleAuthenUtils { 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 Map module; - public static String decrypt(String encryptedData,String secretKey) throws Exception { + @Resource RedisTemplate redisTemplate; + public String decrypt(String encryptedData,String secretKey) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decryptedBytes = cipher.doFinal(Base64.decode(encryptedData)); return new String(decryptedBytes, StandardCharsets.UTF_8); } - public static String encrypt(String data,String secretKey) throws Exception { + public String encrypt(String data,String secretKey) throws Exception { 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){ + public void updateLicences(LicencesMessage ...message){ + if(message != null){ + ModuleAuthenUtils.secretKey = message[0].getSecretKey(); + ModuleAuthenUtils.module = message[0].getData(); + return; + } JSONObject json = new JSONObject(redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_REDIS_KEY)); ModuleAuthenUtils.secretKey = json.getStr("secretKey"); ModuleAuthenUtils.module = json.getJSONObject("data").toBean(Map.class);