|
|
@ -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.crypto.digest.MD5; |
|
|
@ -6,9 +6,11 @@ import cn.hutool.extra.qrcode.QrCodeUtil; |
|
|
|
import cn.hutool.extra.qrcode.QrConfig; |
|
|
|
import cn.hutool.json.JSONObject; |
|
|
|
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; |
|
|
|
import com.win.module.system.controller.licences.vo.GenerateLicenceReqVO; |
|
|
|
import com.win.module.system.mq.producer.licences.LicencesProducer; |
|
|
|
import lombok.Data; |
|
|
|
import org.apache.calcite.util.Util; |
|
|
|
import org.springframework.data.redis.connection.stream.ObjectRecord; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.validation.annotation.Validated; |
|
|
@ -18,16 +20,26 @@ import javax.annotation.Resource; |
|
|
|
import javax.crypto.KeyGenerator; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
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.module.eam.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_IS_ERROR; |
|
|
|
import static com.win.module.system.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_IS_ERROR; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Validated |
|
|
|
public class LicencesServiceImpl implements LicencesService { |
|
|
|
@Resource |
|
|
|
private LicencesProducer licencesProducer; |
|
|
|
@Resource |
|
|
|
private RedisTemplate<String, String> redisTemplate; |
|
|
|
private MD5 md5 = MD5.create(); |
|
|
|
private static final int KEY_SIZE = 128; |
|
|
|
@Data |
|
|
|
class Licences{ |
|
|
|
private String secretKey; |
|
|
|
private Map data; |
|
|
|
} |
|
|
|
@Override |
|
|
|
public void licencesDiscernByCodeImage(MultipartFile file) throws IOException { |
|
|
|
licenceDiscernByCodeStr(QrCodeUtil.decode(file.getInputStream())); |
|
|
@ -40,27 +52,21 @@ public class LicencesServiceImpl implements LicencesService { |
|
|
|
} |
|
|
|
try { |
|
|
|
//todo 数据校验
|
|
|
|
JSONObject codeJson = new JSONObject(encryptionStr); |
|
|
|
String tmpSecretKey = codeJson.getStr("secretKey"); |
|
|
|
if (Util.isNullOrEmpty(tmpSecretKey)) { |
|
|
|
Licences licences = new JSONObject(encryptionStr).toBean(Licences.class); |
|
|
|
if (Util.isNullOrEmpty(licences.getSecretKey())) { |
|
|
|
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); |
|
|
|
} |
|
|
|
String dataStr = codeJson.getStr("data"); |
|
|
|
if (Util.isNullOrEmpty(dataStr)) { |
|
|
|
if (licences.getData() == null || licences.getData().isEmpty()) { |
|
|
|
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); |
|
|
|
} |
|
|
|
//todo 需迁移至mq订阅逻辑中
|
|
|
|
ModuleAuthenUtils.secretKey = licences.getSecretKey(); |
|
|
|
ModuleAuthenUtils.module = licences.getData(); |
|
|
|
|
|
|
|
JSONObject tmpJson = new JSONObject(ModuleAuthenUtils.decrypt(dataStr,tmpSecretKey)); |
|
|
|
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); |
|
|
|
redisTemplate.opsForValue().set(ModuleAuthenUtils.LICENCES_REDIS_KEY,new JSONObject(licences).toString()); |
|
|
|
//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) { |
|
|
|
throw exception(500, e); |
|
|
|
} |
|
|
@ -69,21 +75,25 @@ public class LicencesServiceImpl implements LicencesService { |
|
|
|
@Override |
|
|
|
public void generateLicence(GenerateLicenceReqVO req, HttpServletResponse response) throws Exception { |
|
|
|
//todo 数据准备
|
|
|
|
LicencesMessage licencesMessage = new LicencesMessage() {{ |
|
|
|
Licences licences = new Licences() {{ |
|
|
|
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("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(), secretKey)); |
|
|
|
setData(new HashMap(){{ |
|
|
|
//todo 目前为前端传递的,后期应替换为从数据库读取
|
|
|
|
req.getModules().forEach(module -> { |
|
|
|
try { |
|
|
|
put(md5.digestHex(module.getModuleUri()),ModuleAuthenUtils.encrypt(module.getEndTime(), secretKey)); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
}); |
|
|
|
}}); |
|
|
|
}}; |
|
|
|
//todo 生成二维码
|
|
|
|
QrConfig config = new QrConfig(300, 300); |
|
|
|
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()); |
|
|
|
} |
|
|
|
} |