跨服务调用校验身份

This commit is contained in:
baozhipeng
2021-09-14 17:30:21 +08:00
parent 123bb8c6a8
commit d9049e81ce
4 changed files with 83 additions and 141 deletions
@@ -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<String,String> encryptByRsaPriKey() {
Map<String,String> 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;
}
}
@@ -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("调用会员系统出错");
}
}
/** */
/**
* <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;
}
/**
* 使用公钥加密token
*/
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");
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;
}
}