|
|
@ -4,8 +4,9 @@ import cn.hutool.core.codec.Base64; |
|
|
|
import cn.hutool.crypto.digest.MD5; |
|
|
|
import cn.hutool.extra.qrcode.QrCodeUtil; |
|
|
|
import cn.hutool.extra.qrcode.QrConfig; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.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.module.eam.controller.licences.vo.GenerateLicenceReqVO; |
|
|
|
import com.win.module.eam.mq.message.LicencesMessage; |
|
|
|
import org.apache.calcite.util.Util; |
|
|
@ -22,8 +23,6 @@ import javax.crypto.spec.SecretKeySpec; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import static com.win.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
|
|
import static com.win.module.eam.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_IS_ERROR; |
|
|
@ -31,35 +30,10 @@ import static com.win.module.eam.enums.ErrorCodeConstants.ENCRYPTION_STR_FORMAT_ |
|
|
|
@Service |
|
|
|
@Validated |
|
|
|
public class LicencesServiceImpl implements LicencesService { |
|
|
|
private static final String ALGORITHM = "AES"; |
|
|
|
private static final int KEY_SIZE = 128; |
|
|
|
private static final String LICENCES_UPDATE = "LICENCES"; |
|
|
|
public static final String LICENCES_UPDATE = "system.licences"; |
|
|
|
@Resource |
|
|
|
private RedisTemplate<String, String> redisTemplate; |
|
|
|
|
|
|
|
private static String generateKey() throws Exception { |
|
|
|
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); |
|
|
|
keyGenerator.init(KEY_SIZE); |
|
|
|
SecretKey secretKey = keyGenerator.generateKey(); |
|
|
|
return Base64.encode(secretKey.getEncoded()); |
|
|
|
} |
|
|
|
|
|
|
|
private static String encrypt(String data, String key) throws Exception { |
|
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(key), ALGORITHM); |
|
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM); |
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); |
|
|
|
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); |
|
|
|
return Base64.encode(encryptedBytes); |
|
|
|
} |
|
|
|
|
|
|
|
private static String decrypt(String encryptedData, String key) throws Exception { |
|
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(key), ALGORITHM); |
|
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM); |
|
|
|
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); |
|
|
|
byte[] decryptedBytes = cipher.doFinal(Base64.decode(encryptedData)); |
|
|
|
return new String(decryptedBytes, StandardCharsets.UTF_8); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void licencesDiscernByCodeImage(MultipartFile file) throws IOException { |
|
|
|
licenceDiscernByCodeStr(QrCodeUtil.decode(file.getInputStream())); |
|
|
@ -72,22 +46,25 @@ public class LicencesServiceImpl implements LicencesService { |
|
|
|
} |
|
|
|
try { |
|
|
|
//todo 数据校验
|
|
|
|
JSONObject codeJson = JSONObject.parseObject(encryptionStr); |
|
|
|
String secretKey = codeJson.getString("secretKey"); |
|
|
|
if (Util.isNullOrEmpty(secretKey)) { |
|
|
|
JSONObject codeJson = new JSONObject(encryptionStr); |
|
|
|
String tmpSecretKey = codeJson.getStr("secretKey"); |
|
|
|
if (Util.isNullOrEmpty(tmpSecretKey)) { |
|
|
|
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); |
|
|
|
} |
|
|
|
String dataStr = codeJson.getString("data"); |
|
|
|
ModuleAuthenUtils.secretKey = tmpSecretKey; |
|
|
|
String dataStr = codeJson.getStr("data"); |
|
|
|
if (Util.isNullOrEmpty(dataStr)) { |
|
|
|
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); |
|
|
|
} |
|
|
|
JSONArray dataJson = JSONArray.parseArray(decrypt(dataStr, secretKey)); |
|
|
|
if (dataJson.isEmpty()) { |
|
|
|
//todo 需迁移至mq订阅逻辑中
|
|
|
|
JSONObject tmpJson = new JSONObject(ModuleAuthenUtils.decrypt(dataStr)); |
|
|
|
if (tmpJson.isEmpty()) { |
|
|
|
throw exception(ENCRYPTION_STR_FORMAT_IS_ERROR); |
|
|
|
} |
|
|
|
ModuleAuthenUtils.moduleExpire = tmpJson; |
|
|
|
//todo 更新到redis并使用发布订阅通知其他pods拉取过滤路径
|
|
|
|
redisTemplate.opsForValue().set(LICENCES_UPDATE, dataJson.toJSONString()); |
|
|
|
redisTemplate.convertAndSend(LICENCES_UPDATE, 1); |
|
|
|
redisTemplate.opsForValue().set(LICENCES_UPDATE, ModuleAuthenUtils.moduleExpire.toString()); |
|
|
|
redisTemplate.convertAndSend(LICENCES_UPDATE, ModuleAuthenUtils.moduleExpire.toString()); |
|
|
|
} catch (Exception e) { |
|
|
|
throw exception(500, e); |
|
|
|
} |
|
|
@ -95,22 +72,20 @@ public class LicencesServiceImpl implements LicencesService { |
|
|
|
|
|
|
|
@Override |
|
|
|
public void generateLicence(GenerateLicenceReqVO req, HttpServletResponse response) throws Exception { |
|
|
|
//todo 数据准备
|
|
|
|
LicencesMessage licencesMessage = new LicencesMessage() {{ |
|
|
|
String secretKey = generateKey(); |
|
|
|
String secretKey = ModuleAuthenUtils.generateKey(); |
|
|
|
setSecretKey(secretKey); |
|
|
|
List data = new ArrayList(); |
|
|
|
MD5 md5 = MD5.create(); |
|
|
|
//todo 从数据库表中取业主各子系统到期时间,路径通过拦截器获取第一层进行MD5验证,时间只能用对称加密解析后做对比
|
|
|
|
data.add(new JSONObject(){{ |
|
|
|
put(md5.digestHex("/MES"),encrypt("2025-10-10 00:00:00",secretKey)); |
|
|
|
}}); |
|
|
|
data.add(new JSONObject(){{ |
|
|
|
put(md5.digestHex("/WMS"),encrypt("2025-10-10 00:00:00",secretKey)); |
|
|
|
}}); |
|
|
|
setData(encrypt(JSONObject.toJSONString(data),secretKey)); |
|
|
|
JSONObject modules = new JSONObject(){{ |
|
|
|
put(md5.digestHex("/MES"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00")); |
|
|
|
put(md5.digestHex("/WMS"),ModuleAuthenUtils.encrypt("2025-10-10 00:00:00")); |
|
|
|
}}; |
|
|
|
setData(ModuleAuthenUtils.encrypt(modules.toString())); |
|
|
|
}}; |
|
|
|
//todo 生成二维码
|
|
|
|
QrConfig config = new QrConfig(300, 300); |
|
|
|
config.setErrorCorrection(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.M); // 设置纠错级别
|
|
|
|
QrCodeUtil.generate(JSONObject.toJSONString(licencesMessage), config, "", response.getOutputStream()); |
|
|
|
QrCodeUtil.generate(new JSONObject(licencesMessage).toString(), config, "", response.getOutputStream()); |
|
|
|
} |
|
|
|
} |
|
|
|