Browse Source

1、证书续期发布订阅同步

master
bjang03 6 months ago
parent
commit
1d0f84609a
  1. 5
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/ModuleAuthenInterceptor.java
  2. 4
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/framework/web/config/MyCommandLineRunner.java
  3. 4
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/job/ModuleLicencesJob.java
  4. 7
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/mq/consumer/licences/LicencesConsumer.java
  5. 4
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/licences/LicencesServiceImpl.java
  6. 17
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/util/ModuleAuthenUtils.java

5
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.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -17,6 +18,8 @@ import java.time.format.DateTimeFormatter;
@Slf4j @Slf4j
public class ModuleAuthenInterceptor implements HandlerInterceptor { public class ModuleAuthenInterceptor implements HandlerInterceptor {
@Resource
private ModuleAuthenUtils moduleAuthenUtils;
private MD5 md5 = MD5.create(); private MD5 md5 = MD5.create();
public int findThirdOccurrence(String str, char c) { public int findThirdOccurrence(String str, char c) {
int count = 0; int count = 0;
@ -34,7 +37,7 @@ public class ModuleAuthenInterceptor implements HandlerInterceptor {
String moduleName = request.getRequestURI().substring(0,findThirdOccurrence(request.getRequestURI(),'/')); String moduleName = request.getRequestURI().substring(0,findThirdOccurrence(request.getRequestURI(),'/'));
String endTimeStr = ModuleAuthenUtils.module.get(md5.digestHex(moduleName)); String endTimeStr = ModuleAuthenUtils.module.get(md5.digestHex(moduleName));
if(endTimeStr != null){ 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())){ if (endTime == null || endTime.isBefore(LocalDateTime.now())){
response.setContentType("application/json;charset=utf-8"); response.setContentType("application/json;charset=utf-8");
CommonResult result = new CommonResult<>(); CommonResult result = new CommonResult<>();

4
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 @Component
public class MyCommandLineRunner implements CommandLineRunner { public class MyCommandLineRunner implements CommandLineRunner {
@Resource @Resource
private RedisTemplate<String, String> redisTemplate; private ModuleAuthenUtils moduleAuthenUtils;
@Override @Override
public void run(String... args) { public void run(String... args) {
ModuleAuthenUtils.updateLicences(redisTemplate); moduleAuthenUtils.updateLicences();
} }
} }

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

7
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.framework.mq.core.stream.AbstractStreamMessageListener;
import com.win.module.system.mq.message.licences.LicencesMessage; import com.win.module.system.mq.message.licences.LicencesMessage;
import com.win.module.system.util.ModuleAuthenUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component @Component
public class LicencesConsumer extends AbstractStreamMessageListener<LicencesMessage>{ public class LicencesConsumer extends AbstractStreamMessageListener<LicencesMessage>{
@Resource
private ModuleAuthenUtils moduleAuthenUtils;
@Override @Override
public void onMessage(LicencesMessage message) { public void onMessage(LicencesMessage message) {
System.out.println(message); moduleAuthenUtils.updateLicences();
} }
} }

4
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 @Service
@Validated @Validated
public class LicencesServiceImpl implements LicencesService { public class LicencesServiceImpl implements LicencesService {
@Resource
private ModuleAuthenUtils moduleAuthenUtils;
@Resource @Resource
private LicencesProducer licencesProducer; private LicencesProducer licencesProducer;
@Resource @Resource
@ -82,7 +84,7 @@ public class LicencesServiceImpl implements LicencesService {
//todo 目前为前端传递的,后期应替换为从数据库读取 //todo 目前为前端传递的,后期应替换为从数据库读取
req.getModules().forEach(module -> { req.getModules().forEach(module -> {
try { try {
put(md5.digestHex(module.getModuleUri()),ModuleAuthenUtils.encrypt(module.getEndTime(), secretKey)); put(md5.digestHex(module.getModuleUri()),moduleAuthenUtils.encrypt(module.getEndTime(), secretKey));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

17
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.core.codec.Base64;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.win.module.system.mq.message.licences.LicencesMessage;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
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; import java.util.Map;
@Component
public class ModuleAuthenUtils { public class ModuleAuthenUtils {
public static final String LICENCES_REDIS_KEY = "system.licences"; public static final String LICENCES_REDIS_KEY = "system.licences";
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 Map<String,String> module; public static Map<String,String> 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); 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,String secretKey) throws Exception { public String encrypt(String data,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.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){ 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)); JSONObject json = new JSONObject(redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_REDIS_KEY));
ModuleAuthenUtils.secretKey = json.getStr("secretKey"); ModuleAuthenUtils.secretKey = json.getStr("secretKey");
ModuleAuthenUtils.module = json.getJSONObject("data").toBean(Map.class); ModuleAuthenUtils.module = json.getJSONObject("data").toBean(Map.class);

Loading…
Cancel
Save