From a81291f3c007825d05bb7a6012d2e0ea3051e386 Mon Sep 17 00:00:00 2001 From: yqy Date: Mon, 17 Jan 2022 16:09:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/baiye/util/AESUtils.java | 101 ++++++++++++++++++ .../java/com/baiye/util/JpaConverterAes.java | 24 +++++ .../java/com/baiye/model/entity/BaseClue.java | 2 + .../controller/UploadFileController.java | 2 +- .../com/baiye/module/entity/ClueMiddle.java | 2 +- .../module/service/impl/ClueServiceImpl.java | 8 +- .../service/impl/UploadFileServiceImpl.java | 1 - .../java/com/baiye/util/ExportExcelUtil.java | 1 + .../src/main/resources/application.yml | 4 + .../src/main/resources/logback.xml | 9 +- 10 files changed, 139 insertions(+), 15 deletions(-) create mode 100644 ad-platform-common/src/main/java/com/baiye/util/AESUtils.java create mode 100644 ad-platform-common/src/main/java/com/baiye/util/JpaConverterAes.java diff --git a/ad-platform-common/src/main/java/com/baiye/util/AESUtils.java b/ad-platform-common/src/main/java/com/baiye/util/AESUtils.java new file mode 100644 index 00000000..7a356289 --- /dev/null +++ b/ad-platform-common/src/main/java/com/baiye/util/AESUtils.java @@ -0,0 +1,101 @@ +package com.baiye.util; + +import lombok.extern.slf4j.Slf4j; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import java.nio.charset.StandardCharsets; +import java.security.SecureRandom; +import java.util.Base64; + +/** + * AES加解密 + */ +@Slf4j +public class AESUtils { + + /** 密钥长度: 128, 192 or 256 */ + private static final int KEY_SIZE = 128; + + /** 加密/解密算法名称 */ + private static final String ALGORITHM = "AES"; + + /** 随机数生成器(RNG)算法名称 */ + private static final String RNG_ALGORITHM = "SHA1PRNG"; + + /** + * 生成密钥对象 + */ + private static SecretKey generateKey(byte[] key) throws Exception { + // 创建安全随机数生成器 + SecureRandom random = SecureRandom.getInstance(RNG_ALGORITHM); + // 设置 密钥key的字节数组 作为安全随机数生成器的种子 + random.setSeed(key); + + // 创建 AES算法生成器 + KeyGenerator gen = KeyGenerator.getInstance(ALGORITHM); + // 初始化算法生成器 + gen.init(KEY_SIZE, random); + + // 生成 AES密钥对象, 也可以直接创建密钥对象: return new SecretKeySpec(key, ALGORITHM); + return gen.generateKey(); + } + + /** + * 数据加密: 明文 -> 密文 + */ + public static String encrypt(String plainBytes, String key) { + try { + // 生成密钥对象 + SecretKey secKey = generateKey(key.getBytes(StandardCharsets.UTF_8)); + + // 获取 AES 密码器 + Cipher cipher = Cipher.getInstance(ALGORITHM); + // 初始化密码器(加密模型) + cipher.init(Cipher.ENCRYPT_MODE, secKey); + + //数据反转加密 + String s = new StringBuilder(plainBytes).reverse().toString(); + // 加密数据, 返回密文 + byte[] cipherBytes = cipher.doFinal(s.getBytes(StandardCharsets.UTF_8)); + + //base64加密 + String str = Base64.getEncoder().encodeToString(cipherBytes); + return str; + }catch (Exception e){ + log.info("加密失败"+e.fillInStackTrace()); + return null; + } + + } + + /** + * 数据解密: 密文 -> 明文 + */ + public static String decrypt(String cipherBytes, String key){ + try { + // 生成密钥对象 + SecretKey secKey = generateKey(key.getBytes(StandardCharsets.UTF_8)); + + // 获取 AES 密码器 + Cipher cipher = Cipher.getInstance(ALGORITHM); + // 初始化密码器(解密模型) + cipher.init(Cipher.DECRYPT_MODE, secKey); + + //base64解密 + byte[] bytes = Base64.getDecoder().decode(cipherBytes); + + // 解密数据, 返回明文 + byte[] plainBytes = cipher.doFinal(bytes); + + //反转解密的结果返回 + String str = new StringBuilder(new String(plainBytes, "utf-8")).reverse().toString(); + return str; + }catch (Exception e){ + log.info("解密密失败"+e.fillInStackTrace()); + return null; + } + + } +} diff --git a/ad-platform-common/src/main/java/com/baiye/util/JpaConverterAes.java b/ad-platform-common/src/main/java/com/baiye/util/JpaConverterAes.java new file mode 100644 index 00000000..e00a9fff --- /dev/null +++ b/ad-platform-common/src/main/java/com/baiye/util/JpaConverterAes.java @@ -0,0 +1,24 @@ +package com.baiye.util; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +/** + * @author YQY + * @date : 2022/01/12 + */ +@Converter +public class JpaConverterAes implements AttributeConverter { + + private final String secret = "ad-platform"; + + @Override + public String convertToDatabaseColumn(Object obj) { + return AESUtils.encrypt((String) obj, secret); + } + + @Override + public Object convertToEntityAttribute(String s) { + return AESUtils.decrypt(s,secret); + } +} diff --git a/ad-platform-pojo/src/main/java/com/baiye/model/entity/BaseClue.java b/ad-platform-pojo/src/main/java/com/baiye/model/entity/BaseClue.java index 9fd2e1dd..d8b62695 100644 --- a/ad-platform-pojo/src/main/java/com/baiye/model/entity/BaseClue.java +++ b/ad-platform-pojo/src/main/java/com/baiye/model/entity/BaseClue.java @@ -1,5 +1,6 @@ package com.baiye.model.entity; +import com.baiye.util.JpaConverterAes; import com.baiye.util.JpaConverterListJson; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -31,6 +32,7 @@ public class BaseClue implements Serializable { @ApiModelProperty(value = "nid") @Column(name = "nid") + @Convert(converter = JpaConverterAes.class) private String nid; @ApiModelProperty(value = "微信") diff --git a/services/ad-platform-source/src/main/java/com/baiye/module/controller/UploadFileController.java b/services/ad-platform-source/src/main/java/com/baiye/module/controller/UploadFileController.java index 43d11f80..f207c05d 100644 --- a/services/ad-platform-source/src/main/java/com/baiye/module/controller/UploadFileController.java +++ b/services/ad-platform-source/src/main/java/com/baiye/module/controller/UploadFileController.java @@ -37,7 +37,7 @@ public class UploadFileController { @RequestParam(value = "uploadType") Integer uploadType, @RequestParam(value = "userId") Long userId) { - if (file.length > 0 && uploadType != null && userId != null){ + if (file.length > 0){ return uploadFileService.singleFileUpload(file,uploadType,userId); } return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.EMPTY_ARGUMENT.getDesc()), HttpStatus.OK); diff --git a/services/ad-platform-source/src/main/java/com/baiye/module/entity/ClueMiddle.java b/services/ad-platform-source/src/main/java/com/baiye/module/entity/ClueMiddle.java index 8dba1c38..fa78d843 100644 --- a/services/ad-platform-source/src/main/java/com/baiye/module/entity/ClueMiddle.java +++ b/services/ad-platform-source/src/main/java/com/baiye/module/entity/ClueMiddle.java @@ -27,7 +27,7 @@ public class ClueMiddle{ @Id @ApiModelProperty(value = "线索id") @Column(name = "clue_id") - @NotNull + @NotNull(message = "资源id不能为空") private Long clueId; @ApiModelProperty(value = "小组id") diff --git a/services/ad-platform-source/src/main/java/com/baiye/module/service/impl/ClueServiceImpl.java b/services/ad-platform-source/src/main/java/com/baiye/module/service/impl/ClueServiceImpl.java index 25f0e4d6..f1d6a136 100644 --- a/services/ad-platform-source/src/main/java/com/baiye/module/service/impl/ClueServiceImpl.java +++ b/services/ad-platform-source/src/main/java/com/baiye/module/service/impl/ClueServiceImpl.java @@ -64,9 +64,7 @@ public class ClueServiceImpl implements ClueService { List clueLists = new ArrayList<>(); List clueMiddleList = clueMiddleRepository.findByTaskId(taskId); if (clueMiddleList.size() > 0) { - for (ClueMiddle clueMiddle : clueMiddleList) { - clueLists.add(clueMiddle.getClueId()); - } + clueMiddleList.stream().forEach(ct -> clueLists.add(ct.getClueId())); } return clueLists; } @@ -85,9 +83,7 @@ public class ClueServiceImpl implements ClueService { public void batchUpdateOrganize(DistributeResponseDTO distributeResponseDTO) { List responseList = distributeResponseDTO.getResponseList(); Long deptId = distributeResponseDTO.getDeptId(); - for (Long id : responseList) { - clueMiddleRepository.updateOrganizeIdById(id, deptId); - } + responseList.stream().forEach(id -> clueMiddleRepository.updateOrganizeIdById(id, deptId)); } /** diff --git a/services/ad-platform-source/src/main/java/com/baiye/module/service/impl/UploadFileServiceImpl.java b/services/ad-platform-source/src/main/java/com/baiye/module/service/impl/UploadFileServiceImpl.java index d866915a..36e88ed2 100644 --- a/services/ad-platform-source/src/main/java/com/baiye/module/service/impl/UploadFileServiceImpl.java +++ b/services/ad-platform-source/src/main/java/com/baiye/module/service/impl/UploadFileServiceImpl.java @@ -74,7 +74,6 @@ public class UploadFileServiceImpl implements UploadFileService { List clueRecords = new ArrayList<>(); int totalNum = 0; - // controller层已经判空,可直接取第一个文件名拼接任务名称 String oneFileName = files[0].getOriginalFilename(); long taskId = IdUtil.getSnowflake(workerId, datacenterId).nextId(); //处理文件数据 diff --git a/services/ad-platform-source/src/main/java/com/baiye/util/ExportExcelUtil.java b/services/ad-platform-source/src/main/java/com/baiye/util/ExportExcelUtil.java index 7aec74d6..a07a1e08 100644 --- a/services/ad-platform-source/src/main/java/com/baiye/util/ExportExcelUtil.java +++ b/services/ad-platform-source/src/main/java/com/baiye/util/ExportExcelUtil.java @@ -18,6 +18,7 @@ public class ExportExcelUtil { */ @SneakyThrows //处理异常try public static void downloadEasyExcel(HttpServletResponse response, Object obj, List list){ + // 不要用postman测试,会出现乱码 String fileName = URLEncoder.encode("表单", "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); diff --git a/services/ad-platform-source/src/main/resources/application.yml b/services/ad-platform-source/src/main/resources/application.yml index 371eda3a..0be019ad 100644 --- a/services/ad-platform-source/src/main/resources/application.yml +++ b/services/ad-platform-source/src/main/resources/application.yml @@ -36,6 +36,10 @@ snowflake: workerId: 9 datacenterId: 9 +#AES秘钥 +aes: + secret: ad-platform + ribbon: #建立连接超时时间 diff --git a/services/ad-platform-source/src/main/resources/logback.xml b/services/ad-platform-source/src/main/resources/logback.xml index d22f9c88..cbf30111 100644 --- a/services/ad-platform-source/src/main/resources/logback.xml +++ b/services/ad-platform-source/src/main/resources/logback.xml @@ -3,8 +3,10 @@ trade-account + + @@ -32,7 +34,7 @@ ${LOG_PATTERN} - utf-8 + utf- @@ -41,11 +43,6 @@ - - - - -