Browse Source

1、授权码接口迁移到正式应用场景

master
bjang03 6 months ago
parent
commit
adf6147e9d
  1. 4
      win-framework/win-spring-boot-starter-web/pom.xml
  2. 2
      win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java
  3. 32
      win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/filter/ModuleAuthenInterceptor.java
  4. 16
      win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/core/util/ModuleAuthenUtils.java
  5. 1
      win-module-eam/win-module-eam-api/src/main/java/com/win/module/eam/enums/ErrorCodeConstants.java
  6. 6
      win-module-eam/win-module-eam-biz/pom.xml
  7. 20
      win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/vo/GenerateLicenceReqVO.java
  8. 14
      win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/consumer/LicencesConsumer.java
  9. 10
      win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/message/LicencesMessage.java
  10. 1
      win-module-system/win-module-system-api/src/main/java/com/win/module/system/enums/ErrorCodeConstants.java
  11. 6
      win-module-system/win-module-system-biz/pom.xml
  12. 8
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/licences/LicencesController.java
  13. 30
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/licences/vo/GenerateLicenceReqVO.java
  14. 7
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java
  15. 26
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/job/ModuleLicencesJob.java
  16. 9
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/consumer/licences/LicencesConsumer.java
  17. 8
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/message/licences/LicencesMessage.java
  18. 30
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/producer/licences/LicencesProducer.java
  19. 5
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesService.java
  20. 62
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java

4
win-framework/win-spring-boot-starter-web/pom.xml

@ -60,6 +60,10 @@
<groupId>org.jsoup</groupId> <groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId> <artifactId>jsoup</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
</dependencies> </dependencies>

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

32
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.crypto.digest.MD5;
import cn.hutool.json.JSONObject; 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 com.win.framework.web.core.util.ModuleAuthenUtils;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; 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.io.OutputStream;
import java.io.PrintWriter;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@Slf4j @Slf4j
public class ModuleAuthenInterceptor implements HandlerInterceptor { public class ModuleAuthenInterceptor implements HandlerInterceptor {
private MD5 md5 = MD5.create(); 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 @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String moduleName = request.getRequestURI().split("/")[1]; String moduleName = request.getRequestURI().substring(0,findThirdOccurrence(request.getRequestURI(),'/'));
String endTimeStr = ModuleAuthenUtils.module.getStr(md5.digestHex(moduleName),null); String endTimeStr = ModuleAuthenUtils.module.get(md5.digestHex(moduleName));
if(endTimeStr == null || "".equals(endTimeStr)){ if(endTimeStr != null){
throw new Exception(String.format("没有【%s】模块使用权限,请联系服务商缴费开通",moduleName));
}
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())){ if (endTime == null || endTime.isBefore(LocalDateTime.now())){
throw new Exception(String.format("【%s】模块权限到期,请联系服务商缴费", moduleName)); 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; return true;
} }

16
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.core.codec.Base64;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map;
public class ModuleAuthenUtils { public class ModuleAuthenUtils {
public static final String LICENCES_MODULES = "system.licences.modules"; public static final String LICENCES_REDIS_KEY = "system.licences";
public static final String LICENCES_SECRET_KEY = "system.licences.secretKey";
public static final String LICENCES_UPDATE_MESSAGE = "system.licences.message"; public static final String LICENCES_UPDATE_MESSAGE = "system.licences.message";
public static final String ALGORITHM = "AES"; public static final String ALGORITHM = "AES";
public static String secretKey; public static String secretKey;
public static JSONObject module; public static Map<String,String> module;
public static String decrypt(String encryptedData,String secretKey) 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);
@ -22,10 +23,15 @@ public class ModuleAuthenUtils {
return new String(decryptedBytes, StandardCharsets.UTF_8); return new String(decryptedBytes, StandardCharsets.UTF_8);
} }
public static String encrypt(String data,String secretKey) 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), ALGORITHM);
Cipher cipher = Cipher.getInstance(ModuleAuthenUtils.ALGORITHM); Cipher cipher = Cipher.getInstance(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 void updateLicences(RedisTemplate<String, String> 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);
}
} }

