背景 :
第三方客户端访问系统接口时,需要对第三方客户端传递参数进行加密传输,同时在服务端网关进行解密,保证参数路由到其它内部服务时参数状态为解密状态.
流程:
第三方客户端→网关解密→路由到指定微服务接口
将需要加密传输的请求参数做成jar包或依赖包提供给第三方 , 并在服务端新增解密逻辑判断需要解密的请求进行解密处理.
使用RSA加密请求参数- 对外提供调用核心方法
@Override public ResponseEntity
package *;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public final class RSAUtil {
private static final String RSA_ALGORITHM = "RSA";
private static final int MAX_ENCRYPT_BLOCK = 117;
private static final int MAX_DECRYPT_BLOCK = 128;
public static final String encryptByPublicKey(String dataBase64String, String publicKeyString) throws NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, IOException {
byte[] data = dataBase64String.getBytes("UTF-8");
byte[] keyBytes = Base64.getDecoder().decode(publicKeyString);
//实例化密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
//初始化公钥,根据给定的编码密钥创建一个新的 X509EncodedKeySpec。
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
//数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
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 Base64.getEncoder().encodeToString(encryptedData);
}
public static final byte[] decryptByPublicKey(byte[] encryptedData, String publicKeyString) throws Exception {
byte[] key = Base64.getDecoder().decode(publicKeyString);
//实例化密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
//初始化公钥
//密钥材料转换
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
//产生公钥
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
//数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, pubKey);
int inputLen = encryptedData.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
}
}
参考文章
调用HttpUtil传递参数
1.调用HttpUtil传递参数
这里使用spring自带的 restTemplate 使用默认的 httpclient 进行调用,也可以考虑情况集成开源的okhttp3进行使用.
public final static ResponseEntity参考文章 继承 HttpServletRequestWrapper 解密请求参数postForObject(String url, String params) { String[] requestParams = params.split("-"); //设置Http的Header HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); //设置固定请求头 headers.add("apiAuthAccessEnable", "true"); MultiValueMap paramMap = new LinkedMultiValueMap<>(); paramMap.add("data", requestParams[0]); paramMap.add("secretId", requestParams[1]); //设置访问的Entity HttpEntity > httpEntity = new HttpEntity<>(paramMap, headers); ResponseEntity responseEntity = null; try { responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Map.class); } catch (RestClientException e) { if ("401".contains(e.getMessage().trim())) { HashMap tempBody = new HashMap<>(); tempBody.put("status", HttpStatus.UNAUTHORIZED.value()); tempBody.put("message", "Unauthorized Access"); ResponseEntity temp = new ResponseEntity (tempBody, HttpStatus.UNAUTHORIZED); responseEntity = temp; } } return responseEntity; }
RSA私钥解密请求参数 ,通过继承 HttpServletRequestWrapper 对请求完成解密过程 , 同时开放解密参数提供给 Filter 使用
请求参数解密HttpServletRequestWrapper 相关代码package *;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import *.entity.ApiAuthSecretPO;
import *.service.ApiAuthSecretService;
import *.utils.RSAUtil;
import *.utils.SpringContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public final class ApiAuthRequestWrapper extends HttpServletRequestWrapper {
private static final Logger logger = LoggerFactory.getLogger(ApiAuthRequestWrapper.class);
private byte[] body;
private byte[] requestParam = null;
public ApiAuthRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
inputStream2String(request.getInputStream());
}
@Override
public final ServletInputStream getInputStream() {
final ByteArrayInputStream bais = new ByteArrayInputStream(requestParam);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return bais.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
public final byte[] inputStream2String(InputStream inputStream) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
sb.append("get body params fail");
logger.error(e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
String requestParam = "";
if (sb.toString().trim().length() == 0) {
return new byte[]{};
}
JSONObject jsonObject = JSONObject.parseObject(sb.toString());
String decryptResult = "";
try {
String dataString = jsonObject.getString("data")
.replace("[", "").replace("]", "");
String dataSubstring = dataString.substring(1, dataString.length() - 1);
String secretIdStr = jsonObject.getString("secretId")
.replace("[", "").replace("]", "");
String secretIdSubstring = secretIdStr.substring(1, secretIdStr.length() - 1);
ApiAuthSecretService apiAuthSecretService = SpringContextUtil.getBean(ApiAuthSecretService.class);
ApiAuthSecretPO apiAuthSecretPO = apiAuthSecretService.getByApiInterfaceAuthId(secretIdSubstring);
decryptResult = RSAUtil.decryptByPrivateKey(dataSubstring, apiAuthSecretPO.getPrivateKey());
requestParam = decryptResult;
} catch (Exception e) {
logger.error("参数解密失败,原因:{}", e.getMessage());
throw new RuntimeException(e.getMessage());
}
this.body = decryptResult.getBytes();
JSONObject temp = JSON.parseObject(requestParam);
this.requestParam = JSON.toJSONString(temp.get("data")).getBytes();
return this.requestParam;
}
public final String getRequestBody() throws UnsupportedEncodingException {
String body = new String(this.body, "UTF-8");
return body;
}
public final String getRequestParam() throws UnsupportedEncodingException {
String param = new String(this.requestParam, "UTF-8");
return param;
}
}
参考文章
过滤器Filter对相应参数进行操作
将解密后请求参数从 HttpServletRequestWrapper 取出 , 并在过滤器中进行业务验证及其它操作 , 对符合拦截条件的请求进行拦截处理, 对不符合拦截请求的参数进行放行处理(及不需要解密逻辑).
package *.configure;
import com.alibaba.fastjson.JSONObject;
import *.entity.ApiAuthPO;
import *.service.ApiAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@Component
@WebFilter(filterName = "xxxFilter", urlPatterns = {"/xxx
private Boolean validateRepeatedRequest(Long timestamp, String appId) {
Boolean repeatedBoolean = false;
if (System.currentTimeMillis() - timestamp > 1000 * 60 * 5 || System.currentTimeMillis() - timestamp < 0) {
repeatedBoolean = true;
} else {
String key = redisKey + appId + ":";
repeatedBoolean = !redisTemplate.opsForValue().setIfAbsent(key + timestamp, String.valueOf(timestamp), 5, TimeUnit.MINUTES);
}
return repeatedBoolean;
}
private void changeUnauthorizedInfo(HttpServletResponse response, String path) throws IOException {
logger.debug("当前接口未获取到授权信息!,接口路径为:{}", path);
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.getWriter().write("Unauthorized Access!");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}



