|
|
@@ -47,34 +47,41 @@ public class ChengTaiRequestApiComponent {
|
|
|
private final RequestComponent requestComponent;
|
|
|
|
|
|
public <T extends BaseRequest, B extends BaseResponse> B requestUrl(String url, T requestBody, Class<B> responseType, HttpHeaders httpHeaders) {
|
|
|
- String busReqString = JSON.toJSONString(requestBody, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
|
|
|
- try{
|
|
|
+ try {
|
|
|
+ String busReqString = JSON.toJSONString(requestBody, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
|
|
|
PrivateKey privateKey = RSAUtil.getPrivateKeyFromPKCS8("RSA", chengTaiApiProperties.getPrivateKey());
|
|
|
PublicKey ctPublicKey = RSAUtil.getPublicKeyFromX509("RSA", chengTaiApiProperties.getCtPublicKey());
|
|
|
- System.out.println(busReqString);
|
|
|
+ log.debug("请求数据: {}", busReqString);
|
|
|
+
|
|
|
//base64加密
|
|
|
String baseStr = Base64.getEncoder().encodeToString(busReqString.getBytes(Charset.forName("UTF-8")));
|
|
|
//rsa加密
|
|
|
String encryptStr = RSAUtil.encryptString(ctPublicKey, baseStr); //加密
|
|
|
String signatureStr = RSAUtil.signature(baseStr, privateKey); //加签
|
|
|
ChengTaiEncrypted chengTaiEncrypted = new ChengTaiEncrypted(encryptStr,chengTaiApiProperties,signatureStr);
|
|
|
- System.out.println(JSON.toJSON(chengTaiEncrypted));
|
|
|
+ log.debug("加密后数据: {}", JSON.toJSON(chengTaiEncrypted));
|
|
|
+
|
|
|
ResponseEntity<B> bResponseEntity = requestComponent.objectPost(url, chengTaiEncrypted, responseType, httpHeaders);
|
|
|
|
|
|
String body = JSON.toJSONString(bResponseEntity.getBody());//需要联调观察数据格式
|
|
|
B res = JSON.parseObject(body).toJavaObject(responseType);
|
|
|
- System.out.println(JSON.toJSON(res));
|
|
|
-// if (!ResponseCode.R_000.getCode().equals(res.getCode())){throw new SystemException(res.message);}
|
|
|
+ log.debug("响应数据: {}", JSON.toJSON(res));
|
|
|
+
|
|
|
// 解密响应
|
|
|
String newSource = RSAUtil.decryptString(privateKey, res.encryptionMessage);
|
|
|
String info = new String(Base64.getDecoder().decode(newSource), Charset.forName("UTF-8"));
|
|
|
- System.out.println(info);
|
|
|
+ log.debug("解密后数据: {}", info);
|
|
|
+
|
|
|
boolean flag = RSAUtil.signatureVerfiy(newSource, res.signature, ctPublicKey);
|
|
|
- if(!flag){throw new SystemException(res.message);}
|
|
|
+ if(!flag){throw new SystemException("验签失败: " + res.message);}
|
|
|
res.setDecodeData(info);
|
|
|
return res;
|
|
|
- }catch (Exception e){
|
|
|
- throw new SystemException(e.toString());
|
|
|
+ } catch (SystemException e) {
|
|
|
+ log.error("请求异常: {}", e.getMessage(), e);
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("请求异常: {}", e.getMessage(), e);
|
|
|
+ throw new SystemException("请求失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -83,22 +90,32 @@ public class ChengTaiRequestApiComponent {
|
|
|
}
|
|
|
|
|
|
public <T extends BaseRequest, B extends BaseResponse> B requestUrlToken(String url, T requestBody, Class<B> responseType, HttpHeaders httpHeaders) {
|
|
|
- try{
|
|
|
+ try {
|
|
|
ResponseEntity<B> bResponseEntity = requestComponent.objectPost(url, requestBody, responseType, httpHeaders);
|
|
|
- String body = JSON.toJSONString(bResponseEntity.getBody());//需要联调观察数据格式
|
|
|
+ String body = JSON.toJSONString(bResponseEntity.getBody());
|
|
|
B res = JSON.parseObject(body).toJavaObject(responseType);
|
|
|
- if (!ResponseCode.R_000.getCode().equals(res.getCode())){throw new SystemException(res.message);}
|
|
|
+
|
|
|
+ if (!ResponseCode.R_000.getCode().equals(res.getCode())){
|
|
|
+ throw new SystemException("API返回错误: " + res.message);
|
|
|
+ }
|
|
|
+
|
|
|
PrivateKey privateKey = RSAUtil.getPrivateKeyFromPKCS8("RSA", chengTaiApiProperties.getPrivateKey());
|
|
|
PublicKey ctPublicKey = RSAUtil.getPublicKeyFromX509("RSA", chengTaiApiProperties.getCtPublicKey());
|
|
|
+
|
|
|
// 解密响应
|
|
|
String newSource = RSAUtil.decryptString(privateKey, res.encryptionMessage);
|
|
|
String info = new String(Base64.getDecoder().decode(newSource), Charset.forName("UTF-8"));
|
|
|
+
|
|
|
boolean flag = RSAUtil.signatureVerfiy(newSource, res.signature, ctPublicKey);
|
|
|
- if(!flag){throw new SystemException(res.message);}
|
|
|
+ if(!flag){throw new SystemException("验签失败: " + res.message);}
|
|
|
res.setDecodeData(info);
|
|
|
return res;
|
|
|
- }catch (Exception e){
|
|
|
- throw new SystemException(e.toString());
|
|
|
+ } catch (SystemException e) {
|
|
|
+ log.error("token请求异常: {}", e.getMessage(), e);
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("token请求异常: {}", e.getMessage(), e);
|
|
|
+ throw new SystemException("token请求失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -135,9 +152,7 @@ public class ChengTaiRequestApiComponent {
|
|
|
.get(AUTHORIZATION);
|
|
|
|
|
|
if (StringUtils.isNotBlank(authorization)) {
|
|
|
- System.out.print("---------------\n--------------\n---------------\n--------------\n---------------\n--------------\n---------------\n--------------\n---------------\n--------------\n");
|
|
|
- System.out.printf("authorization:" + authorization);
|
|
|
- System.out.print("---------------\n--------------\n---------------\n--------------\n---------------\n--------------\n---------------\n--------------\n---------------\n--------------\n");
|
|
|
+ log.debug("使用缓存中的token: {}", authorization);
|
|
|
httpHeaders.set(AUTHTOKEN, authorization);
|
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
return httpHeaders;
|
|
|
@@ -151,17 +166,19 @@ public class ChengTaiRequestApiComponent {
|
|
|
try {
|
|
|
return this.getToken(chengTaiApiConfigureParameters,httpHeaders,url);
|
|
|
} catch (Exception e) {
|
|
|
- throw new SystemException(e.toString());
|
|
|
+ log.error("获取token失败: {}", e.getMessage(), e);
|
|
|
+ throw new SystemException("获取token失败: " + e.getMessage());
|
|
|
} finally {
|
|
|
- lock.unlock();
|
|
|
+ if (lock.isHeldByCurrentThread()) {
|
|
|
+ lock.unlock();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
- log.debug(e.getMessage());
|
|
|
- lock.unlock();
|
|
|
+ log.error("获取锁失败: {}", e.getMessage(), e);
|
|
|
Thread.currentThread().interrupt();
|
|
|
- throw new SystemException(e.getMessage());
|
|
|
+ throw new SystemException("获取锁失败: " + e.getMessage());
|
|
|
}
|
|
|
|
|
|
// 睡眠一段时间 自调用 重新获取缓存中的 header
|
|
|
@@ -183,16 +200,25 @@ public class ChengTaiRequestApiComponent {
|
|
|
* @return
|
|
|
*/
|
|
|
private HttpHeaders getToken(ChengTaiApiConfigureParameters chengTaiApiConfigureParameters, HttpHeaders httpHeaders,String url) {
|
|
|
+ try {
|
|
|
+ ChengTaiTokenRequest request = new ChengTaiTokenRequest(chengTaiApiProperties.getPartnerCode());
|
|
|
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ ChengTaiTokenResponse accessToken = getAccessToken(request,httpHeaders,url);
|
|
|
+
|
|
|
+ if (accessToken == null || StringUtils.isBlank(accessToken.getAccessToken())) {
|
|
|
+ throw new SystemException("获取accessToken失败或为空");
|
|
|
+ }
|
|
|
|
|
|
- ChengTaiTokenRequest request = new ChengTaiTokenRequest(chengTaiApiProperties.getPartnerCode());
|
|
|
- httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- ChengTaiTokenResponse accessToken = getAccessToken(request,httpHeaders,url);
|
|
|
- httpHeaders.set(AUTHTOKEN, accessToken.getAccessToken());
|
|
|
- // 3.1 存入redis中
|
|
|
- RMap<Object, Object> rMap = redissonClient.getMap(CacheConstant.CHENGTAI_AUTHORIZATION + chengTaiApiConfigureParameters.getUserId());
|
|
|
- rMap.put(AUTHORIZATION, accessToken.getAccessToken());
|
|
|
- rMap.expire(Duration.ofHours(CacheConstant.CHENGTAI_AUTHORIZATION_TIME));
|
|
|
- return httpHeaders;
|
|
|
+ httpHeaders.set(AUTHTOKEN, accessToken.getAccessToken());
|
|
|
+ // 3.1 存入redis中
|
|
|
+ RMap<Object, Object> rMap = redissonClient.getMap(CacheConstant.CHENGTAI_AUTHORIZATION + chengTaiApiConfigureParameters.getUserId());
|
|
|
+ rMap.put(AUTHORIZATION, accessToken.getAccessToken());
|
|
|
+ rMap.expire(Duration.ofHours(CacheConstant.CHENGTAI_AUTHORIZATION_TIME));
|
|
|
+ return httpHeaders;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取token异常: {}", e.getMessage(), e);
|
|
|
+ throw new SystemException("获取token失败: " + e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -203,14 +229,24 @@ public class ChengTaiRequestApiComponent {
|
|
|
* @return 出参实体
|
|
|
*/
|
|
|
private ChengTaiTokenResponse getAccessToken(ChengTaiTokenRequest chengTaiTokenRequest, HttpHeaders httpHeaders,String url) {
|
|
|
- ChengTaiTokenResponse response = requestRequestUrlToken(RequestUrl.Z00001,
|
|
|
- chengTaiTokenRequest,
|
|
|
- ChengTaiTokenResponse.class,
|
|
|
- httpHeaders,url);
|
|
|
- ChengTaiTokenResponse res = JSON.parseObject(response.getDecodeData()).toJavaObject(ChengTaiTokenResponse.class);
|
|
|
- res.setCode(response.getCode());
|
|
|
- res.setMessage(response.getMessage());
|
|
|
- return res;
|
|
|
+ try {
|
|
|
+ ChengTaiTokenResponse response = requestRequestUrlToken(RequestUrl.Z00001,
|
|
|
+ chengTaiTokenRequest,
|
|
|
+ ChengTaiTokenResponse.class,
|
|
|
+ httpHeaders,url);
|
|
|
+
|
|
|
+ if (response == null || StringUtils.isBlank(response.getDecodeData())) {
|
|
|
+ throw new SystemException("获取token响应为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ ChengTaiTokenResponse res = JSON.parseObject(response.getDecodeData()).toJavaObject(ChengTaiTokenResponse.class);
|
|
|
+ res.setCode(response.getCode());
|
|
|
+ res.setMessage(response.getMessage());
|
|
|
+ return res;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("请求token接口失败: {}", e.getMessage(), e);
|
|
|
+ throw new SystemException("请求token接口失败: " + e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|