From d9049e81ce290a70caec1da56ef9b7f3cf55c7f5 Mon Sep 17 00:00:00 2001 From: baozhipeng <961815525@qq.com> Date: Tue, 14 Sep 2021 17:30:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=A8=E6=9C=8D=E5=8A=A1=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E8=BA=AB=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/PayPaymentRecordServiceImpl.java | 51 ++++---- .../com/jero/util/AuthenticationUtils.java | 49 ++++++++ .../java/com/jero/util/standardsUtil.java | 118 ------------------ .../src/main/resources/application-dev.yml | 6 +- 4 files changed, 83 insertions(+), 141 deletions(-) create mode 100644 jero-boot-incoming-payment/src/main/java/com/jero/util/AuthenticationUtils.java delete mode 100644 jero-boot-incoming-payment/src/main/java/com/jero/util/standardsUtil.java 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 300b909c..cba90904 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 @@ -31,7 +31,7 @@ import com.jero.project.entity.PayWorkingGroup; import com.jero.project.entity.PayWorkingGroupSubItem; import com.jero.project.mapper.PayCommonProjectMapper; import com.jero.project.mapper.PayWorkingGroupSubItemMapper; -import com.jero.util.standardsUtil; +import com.jero.util.AuthenticationUtils; import io.lettuce.core.dynamic.annotation.Param; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.collections.CollectionUtils; @@ -73,11 +73,13 @@ 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); } @@ -105,8 +97,6 @@ public class PayPaymentRecordServiceImpl extends ServiceImpl addPhase(PayPaymentRecord payPaymentRecord){ - //来款记录表新增 - insertPayPaymentRecord(payPaymentRecord); BigDecimal receivableAmount; Boolean isAdd; @@ -195,14 +185,17 @@ public class PayPaymentRecordServiceImpl extends ServiceImpl wrapper = new LambdaQueryWrapper<>(); wrapper.eq(PayPaymentRecord::getProjectId, payPaymentRecord.getProjectId()); @@ -249,8 +244,7 @@ public class PayPaymentRecordServiceImpl extends ServiceImpl map = authenticationUtils.encryptByRsaPriKey(); + + HashMap paramMap = new HashMap<>(); + + paramMap.put("token", map.get("token")); + paramMap.put("publicKey", map.get("publicKey")); + + String result3= HttpUtil.get(url+"/paymentInfo/incomingPaymentCheck", paramMap); + } + /** * 导出 * @param request diff --git a/jero-boot-incoming-payment/src/main/java/com/jero/util/AuthenticationUtils.java b/jero-boot-incoming-payment/src/main/java/com/jero/util/AuthenticationUtils.java new file mode 100644 index 00000000..3e9ae3ba --- /dev/null +++ b/jero-boot-incoming-payment/src/main/java/com/jero/util/AuthenticationUtils.java @@ -0,0 +1,49 @@ +package com.jero.util; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.asymmetric.KeyType; +import cn.hutool.crypto.asymmetric.RSA; +import cn.hutool.http.HttpUtil; +import com.alibaba.fastjson.JSONObject; +import com.jero.common.exception.JeroBootException; +import com.jero.common.util.RestUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; + +/** + * 跨服务调用时验证身份 + */ +@Component +public class AuthenticationUtils { + + @Value("${standards.url}") + private String URL; + + @Value("${standards.token}") + private String APPLICATION_TOKEN; + + /** + * 获取公钥并使用公钥加密token + */ + public Map encryptByRsaPriKey() { + + Map map= new HashMap<>(); + //从标协项目获取公钥 + String publicKey=HttpUtil.get(URL+"/authenticationUtils/getPublicKey"); + + RSA rsa = new RSA(null,publicKey); + + byte[] token = rsa.encrypt(StrUtil.bytes(APPLICATION_TOKEN, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey); + String tokenStr = Base64.getEncoder().encodeToString(token); + + map.put("publicKey",publicKey); + map.put("token",tokenStr); + return map; + } + +} 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 deleted file mode 100644 index c4c907e6..00000000 --- a/jero-boot-incoming-payment/src/main/java/com/jero/util/standardsUtil.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.jero.util; - -import cn.hutool.core.util.CharsetUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.crypto.asymmetric.KeyType; -import cn.hutool.crypto.asymmetric.RSA; -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")) { - return jsonObject.getString("result"); - }else { - throw new JeroBootException("调用会员系统出错"); - } - } - - /** */ - /** - *

- * 公钥加密 - *

- * - * @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; - } - - /** - * 使用公钥加密token - */ - public static Map encryptByRsaPriKey() { - Map map= new HashMap(); - try { - //从标协项目获取公钥 - String publicKey=HttpUtil.get("http://localhost:8080/api/authenticationUtils/getPublicKey"); - - String token= Base64.encodeBase64String(encryptByPublicKey("abc".getBytes(), 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 0aa6899a..994215a6 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: 8081 + port: 8080 tomcat: max-swallow-size: -1 error: @@ -308,6 +308,8 @@ huaWeiSms: appSecret: eGt7RfKNia8cK4Z3Vnw5QE4Uy108 # 短信开关 open: false + +#标协项目 standards: - url: http://loaclhots:8080/api + url: http://localhost:8081/api token: Aa123456!! \ No newline at end of file