Browse Source

1、增加服务器启动时从redis获取模块期限数据

master
bjang03 7 months ago
parent
commit
651aa80c1f
  1. 4
      win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java
  2. 10
      win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/filter/ModuleAuthenInterceptor.java
  3. 19
      win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/util/ModuleAuthenUtils.java
  4. 30
      win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java
  5. 21
      win-server/src/main/java/com/win/server/MyCommandLineRunner.java

4
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 @Override
public void addInterceptors(InterceptorRegistry registry) { 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(); MyI18nInterceptor myHandlerInterceptor = new MyI18nInterceptor();

10
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; package com.win.framework.web.core.filter;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.win.framework.web.core.util.ModuleAuthenUtils; import com.win.framework.web.core.util.ModuleAuthenUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -9,14 +10,19 @@ import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Slf4j @Slf4j
public class ModuleAuthenInterceptor implements HandlerInterceptor { public class ModuleAuthenInterceptor implements HandlerInterceptor {
private MD5 md5 = MD5.create();
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 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())){ if (endTime == null || endTime.isBefore(LocalDateTime.now())){
throw new Exception("权限到期或没有当前模块使用权限,请联系服务商缴费"); throw new Exception("权限到期或没有当前模块使用权限,请联系服务商缴费");
} }

19
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 cn.hutool.json.JSONObject;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
public class ModuleAuthenUtils { public class ModuleAuthenUtils {
private static final String ALGORITHM = "AES"; public static final String LICENCES_MODULES = "system.licences.modules";
private static final int KEY_SIZE = 128; 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 String secretKey;
public static JSONObject moduleExpire; public static JSONObject module;
public static String decrypt(String encryptedData) throws Exception { public static String decrypt(String encryptedData,String secretKey) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM); SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.decode(encryptedData)); byte[] decryptedBytes = cipher.doFinal(Base64.decode(encryptedData));
return new String(decryptedBytes, StandardCharsets.UTF_8); 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); SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ModuleAuthenUtils.ALGORITHM);
Cipher cipher = Cipher.getInstance(ModuleAuthenUtils.ALGORITHM); Cipher cipher = Cipher.getInstance(ModuleAuthenUtils.ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.encode(encryptedBytes); 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());
}
} }

30
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.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig; import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.win.framework.web.core.filter.ModuleAuthenInterceptor;
import com.win.framework.web.core.util.ModuleAuthenUtils; import com.win.framework.web.core.util.ModuleAuthenUtils;
import com.win.module.eam.controller.licences.vo.GenerateLicenceReqVO; import com.win.module.eam.controller.licences.vo.GenerateLicenceReqVO;
import com.win.module.eam.mq.message.LicencesMessage; 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 org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static com.win.framework.common.exception.util.ServiceExceptionUtil.exception; 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.eam.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_IS_ERROR;
@ -30,10 +25,9 @@ import static com.win.module.eam.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_
@Service @Service
@Validated @Validated
public class LicencesServiceImpl implements LicencesService { public class LicencesServiceImpl implements LicencesService {
public static final String LICENCES_UPDATE = "system.licences";
@Resource @Resource
private RedisTemplate<String, String> redisTemplate; private RedisTemplate<String, String> redisTemplate;
private static final int KEY_SIZE = 128;
@Override @Override
public void licencesDiscernByCodeImage(MultipartFile file) throws IOException { public void licencesDiscernByCodeImage(MultipartFile file) throws IOException {
licenceDiscernByCodeStr(QrCodeUtil.decode(file.getInputStream())); licenceDiscernByCodeStr(QrCodeUtil.decode(file.getInputStream()));
@ -51,20 +45,22 @@ public class LicencesServiceImpl implements LicencesService {
if (Util.isNullOrEmpty(tmpSecretKey)) { if (Util.isNullOrEmpty(tmpSecretKey)) {
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR);
} }
ModuleAuthenUtils.secretKey = tmpSecretKey;
String dataStr = codeJson.getStr("data"); String dataStr = codeJson.getStr("data");
if (Util.isNullOrEmpty(dataStr)) { if (Util.isNullOrEmpty(dataStr)) {
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR);
} }
//todo 需迁移至mq订阅逻辑中 //todo 需迁移至mq订阅逻辑中
JSONObject tmpJson = new JSONObject(ModuleAuthenUtils.decrypt(dataStr));
JSONObject tmpJson = new JSONObject(ModuleAuthenUtils.decrypt(dataStr,tmpSecretKey));
if (tmpJson.isEmpty()) { if (tmpJson.isEmpty()) {
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); 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拉取过滤路径 //todo 更新到redis并使用发布订阅通知其他pods拉取过滤路径
redisTemplate.opsForValue().set(LICENCES_UPDATE, ModuleAuthenUtils.moduleExpire.toString()); redisTemplate.convertAndSend(ModuleAuthenUtils.LICENCES_UPDATE_MESSAGE, ModuleAuthenUtils.module.toString());
redisTemplate.convertAndSend(LICENCES_UPDATE, ModuleAuthenUtils.moduleExpire.toString());
} catch (Exception e) { } catch (Exception e) {
throw exception(500, e); throw exception(500, e);
} }
@ -74,14 +70,16 @@ public class LicencesServiceImpl implements LicencesService {
public void generateLicence(GenerateLicenceReqVO req, HttpServletResponse response) throws Exception { public void generateLicence(GenerateLicenceReqVO req, HttpServletResponse response) throws Exception {
//todo 数据准备 //todo 数据准备
LicencesMessage licencesMessage = new LicencesMessage() {{ 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); setSecretKey(secretKey);
MD5 md5 = MD5.create(); MD5 md5 = MD5.create();
JSONObject modules = new JSONObject(){{ JSONObject modules = new JSONObject(){{
put(md5.digestHex("/MES"),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("/WMS"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00")); 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 生成二维码 //todo 生成二维码
QrConfig config = new QrConfig(300, 300); QrConfig config = new QrConfig(300, 300);

21
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<String, String> 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));
}
}
Loading…
Cancel
Save