1
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 LOCATION_EXISTS = new ErrorCode(1_000_020_021, "该库位已与备件绑定不可重复绑定");
ErrorCode ITEM_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, "授权码字符串格式错误");
} }

6
win-module-eam/win-module-eam-biz/pom.xml

@ -124,12 +124,6 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

20
win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/vo/GenerateLicenceReqVO.java

@ -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;
}

14
win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/consumer/LicencesConsumer.java

@ -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;
}

10
win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/mq/message/LicencesMessage.java

@ -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;
}

1
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_NOT_EXISTS = new ErrorCode(1_002_026_002, "系统参数为空,请重试");
ErrorCode CONFIG_IMPORT_LIST_IS_EMPTY = 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 DEPT_BUSI_TYPE_NOT_FOUND = new ErrorCode(1_002_026_002, "没有配置该部门的业务类型");
ErrorCode ENCRYPTION_STR_FORMAT_IS_ERROR = new ErrorCode(1_002_026_002, "授权码字符串格式错误");
} }

6
win-module-system/win-module-system-biz/pom.xml

@ -111,6 +111,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId> <artifactId>spring-boot-starter-mail</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

8
win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/licences/LicencesController.java → 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.framework.common.pojo.CommonResult;
import com.win.module.eam.controller.licences.vo.GenerateLicenceReqVO; import com.win.module.system.controller.licences.vo.GenerateLicenceReqVO;
import com.win.module.eam.service.licences.LicencesService; import com.win.module.system.service.licences.LicencesService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.Parameters;
@ -22,7 +22,7 @@ import static com.win.framework.common.pojo.CommonResult.success;
@Tag(name = "接口-续期许可") @Tag(name = "接口-续期许可")
@RestController @RestController
@RequestMapping("/eam/basic/licences") @RequestMapping("/system/licences")
@Validated @Validated
public class LicencesController { public class LicencesController {
@Resource @Resource

30
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> modules;
@Data
public static class Modules{
@NotNull(message = "模块路径前缀不能为空")
@Schema(description = "模块路径前缀")
private String moduleUri;
@NotNull(message = "到期时间不能为空")
@Schema(description = "到期时间")
private String endTime;
}
}

7
win-server/src/main/java/com/win/server/MyCommandLineRunner.java → 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 com.win.framework.web.core.util.ModuleAuthenUtils;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@ -12,10 +11,8 @@ import javax.annotation.Resource;
public class MyCommandLineRunner implements CommandLineRunner { public class MyCommandLineRunner implements CommandLineRunner {
@Resource @Resource
private RedisTemplate<String, String> redisTemplate; private RedisTemplate<String, String> redisTemplate;
@Override @Override
public void run(String... args) { public void run(String... args) {
ModuleAuthenUtils.secretKey = redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_SECRET_KEY); ModuleAuthenUtils.updateLicences(redisTemplate);
ModuleAuthenUtils.module = new JSONObject(redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_MODULES));
} }
} }

26
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<String, String> redisTemplate;
@Scheduled(cron = "0 0/1 * * * ?")
public void updateLicences(){
try{
ModuleAuthenUtils.updateLicences(redisTemplate);
}catch (Exception e){
log.error("更新模块证书发生异常",e);
}
}
}

9
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 {
}

8
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;
}

30
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);
}
}

5
win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesService.java → 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.system.controller.licences.vo.GenerateLicenceReqVO;
import com.win.module.eam.mq.message.LicencesMessage;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;

