From 6ef135340ef3401619d7edafccfa5de2cc9c0acd Mon Sep 17 00:00:00 2001 From: baozhipeng <961815525@qq.com> Date: Tue, 14 Sep 2021 15:50:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E7=89=88=E8=B7=A8=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E8=B0=83=E7=94=A8=E6=A0=A1=E9=AA=8C=E8=BA=AB=E4=BB=BD?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=B8=8D=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/PayPaymentRecordServiceImpl.java | 10 +++ .../java/com/jero/util/standardsUtil.java | 81 ++++++++++++++++++- .../src/main/resources/application-dev.yml | 6 +- 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/jero-boot-incoming-payment/src/main/java/com/jero/payment/service/impl/PayPaymentRecordServiceImpl.java b/jero-boot-incoming-payment/src/main/java/com/jero/payment/service/impl/PayPaymentRecordServiceImpl.java index ae3ce525..300b909c 100644 --- a/jero-boot-incoming-payment/src/main/java/com/jero/payment/service/impl/PayPaymentRecordServiceImpl.java +++ b/jero-boot-incoming-payment/src/main/java/com/jero/payment/service/impl/PayPaymentRecordServiceImpl.java @@ -86,6 +86,16 @@ public class PayPaymentRecordServiceImpl extends ServiceImpl mainList(Page page, PayCommonProject payCommonProject){ + //从标协项目获取公钥 +// String publicKey = HttpUtil.get("http://localhost:8080/api/authenticationUtils/getPublicKey"); + + HashMap paramMap = new HashMap<>(); + + paramMap.put("token", standardsUtil.encryptByRsaPriKey().get("token")); + paramMap.put("publicKey", standardsUtil.encryptByRsaPriKey().get("publicKey")); + + String result3= HttpUtil.get("http://localhost:8080/api/paymentInfo/incomingPaymentCheck", paramMap); + return payPaymentRecordMapper.mainList(page, payCommonProject); } diff --git a/jero-boot-incoming-payment/src/main/java/com/jero/util/standardsUtil.java b/jero-boot-incoming-payment/src/main/java/com/jero/util/standardsUtil.java index f6c709b6..c4c907e6 100644 --- a/jero-boot-incoming-payment/src/main/java/com/jero/util/standardsUtil.java +++ b/jero-boot-incoming-payment/src/main/java/com/jero/util/standardsUtil.java @@ -8,18 +8,43 @@ import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSONObject; import com.jero.common.exception.JeroBootException; import com.jero.common.util.RestUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.codec.binary.Base64; import org.springframework.beans.factory.annotation.Value; +import javax.crypto.Cipher; +import java.io.ByteArrayOutputStream; +import java.security.Key; +import java.security.KeyFactory; +import java.security.spec.X509EncodedKeySpec; +import java.util.HashMap; +import java.util.Map; + /** * @author liJiaRao * @date 2021-09-10 9:58 */ +@Slf4j public class standardsUtil { @Value("${standards.url}") private static String url; @Value("${standards.token}") private static String token; + + /** */ + /** + * 加密算法RSA + */ + public static final String KEY_ALGORITHM = "RSA"; + + /** */ + /** + * RSA最大加密明文大小 + */ + private static final int MAX_ENCRYPT_BLOCK = 117; + + public static String getRSAPublicKey(){ JSONObject jsonObject = RestUtil.get(url + "/commonUtils/getRSAPublicKey"); if (jsonObject.getBoolean("success")) { @@ -29,17 +54,65 @@ public class standardsUtil { } } + /** */ + /** + *

+ * 公钥加密 + *

+ * + * @param data 源数据 + * @param publicKey 公钥(BASE64编码) + * @return + * @throws Exception + */ + public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { + byte[] keyBytes = Base64.decodeBase64(publicKey); + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + Key publicK = keyFactory.generatePublic(x509KeySpec); + // 对数据加密 + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + cipher.init(Cipher.ENCRYPT_MODE, publicK); + int inputLen = data.length; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int offSet = 0; + byte[] cache; + int i = 0; + // 对数据分段加密 + while (inputLen - offSet > 0) { + if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { + cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); + } else { + cache = cipher.doFinal(data, offSet, inputLen - offSet); + } + out.write(cache, 0, cache.length); + i++; + offSet = i * MAX_ENCRYPT_BLOCK; + } + byte[] encryptedData = out.toByteArray(); + out.close(); + return encryptedData; + } /** - * RSA 使用公钥加密token + * 使用公钥加密token */ - public static byte[] encryptByRsaPriKey() { + public static Map encryptByRsaPriKey() { + Map map= new HashMap(); + try { //从标协项目获取公钥 String publicKey=HttpUtil.get("http://localhost:8080/api/authenticationUtils/getPublicKey"); - RSA rsa = new RSA(null,publicKey); + String token= Base64.encodeBase64String(encryptByPublicKey("abc".getBytes(), publicKey)); - return rsa.encrypt(StrUtil.bytes("Aa123456!!", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey); + map.put("publicKey",publicKey); + map.put("token",token); + + } catch (Exception e) { + e.printStackTrace(); + log.error("使用公钥加密token失败",e); + } + return map; } } diff --git a/jero-boot-single-startup/src/main/resources/application-dev.yml b/jero-boot-single-startup/src/main/resources/application-dev.yml index 6bdf0082..0aa6899a 100644 --- a/jero-boot-single-startup/src/main/resources/application-dev.yml +++ b/jero-boot-single-startup/src/main/resources/application-dev.yml @@ -1,5 +1,5 @@ server: - port: 8080 + port: 8081 tomcat: max-swallow-size: -1 error: @@ -257,7 +257,7 @@ jero: # cors白名单 notFilter: # origin地址 - originIp: http://localhost:3000,http://localhost:3001,http://localhost:8080,127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:8080,10.0.1.70:3000,10.0.1.81:3000,10.0.1.81:8080,192.168.1.7:8080,192.168.1.7:3000,192.168.1.7:3001,121.36.69.172:4780 + originIp: http://localhost:3000,http://localhost:3001,http://localhost:8080,http://localhost:8081,127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:8080,10.0.1.70:3000,10.0.1.81:3000,10.0.1.81:8080,192.168.1.7:8080,192.168.1.7:3000,192.168.1.7:3001,121.36.69.172:4780 #cas单点登录 cas: @@ -309,5 +309,5 @@ huaWeiSms: # 短信开关 open: false standards: - url: http://loaclhots:8081/api + url: http://loaclhots:8080/api token: Aa123456!! \ No newline at end of file