From 651aa80c1f394a7260b50287e34e7cfd67b1658e Mon Sep 17 00:00:00 2001 From: bjang03 Date: Wed, 15 May 2024 15:00:54 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=A2=9E=E5=8A=A0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E5=90=AF=E5=8A=A8=E6=97=B6=E4=BB=8Eredis=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=A8=A1=E5=9D=97=E6=9C=9F=E9=99=90=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/config/WinWebAutoConfiguration.java | 4 +-- .../core/filter/ModuleAuthenInterceptor.java | 10 +++++-- .../web/core/util/ModuleAuthenUtils.java | 19 +++++------- .../service/licences/LicencesServiceImpl.java | 30 +++++++++---------- .../com/win/server/MyCommandLineRunner.java | 21 +++++++++++++ 5 files changed, 52 insertions(+), 32 deletions(-) create mode 100644 win-server/src/main/java/com/win/server/MyCommandLineRunner.java 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 3721a7c..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 @@ -151,8 +151,8 @@ public class WinWebAutoConfiguration implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { - // 模块权限拦截器 张斌 2024-05-15 10:29 -// registry.addInterceptor(new ModuleAuthenInterceptor()).addPathPatterns("/**"); + + 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 d0bfb6d..98a5b52 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 @@ -1,5 +1,6 @@ package com.win.framework.web.core.filter; +import cn.hutool.crypto.digest.MD5; import cn.hutool.json.JSONObject; import com.win.framework.web.core.util.ModuleAuthenUtils; import lombok.extern.slf4j.Slf4j; @@ -9,14 +10,19 @@ import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; @Slf4j public class ModuleAuthenInterceptor implements HandlerInterceptor { - + private MD5 md5 = MD5.create(); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - LocalDateTime endTime = LocalDateTime.parse(ModuleAuthenUtils.decrypt(ModuleAuthenUtils.moduleExpire.getStr(request.getRequestURI().split("/")[1],null))); + String endTimeStr = ModuleAuthenUtils.module.getStr(md5.digestHex(request.getRequestURI().split("/")[1]),null); + if(endTimeStr == null || "".equals(endTimeStr)){ + throw new Exception("没有当前模块使用权限,请联系服务商缴费开通"); + } + 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("权限到期或没有当前模块使用权限,请联系服务商缴费"); } 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 8187209..74a8fc5 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 @@ -4,33 +4,28 @@ import cn.hutool.core.codec.Base64; import cn.hutool.json.JSONObject; import javax.crypto.Cipher; -import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; -import java.security.NoSuchAlgorithmException; public class ModuleAuthenUtils { - private static final String ALGORITHM = "AES"; - private static final int KEY_SIZE = 128; + public static final String LICENCES_MODULES = "system.licences.modules"; + public static final String LICENCES_SECRET_KEY = "system.licences.secretKey"; + public static final String LICENCES_UPDATE_MESSAGE = "system.licences.message"; + public static final String ALGORITHM = "AES"; public static String secretKey; - public static JSONObject moduleExpire; - public static String decrypt(String encryptedData) throws Exception { + public static JSONObject module; + public static 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) throws Exception { + 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); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); return Base64.encode(encryptedBytes); } - public static String generateKey() throws NoSuchAlgorithmException { - KeyGenerator keyGenerator = KeyGenerator.getInstance(ModuleAuthenUtils.ALGORITHM); - keyGenerator.init(ModuleAuthenUtils.KEY_SIZE); - return Base64.encode(keyGenerator.generateKey().getEncoded()); - } } diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java index 2fc84e1..f7644b1 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java @@ -5,7 +5,6 @@ import cn.hutool.crypto.digest.MD5; import cn.hutool.extra.qrcode.QrCodeUtil; import cn.hutool.extra.qrcode.QrConfig; import cn.hutool.json.JSONObject; -import com.win.framework.web.core.filter.ModuleAuthenInterceptor; 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; @@ -16,13 +15,9 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import javax.crypto.Cipher; import javax.crypto.KeyGenerator; -import javax.crypto.SecretKey; -import javax.crypto.spec.SecretKeySpec; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.nio.charset.StandardCharsets; import static com.win.framework.common.exception.util.ServiceExceptionUtil.exception; import static com.win.module.eam.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_IS_ERROR; @@ -30,10 +25,9 @@ import static com.win.module.eam.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_ @Service @Validated public class LicencesServiceImpl implements LicencesService { - public static final String LICENCES_UPDATE = "system.licences"; @Resource private RedisTemplate redisTemplate; - + private static final int KEY_SIZE = 128; @Override public void licencesDiscernByCodeImage(MultipartFile file) throws IOException { licenceDiscernByCodeStr(QrCodeUtil.decode(file.getInputStream())); @@ -51,20 +45,22 @@ public class LicencesServiceImpl implements LicencesService { if (Util.isNullOrEmpty(tmpSecretKey)) { throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); } - ModuleAuthenUtils.secretKey = tmpSecretKey; String dataStr = codeJson.getStr("data"); if (Util.isNullOrEmpty(dataStr)) { throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); } //todo 需迁移至mq订阅逻辑中 - JSONObject tmpJson = new JSONObject(ModuleAuthenUtils.decrypt(dataStr)); + + JSONObject tmpJson = new JSONObject(ModuleAuthenUtils.decrypt(dataStr,tmpSecretKey)); if (tmpJson.isEmpty()) { throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); } - ModuleAuthenUtils.moduleExpire = tmpJson; + ModuleAuthenUtils.module = tmpJson; + + redisTemplate.opsForValue().set(ModuleAuthenUtils.LICENCES_MODULES, ModuleAuthenUtils.module.toString()); + redisTemplate.opsForValue().set(ModuleAuthenUtils.LICENCES_SECRET_KEY, tmpSecretKey); //todo 更新到redis并使用发布订阅通知其他pods拉取过滤路径 - redisTemplate.opsForValue().set(LICENCES_UPDATE, ModuleAuthenUtils.moduleExpire.toString()); - redisTemplate.convertAndSend(LICENCES_UPDATE, ModuleAuthenUtils.moduleExpire.toString()); + redisTemplate.convertAndSend(ModuleAuthenUtils.LICENCES_UPDATE_MESSAGE, ModuleAuthenUtils.module.toString()); } catch (Exception e) { throw exception(500, e); } @@ -74,14 +70,16 @@ public class LicencesServiceImpl implements LicencesService { public void generateLicence(GenerateLicenceReqVO req, HttpServletResponse response) throws Exception { //todo 数据准备 LicencesMessage licencesMessage = new LicencesMessage() {{ - String secretKey = ModuleAuthenUtils.generateKey(); + 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("/MES"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00")); - put(md5.digestHex("/WMS"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00")); + 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())); + setData(ModuleAuthenUtils.encrypt(modules.toString(), secretKey)); }}; //todo 生成二维码 QrConfig config = new QrConfig(300, 300); diff --git a/win-server/src/main/java/com/win/server/MyCommandLineRunner.java b/win-server/src/main/java/com/win/server/MyCommandLineRunner.java new file mode 100644 index 0000000..0a9ba73 --- /dev/null +++ b/win-server/src/main/java/com/win/server/MyCommandLineRunner.java @@ -0,0 +1,21 @@ +package com.win.server; + +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; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +@Component +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)); + } +} \ No newline at end of file