62
win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/licences/LicencesServiceImpl.java → 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.core.codec.Base64;
import cn.hutool.crypto.digest.MD5; 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.extra.qrcode.QrConfig;
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 com.win.module.eam.controller.licences.vo.GenerateLicenceReqVO; import com.win.module.system.controller.licences.vo.GenerateLicenceReqVO;
import com.win.module.eam.mq.message.LicencesMessage; import com.win.module.system.mq.producer.licences.LicencesProducer;
import lombok.Data;
import org.apache.calcite.util.Util; import org.apache.calcite.util.Util;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -18,16 +20,26 @@ import javax.annotation.Resource;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; 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.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 @Service
@Validated @Validated
public class LicencesServiceImpl implements LicencesService { public class LicencesServiceImpl implements LicencesService {
@Resource
private LicencesProducer licencesProducer;
@Resource @Resource
private RedisTemplate<String, String> redisTemplate; private RedisTemplate<String, String> redisTemplate;
private MD5 md5 = MD5.create();
private static final int KEY_SIZE = 128; private static final int KEY_SIZE = 128;
@Data
class Licences{
private String secretKey;
private Map data;
}
@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()));
@ -40,27 +52,21 @@ public class LicencesServiceImpl implements LicencesService {
} }
try { try {
//todo 数据校验 //todo 数据校验
JSONObject codeJson = new JSONObject(encryptionStr); Licences licences = new JSONObject(encryptionStr).toBean(Licences.class);
String tmpSecretKey = codeJson.getStr("secretKey"); if (Util.isNullOrEmpty(licences.getSecretKey())) {
if (Util.isNullOrEmpty(tmpSecretKey)) {
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR);
} }
String dataStr = codeJson.getStr("data"); if (licences.getData() == null || licences.getData().isEmpty()) {
if (Util.isNullOrEmpty(dataStr)) {
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR);
} }
//todo 需迁移至mq订阅逻辑中 //todo 需迁移至mq订阅逻辑中
ModuleAuthenUtils.secretKey = licences.getSecretKey();
ModuleAuthenUtils.module = licences.getData();
JSONObject tmpJson = new JSONObject(ModuleAuthenUtils.decrypt(dataStr,tmpSecretKey)); redisTemplate.opsForValue().set(ModuleAuthenUtils.LICENCES_REDIS_KEY,new JSONObject(licences).toString());
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);
//todo 更新到redis并使用发布订阅通知其他pods拉取过滤路径 //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) { } catch (Exception e) {
throw exception(500, e); throw exception(500, e);
} }
@ -69,21 +75,25 @@ public class LicencesServiceImpl implements LicencesService {
@Override @Override
public void generateLicence(GenerateLicenceReqVO req, HttpServletResponse response) throws Exception { public void generateLicence(GenerateLicenceReqVO req, HttpServletResponse response) throws Exception {
//todo 数据准备 //todo 数据准备
LicencesMessage licencesMessage = new LicencesMessage() {{ Licences licences = new Licences() {{
KeyGenerator keyGenerator = KeyGenerator.getInstance(ModuleAuthenUtils.ALGORITHM); KeyGenerator keyGenerator = KeyGenerator.getInstance(ModuleAuthenUtils.ALGORITHM);
keyGenerator.init(KEY_SIZE); keyGenerator.init(KEY_SIZE);
String secretKey = Base64.encode(keyGenerator.generateKey().getEncoded()); String secretKey = Base64.encode(keyGenerator.generateKey().getEncoded());
setSecretKey(secretKey); setSecretKey(secretKey);
MD5 md5 = MD5.create(); setData(new HashMap(){{
JSONObject modules = new JSONObject(){{ //todo 目前为前端传递的,后期应替换为从数据库读取
put(md5.digestHex("admin-api"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00", secretKey)); req.getModules().forEach(module -> {
put(md5.digestHex("mes"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00", secretKey)); try {
}}; put(md5.digestHex(module.getModuleUri()),ModuleAuthenUtils.encrypt(module.getEndTime(), secretKey));
setData(ModuleAuthenUtils.encrypt(modules.toString(), secretKey)); } catch (Exception e) {
throw new RuntimeException(e);
}
});
}});
}}; }};
//todo 生成二维码 //todo 生成二维码
QrConfig config = new QrConfig(300, 300); QrConfig config = new QrConfig(300, 300);
config.setErrorCorrection(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.M); // 设置纠错级别 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());
} }
} }
Loading…
Cancel
Save