Commit 7f1123b0 authored by matianhao's avatar matianhao

[资产资源接口] <add> 调用js加解密,接入获取项目信息接口

parent 3d120729
package com.mth.requestsecret.controller;
import com.mth.requestsecret.service.RestTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* 资产资源接口
*
* @author MaTianHao
* @date 2020/11/23
*/
@RestController
@Slf4j
public class ZczyController {
@Autowired
private RestTemplateService restTemplateService;
/**
* 获取项目信息接口
* 根据时间段获取成交项目信息,时间跨度不能超过5天
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 解密后字符串
*/
@PostMapping("/getProjectBaseInformation")
public String getProjectBaseInformation(String startTime, String endTime) throws Exception {
// api方法签名
String apiMethod = "fGetProjectBaseInformation";
// api参数
Map<String, String> param = new HashMap<>(2);
param.put("StartTime", startTime);
param.put("EndTime", endTime);
return restTemplateService.zczySendRequest(param, apiMethod);
}
}
......@@ -65,6 +65,12 @@ public class RestTemplateService {
@Value("${buildhouse.address}")
private String buildHouseAddress;
/**
* 资产资源接口地址
*/
@Value(("${zczy.address}"))
private String zczyAddress;
/**
* 瑞成平台接口发送请求
*
......@@ -241,4 +247,32 @@ public class RestTemplateService {
return responseEntity;
}
/**
* 资产资源接口 发送请求
*/
public String zczySendRequest(Map<String, String> paramMap, String apiMethod) throws Exception {
String url = zczyAddress + apiMethod;
log.info("资产资源api:{}", apiMethod);
log.info("api url:{}", url);
log.info("api params:{}", paramMap);
// 1. 参数加密
paramMap.replace("StartTime", AESUtils.zczyDes(paramMap.get("StartTime"), "encMe"));
paramMap.replace("EndTime", AESUtils.zczyDes(paramMap.get("EndTime"), "encMe"));
// 2. 构建请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity httpEntity = new HttpEntity<>(paramMap, headers);
// 3. 发送请求
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
// 响应解密
String decryptResponse = AESUtils.zczyDes(responseEntity.getBody(), "uncMe");
String decryptStr = decryptResponse.trim();
log.info("api response:{}", decryptStr);
return decryptStr;
}
}
\ No newline at end of file
......@@ -9,6 +9,10 @@ import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.Security;
......@@ -42,6 +46,12 @@ public class AESUtils {
*/
private final static byte[] SGB_IV = {(byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF};
// 资产资源加密参数
/**
* key
*/
private final static String ZCZY_KEY = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
static {
Security.addProvider(new BouncyCastleProvider());
}
......@@ -132,6 +142,33 @@ public class AESUtils {
}
}
/**
* 资产资源接口:des加密、解密
*
* @param content 待处理字符串
* @param method 加密:encMe;解密:uncMe
* @return
* @throws Exception
*/
public static String zczyDes(String content, String method) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
// 读取js文件
FileReader reader = new FileReader("src/main/resources/js/des.js");
engine.eval(reader);
String encryptContent = "";
if (engine instanceof Invocable) {
Invocable invoke = (Invocable) engine;
// 调用js函数
encryptContent = (String) invoke.invokeFunction(method, content, ZCZY_KEY);
}
reader.close();
return encryptContent;
}
/**
* 16进制字符串转字节数组
*
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment