第二版跨服务调用校验身份,用不了

This commit is contained in:
baozhipeng
2021-09-14 15:50:07 +08:00
parent 858279791e
commit 6ef135340e
3 changed files with 90 additions and 7 deletions
@@ -86,6 +86,16 @@ public class PayPaymentRecordServiceImpl extends ServiceImpl<PayPaymentRecordMap
* @return
*/
public List<PayCommonProject> mainList(Page page, PayCommonProject payCommonProject){
//从标协项目获取公钥
// String publicKey = HttpUtil.get("http://localhost:8080/api/authenticationUtils/getPublicKey");
HashMap<String, Object> 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);
}
@@ -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 {
}
}
/** */
/**
* <p>
* 公钥加密
* </p>
*
* @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<String,String> encryptByRsaPriKey() {
Map<String,String> map= new HashMap<String,String>();
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;
}
}
@@ -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!!