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