From c0ae832ace42fbb7683a54a4b54391f5376b6388 Mon Sep 17 00:00:00 2001 From: qiushengjie Date: Mon, 28 Aug 2023 14:26:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=BC=80=E5=A7=8B=E6=90=AC?= =?UTF-8?q?=E5=AE=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/move/GetProductInfoRequestDTO.java | 2 + .../java/com/ms/api/DsRequestService.java | 17 ++-- .../main/java/com/ms/api/bo/ConditionBO.java | 8 ++ .../spi/auth/GetPlatformAuthUrlService.java | 7 +- .../spi/move/SaveBatchMoveTaskService.java | 94 +++++++++++++++---- .../java/com/ms/api/tool/SecurityTool.java | 45 +++++++++ .../java/com/ms/api/tool/security/AES.java | 61 ++++++++++++ .../src/main/resources/application.properties | 1 + .../test/java/com/ms/test/SecurityTest.java | 13 +++ 9 files changed, 220 insertions(+), 28 deletions(-) create mode 100644 ms-biz/src/main/java/com/ms/api/bo/ConditionBO.java create mode 100644 ms-biz/src/main/java/com/ms/api/tool/SecurityTool.java create mode 100644 ms-biz/src/main/java/com/ms/api/tool/security/AES.java create mode 100644 ms-deploy/src/test/java/com/ms/test/SecurityTest.java diff --git a/ms-api/src/main/java/com/ms/api/dto/move/GetProductInfoRequestDTO.java b/ms-api/src/main/java/com/ms/api/dto/move/GetProductInfoRequestDTO.java index 4e63df58..d2a1af97 100644 --- a/ms-api/src/main/java/com/ms/api/dto/move/GetProductInfoRequestDTO.java +++ b/ms-api/src/main/java/com/ms/api/dto/move/GetProductInfoRequestDTO.java @@ -11,4 +11,6 @@ import java.util.List; public class GetProductInfoRequestDTO { // sourceItemUrls array 来源商品连接 是 + + } diff --git a/ms-biz/src/main/java/com/ms/api/DsRequestService.java b/ms-biz/src/main/java/com/ms/api/DsRequestService.java index cbdd2b0b..1ec59ec7 100644 --- a/ms-biz/src/main/java/com/ms/api/DsRequestService.java +++ b/ms-biz/src/main/java/com/ms/api/DsRequestService.java @@ -6,6 +6,8 @@ import com.jinritemai.cloud.base.api.ExtensionService; import com.jinritemai.cloud.base.api.ExtensionServiceHandler; import com.ms.api.dto.ItemDTO; import com.ms.api.tool.DsFormRequestTemplate; +import com.ms.api.tool.SecurityTool; +import com.ms.api.util.UniqueKeyMultiValueMap; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -21,13 +23,14 @@ public class DsRequestService implements ExtensionServiceHandler handle(BaseRequest req) { - Map params = new HashMap(); - params.put("platform", "ddMicroApp"); - - params.put("platformOrderId", "2332"); - params.put("shopId", "78327"); - params.put("operateInfo", "{\"userId\":\"2332\", \"ip\": \"123.21.123.123\"}"); - params.put("platformSkuId", "123232"); + UniqueKeyMultiValueMap params = new UniqueKeyMultiValueMap<>(); + params.add("platform", "ddMicroApp"); + params.add("platformOrderId", "2332"); + String shopId = SecurityTool.encodeByAES(String.valueOf(78327)); + log.info(shopId); + params.add("shopId", shopId); + params.add("operateInfo", "{\"userId\":\"2332\", \"ip\": \"123.21.123.123\"}"); + params.add("platformSkuId", "123232"); try { String res = dsFormRequestTemplate.execute("/order/cancelPurchaseOrderRelate", params); log.info(res); diff --git a/ms-biz/src/main/java/com/ms/api/bo/ConditionBO.java b/ms-biz/src/main/java/com/ms/api/bo/ConditionBO.java new file mode 100644 index 00000000..da543858 --- /dev/null +++ b/ms-biz/src/main/java/com/ms/api/bo/ConditionBO.java @@ -0,0 +1,8 @@ +package com.ms.api.bo; + +import lombok.Data; + +@Data +public class ConditionBO { + +} diff --git a/ms-biz/src/main/java/com/ms/api/spi/auth/GetPlatformAuthUrlService.java b/ms-biz/src/main/java/com/ms/api/spi/auth/GetPlatformAuthUrlService.java index 6c74253d..ef7b0a53 100644 --- a/ms-biz/src/main/java/com/ms/api/spi/auth/GetPlatformAuthUrlService.java +++ b/ms-biz/src/main/java/com/ms/api/spi/auth/GetPlatformAuthUrlService.java @@ -8,6 +8,7 @@ import com.ms.api.common.R; import com.ms.api.common.SPIBaseService; import com.ms.api.dto.order.GetPlatformAuthUrlRequestDTO; import com.ms.api.tool.DsFormRequestTemplate; +import com.ms.api.tool.SecurityTool; import com.ms.api.util.UniqueKeyMultiValueMap; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -33,10 +34,10 @@ public class GetPlatformAuthUrlService extends SPIBaseService implements Extensi // ----业务处理---- MultiValueMap params = new UniqueKeyMultiValueMap<>(); - params.add("userId",fields.getUserId()); // TODO 需要加密发送 - params.add("redirectUrl",fields.getRedirectUrl()); + params.add("userId", SecurityTool.encodeByAES(fields.getUserId())); + //params.add("redirectUrl",fields.getRedirectUrl()); params.add("platform","ddMicroApp"); - params.add("appName",fields.getAppName()); + //params.add("appName",fields.getAppName()); String res = null; try { res = dsFormRequestTemplate.execute("/platform_shop/checkDsAppAuthExpire", params); diff --git a/ms-biz/src/main/java/com/ms/api/spi/move/SaveBatchMoveTaskService.java b/ms-biz/src/main/java/com/ms/api/spi/move/SaveBatchMoveTaskService.java index 9ef96cf8..71ce878c 100644 --- a/ms-biz/src/main/java/com/ms/api/spi/move/SaveBatchMoveTaskService.java +++ b/ms-biz/src/main/java/com/ms/api/spi/move/SaveBatchMoveTaskService.java @@ -4,6 +4,7 @@ import com.jinritemai.cloud.base.api.BaseRequest; import com.jinritemai.cloud.base.api.BaseResponse; import com.jinritemai.cloud.base.api.ExtensionService; import com.jinritemai.cloud.base.api.ExtensionServiceHandler; +import com.ms.api.bo.ConditionBO; import com.ms.api.bo.ProductBO; import com.ms.api.common.R; import com.ms.api.common.SPIBaseService; @@ -49,7 +50,7 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio long shopId = 0L; //TODO 要从session中取 log.info("shopId "+shopId+" start saveTask"); - addCollectTask(shopId, "copyType", "name",null); +// addCollectTask(shopId, "copyType", "name",null); // ----结果返回---- return R.ok(); @@ -59,22 +60,80 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio } - private void addShopCollectTask(long shopId, List shopProductsList, String name, String copyType){ - // TODO 开启事务 +// private Map saveTaskByAuthShop(int shopId, String copyType, Map shopMoveConditionMap, Map shopMoveSettingOperateLogMap, Map shopConditions, Map shopProducts, Map shopPreviewCateAttr, String hostname, String operateSource, int isFromLtaoRank) { +// log.info("shopId " + shopId + " start saveMoveSetting"); +// +// Pair, List> result = saveMoveSetting(shopMoveConditionMap, shopMoveSettingOperateLogMap); +// List updateErrorShopIds = result.getFirst(); +// List moveShopSettingIds = result.getSecond(); +// +// zc.G("shopId " + shopId + " end saveMoveSetting"); +// +// if (!updateErrorShopIds.isEmpty()) { +// Map ret = CommonTool.failResult("店铺Id:" + String.join(" ", updateErrorShopIds) + ",保存搬家配置失败,请稍后再尝试"); +// ret.put("hostname", hostname); +// return ret; +// } +// +// String name; +// if (copyType.equals(MoveConst.COLLECT_LINKS_COPY)) { // Assuming you have a MoveConst class +// name = ZcDateHelper.now() + " 批量抓取"; // Assuming you have a ZcDateHelper class +// } else { +// name = ZcDateHelper.now().toString(); +// } +// +// zc.G("shopId " + shopId + " start replaceShopConditionsBigField"); +// shopConditions = moveService.replaceShopConditionsBigField(shopConditions, moveShopSettingIds); +// zc.G("shopId " + shopId + " end replaceShopConditionsBigField"); +// +// shopConditions = attachMoveSettingIdToMoveTaskCondition(moveShopSettingIds, shopConditions); +// moveBatchSaveTaskLog.info("shopId " + shopId + " start addShopCollectTask"); +// +// Map addRet = moveService.addShopCollectTask(shopId, shopProducts, name, copyType, shopConditions, shopPreviewCateAttr, operateSource, isFromLtaoRank); +// +// if (CommonTool.isFailRet(addRet)) { // Assuming you have a CommonTool class +// moveBatchSaveTaskLog.info("shopId " + shopId + " saveTask addRet fail" + zc.G().toString()); +// addRet.put("hostname", hostname); +// } +// +// return addRet; +// } +// +// private Pair,List> saveMoveSetting(Map shopMoveConditionMap, Map shopMoveSettingOperateLogMap) { +// } + + private Map addShopCollectTask(long shopId, Map> shopProductsList, String name, String copyType, Map shopConditions) { + // TODO 添加事务 +// int oldErrorMode = db.setErrorMode(ZcDb.ERROR_MODE_EXCEPTION); +// boolean transStatus = db.startTransaction(); + + int taskId = 0; + String cnaliDsDetailMap = null; try { - for (ProductBO product: shopProductsList) { -// this.addCollectTask(shopId,shopProductsList,copyType,name,); + for (Map.Entry> entry : shopProductsList.entrySet()) { + long authShopId = entry.getKey(); + Map addRet = this.addCollectTask(authShopId, shopId, copyType, name, shopConditions.get(authShopId), shopProductsList.get(authShopId)); + if (CommonTool.isSuccessRet(addRet) && authShopId == shopId) { + taskId = Integer.parseInt(addRet.get("taskId")); + cnaliDsDetailMap = addRet.get("cnaliDsDetailMap"); + } } - - }catch (Exception e){ - +// db.commit(transStatus); +// db.setErrorMode(oldErrorMode); + } catch (Exception ex) { +// moveLog.error(ex.getMessage()); +// db.rollback(transStatus); +// db.setErrorMode(oldErrorMode); + return CommonTool.failResult("添加任务数据失败"); } - // TODO 关闭事务 - + HashMap ret = new HashMap<>(); + ret.put("taskId", String.valueOf(taskId)); + ret.put("cnaliDsDetailMap", cnaliDsDetailMap); + return CommonTool.successResult(ret); } - private Map addCollectTask(long shopId, long operatorShopId, String copyType, String name, List products){ + private Map addCollectTask(long shopId, long operatorShopId, String copyType, String name, ConditionBO condition, Map products){ int productTotal = products.size(); // TODO 这边计算还有点内容 Map checkAndGetConfigIdRet = this.checkAvailableCopyAndGetMoveConfigId(shopId,productTotal); if(CommonTool.isFailRet(checkAndGetConfigIdRet)){ @@ -98,7 +157,8 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio log.info("INSERT move_collect task SQL:" + moveCollectTask.toString()); - for (ProductBO product: products) { + for (Map.Entry entry : products.entrySet()) { + ProductBO product = entry.getValue(); MoveCollectTaskDetail moveCollectTaskDetail = new MoveCollectTaskDetail(); moveCollectTaskDetail.setShopId(shopId); // moveCollectTaskDetail.setAuthShopId() @@ -130,12 +190,10 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio log.info("INSERT move_product_publish_buffer SQL"); } - return CommonTool.successResult( - new HashMap() {{ - put("taskId", String.valueOf(moveCollectTaskId)); - put("cnaliDsDetailMap", ""); - }} - ); + HashMap ret = new HashMap<>(); + ret.put("taskId", String.valueOf(moveCollectTaskId)); + ret.put("cnaliDsDetailMap", ""); + return CommonTool.successResult(ret); } diff --git a/ms-biz/src/main/java/com/ms/api/tool/SecurityTool.java b/ms-biz/src/main/java/com/ms/api/tool/SecurityTool.java new file mode 100644 index 00000000..1a3a9599 --- /dev/null +++ b/ms-biz/src/main/java/com/ms/api/tool/SecurityTool.java @@ -0,0 +1,45 @@ +package com.ms.api.tool; + +import com.ms.api.tool.security.AES; +import org.springframework.beans.factory.annotation.Value; + +import java.util.Base64; + +public class SecurityTool { + + public static String key = "D2E31857FB4C4C9439EDED69F5A85775"; + + /** + * AES加密 + */ + public static String encodeByAES(String context){ + byte[] aesByte; + try { + AES aes = new AES(key); + aesByte = aes.encode(context); + } catch (Exception e) { + throw new RuntimeException(e); + } + // 将ase_encode的结果base64_encode处理 + Base64.Encoder encode = Base64.getEncoder(); + return encode.encodeToString(aesByte); + } + + /** + * AES解密 + */ + public static String decodeByAES(String aesString){ + Base64.Decoder decoder = Base64.getDecoder(); + //将加密后的字符串先base64_decode处理 + byte[] aesByte = decoder.decode(aesString); + //解密 + String desString; + try { + AES aes = new AES(key); + desString = aes.decode(aesByte); + } catch (Exception e) { + throw new RuntimeException(e); + } + return desString; + } +} diff --git a/ms-biz/src/main/java/com/ms/api/tool/security/AES.java b/ms-biz/src/main/java/com/ms/api/tool/security/AES.java new file mode 100644 index 00000000..a4274dee --- /dev/null +++ b/ms-biz/src/main/java/com/ms/api/tool/security/AES.java @@ -0,0 +1,61 @@ +package com.ms.api.tool.security; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.security.SecureRandom; + +/** + * AES加解密 + * + * https://www.jianshu.com/p/13b7fd018f7c + */ +public class AES { + private String key; + + public AES(String key) + { + this.key = key; + } + + /** + * 加密 + * + * @param context + * @return + * @throws Exception + */ + public byte[] encode(String context) throws Exception + { + KeyGenerator kgen = KeyGenerator.getInstance("AES"); + + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); + secureRandom.setSeed(key.getBytes()); + kgen.init(128, secureRandom); + + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES")); + return cipher.doFinal(context.getBytes(StandardCharsets.UTF_8)); + } + + /** + * 解密 + * + * @param encryptBytes + * @return + * @throws Exception + */ + public String decode(byte[] encryptBytes) throws Exception + { + KeyGenerator kgen = KeyGenerator.getInstance("AES"); + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); + secureRandom.setSeed(key.getBytes()); + kgen.init(128, secureRandom); + + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES")); + byte[] decryptBytes = cipher.doFinal(encryptBytes); + return new String(decryptBytes, "UTF-8"); + } +} \ No newline at end of file diff --git a/ms-dal/src/main/resources/application.properties b/ms-dal/src/main/resources/application.properties index 10802dec..64af3154 100644 --- a/ms-dal/src/main/resources/application.properties +++ b/ms-dal/src/main/resources/application.properties @@ -17,3 +17,4 @@ jx.ds.host-prod=https://acn-ds.chengji-inc.com/open jx.ds.host-test=https://acn-ds-test.chengji-inc.com/open jx.ds.token-prod=dighgfghf^&)l3sedi&(.D@BC jx.ds.token-test=dighgfghf^&)l3sedi&(.D@BC + diff --git a/ms-deploy/src/test/java/com/ms/test/SecurityTest.java b/ms-deploy/src/test/java/com/ms/test/SecurityTest.java new file mode 100644 index 00000000..b7deec5b --- /dev/null +++ b/ms-deploy/src/test/java/com/ms/test/SecurityTest.java @@ -0,0 +1,13 @@ +package com.ms.test; + +import com.ms.api.tool.SecurityTool; + +public class SecurityTest { + public static void main(String[] args) { + String word = SecurityTool.encodeByAES("hello world!"); + System.out.println(word); + + String ss = SecurityTool.decodeByAES(word); + System.out.println(ss); + } +}