Browse Source

38088转发

tags/24080901
PoffyZhang 1 year ago
parent
commit
70dbd8d151
7 changed files with 213 additions and 24 deletions
  1. +29
    -24
      pmapi/src/test/java/com/ningdatech/pmapi/irs/CoreTest.java
  2. +6
    -0
      syn-data-task/pom.xml
  3. +1
    -0
      syn-data-task/src/main/java/com/ningdatech/syndataapi/common/constant/BizConst.java
  4. +85
    -0
      syn-data-task/src/main/java/com/ningdatech/syndataapi/common/utils/CryptUtils.java
  5. +10
    -0
      syn-data-task/src/main/java/com/ningdatech/syndataapi/open/controller/ProjectReceiveController.java
  6. +56
    -0
      syn-data-task/src/main/java/com/ningdatech/syndataapi/open/manage/IrsManage.java
  7. +26
    -0
      syn-data-task/src/main/java/com/ningdatech/syndataapi/scheduler/model/dto/ForwardDTO.java

+ 29
- 24
pmapi/src/test/java/com/ningdatech/pmapi/irs/CoreTest.java View File

@@ -1,20 +1,23 @@
package com.ningdatech.pmapi.irs;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.ningdatech.irs.service.IRefreshTokenService;
import com.ningdatech.pmapi.AppTests;
import com.ningdatech.pmapi.common.util.CryptUtils;
import com.ningdatech.pmapi.common.util.HttpUtil;
import com.ningdatech.pmapi.gov.model.req.ProjectPushReq;
import com.ningdatech.pmapi.irs.model.dto.ForwardDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
@@ -53,14 +56,9 @@ public class CoreTest extends AppTests {
Integer page = 1;

long timestamp = System.currentTimeMillis();
String secret = refreshTokenService.refreshToken(appKey, appSecret, govRequestTokenUrl, govRefreshTokenUrl,
interfaceName, HttpMethod.POST.name());
String sign = null;
try{
sign = CryptUtils.MD5Encode(appKey + secret + timestamp);
}catch (Exception e){
log.error(e.getMessage());
}
// String secret = refreshTokenService.refreshToken(appKey, appSecret, govRequestTokenUrl, govRefreshTokenUrl,
// interfaceName, HttpMethod.POST.name());
String secret = appSecret;
HttpComponentsClientHttpRequestFactory factory = HttpUtil.generateHttpRequestFactory();
RestTemplate restTemplate;
if(Objects.nonNull(factory)){
@@ -68,19 +66,26 @@ public class CoreTest extends AppTests {
}else{
restTemplate = new RestTemplate();
}

String url = coreBizUrl + "?" +
"dingCode=" + orgCode + "&matterName=" + (StringUtils.isNotBlank(businessName)?businessName:StringUtils.EMPTY) + "&oid=&useState=1&" +
"limit=" + (Objects.isNull(limit) ? "10" : limit) + "&page=" + (Objects.isNull(page) ? "1" : page) +
"&appKey=" + appKey + "&sign=" + sign + "&requestTime=" + timestamp;

ResponseEntity<JSONObject> forEntity = restTemplate.getForEntity(url, JSONObject.class);
JSONObject body = forEntity.getBody();

JSONObject datas = body.getJSONObject("datas");
if(Objects.nonNull(datas)){
System.out.println(datas);
}
System.out.println(forEntity.getBody());
ForwardDTO dto = new ForwardDTO();
Map<String,Object> map = Maps.newHashMap();
map.put("dingCode",orgCode);
map.put("matterName",(StringUtils.isNotBlank(businessName)?businessName:StringUtils.EMPTY));
map.put("useState",1);
map.put("limit",limit);
map.put("page",page);
map.put("appKey",appKey);
dto.setData(map);
dto.setUrl(coreBizUrl);
dto.setSecret(secret);
dto.setAppKey(appKey);
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HttpEntity<ForwardDTO> formEntity = new HttpEntity<ForwardDTO>(dto, headers);
String url = "http://10.53.168.41:38888/pm/api/v1/zwdd/pull/forward";
ResponseEntity<String> forEntity = restTemplate.postForEntity(url,formEntity, String.class);
String body = forEntity.getBody();
System.out.println(body);;
}
}

+ 6
- 0
syn-data-task/pom.xml View File

@@ -112,6 +112,12 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>

<!-- 打包 -->


+ 1
- 0
syn-data-task/src/main/java/com/ningdatech/syndataapi/common/constant/BizConst.java View File

@@ -102,4 +102,5 @@ public interface BizConst {
String OP_INSERT = "insert";
String OP_UPDATE = "update";
String OP_DELETE = "delete";
String OP_FAIL = "操作失败";
}

+ 85
- 0
syn-data-task/src/main/java/com/ningdatech/syndataapi/common/utils/CryptUtils.java View File

@@ -0,0 +1,85 @@
package com.ningdatech.syndataapi.common.utils;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* 加密工具
* @author hank
*/
public class CryptUtils {
/**
* 默认的算法
*/
private static final String DE_KEY_MAC = "HmacMD5";
/**
* 默认的字符集
*/
private static final Charset DE_CHARSET = StandardCharsets.UTF_8;
/**
* 使用默认的算法(HmacMD5) 得到hmac 16进制字符串
* @param inputStr 需要加密的串
* @param key key
* @return 16进制字符串
* @throws Exception
*/
public static String encryptHMAC(String inputStr, String key) throws Exception {
return encryptHMAC(inputStr, key, DE_KEY_MAC);
}
/**
* 使用指定的算法得到hmac 16进制字符串
* @param inputStr 需要加密的串
* @param key key
* @param keyMac hmac算法
* @return 16进制字符串
* @throws Exception
*/
public static String encryptHMAC(String inputStr, String key, String keyMac) throws Exception {
return Hex.encodeHexString(encryptHMAC(inputStr.getBytes(DE_CHARSET), key, keyMac));
}
public static String MD5Encode(String origin) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
return Hex.encodeHexString(md.digest(origin.getBytes(DE_CHARSET)));
}
private static byte[] encryptHMAC(byte[] data, String key, String keyMac) throws Exception {
SecretKey secretKey = new SecretKeySpec(key.getBytes(DE_CHARSET), keyMac);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(data);
}

/**
* 生成HMAC-MD5值
* @param data 消息数据
* @param key 密钥
* @return HMAC-MD5值
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
public static byte[] hmacMd5(byte[] data, byte[] key)
throws NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec signingKey = new SecretKeySpec(key, "HmacMD5");
Mac mac = Mac.getInstance("HmacMD5");
mac.init(signingKey);
return mac.doFinal(data);
}
public static void main(String[] args) throws Exception{
System.out.println("HMACStr:\n" + encryptHMAC("a", "hank"));
}
}

+ 10
- 0
syn-data-task/src/main/java/com/ningdatech/syndataapi/open/controller/ProjectReceiveController.java View File

@@ -1,7 +1,9 @@
package com.ningdatech.syndataapi.open.controller;

import com.ningdatech.basic.model.ApiResponse;
import com.ningdatech.syndataapi.open.manage.IrsManage;
import com.ningdatech.syndataapi.open.manage.ProjectReceiveManage;
import com.ningdatech.syndataapi.scheduler.model.dto.ForwardDTO;
import com.ningdatech.syndataapi.scheduler.model.dto.ProjectBaseInfoDTO;
import com.ningdatech.syndataapi.scheduler.model.dto.ProjectSaveDTO;
import io.swagger.annotations.Api;
@@ -25,6 +27,8 @@ public class ProjectReceiveController {

private final ProjectReceiveManage receiveManage;

private final IrsManage irsManage;

@PostMapping("/save")
@ApiOperation("项目归集接收")
public ApiResponse<String> save(@Valid @RequestBody ProjectSaveDTO dto) {
@@ -42,4 +46,10 @@ public class ProjectReceiveController {
public ApiResponse<String> deleteAll() {
return ApiResponse.ofSuccess(receiveManage.deleteAll());
}

@ApiOperation(value = "转发IRS请求", notes = "转发IRS请求 用于")
@PostMapping("/forward")
public String forward(@Valid @RequestBody ForwardDTO dto) {
return irsManage.forward(dto);
}
}

+ 56
- 0
syn-data-task/src/main/java/com/ningdatech/syndataapi/open/manage/IrsManage.java View File

@@ -0,0 +1,56 @@
package com.ningdatech.syndataapi.open.manage;

import com.google.common.base.Joiner;
import com.ningdatech.syndataapi.common.constant.BizConst;
import com.ningdatech.syndataapi.common.utils.CryptUtils;
import com.ningdatech.syndataapi.scheduler.model.dto.ForwardDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
* @Classname IrsManage
* @Description
* @Date 2023/9/1 15:15
* @Author PoffyZhang
*/
@Component
@Slf4j
public class IrsManage {


/**
* 转发IRS请求
* @param dto
* @return
*/
public String forward(ForwardDTO dto) {
try{
String url = dto.getUrl();
String appkey = dto.getAppKey();
long timestamp = System.currentTimeMillis();
String requestSecret = dto.getSecret();
String sign = CryptUtils.MD5Encode(appkey + requestSecret + timestamp);
url = url + "?requestTime=" + timestamp + "&sign=" + sign + asUrlParams(dto.getData());
log.info("url :{}",url);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
log.info(forEntity.getBody());
return forEntity.getBody();
}catch (Exception e){
log.error("e:" + e);
}
return BizConst.OP_FAIL;
}

public static String asUrlParams(Map<String, Object> source){
return Joiner.on("&")
// 用指定符号代替空值,key 或者value 为null都会被替换
.useForNull("")
.withKeyValueSeparator("=")
.join(source);
}
}

+ 26
- 0
syn-data-task/src/main/java/com/ningdatech/syndataapi/scheduler/model/dto/ForwardDTO.java View File

@@ -0,0 +1,26 @@
package com.ningdatech.syndataapi.scheduler.model.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Map;

/**
* @Classname ForwardDTO
* @Description
* @Date 2023/7/13 18:03
* @Author PoffyZhang
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ForwardDTO implements Serializable {
private static final long serialVersionUID = 1L;

private String url;
private String secret;
private String appKey;
private Map<String,Object> data;
}

Loading…
Cancel
Save