Browse Source

期货日报数据接入

fea_dev_2023_09_07_zhidemaiAndQiHuo
yechuan 1 year ago
parent
commit
ce85d78c10
8 changed files with 96 additions and 10 deletions
  1. +7
    -2
      common/src/main/java/tech/deepq/common/enums/SignatureEnumGroup.java
  2. +2
    -2
      common/src/main/java/tech/deepq/common/utils/signature/AbstractSignatureService.java
  3. +1
    -1
      common/src/main/java/tech/deepq/common/utils/signature/RSASignatureService.java
  4. +3
    -0
      common/src/main/java/tech/deepq/common/utils/signature/SignatureFactory.java
  5. +6
    -1
      source/src/main/java/tech/deepq/source/handler/SourceHandler.java
  6. +73
    -0
      source/src/main/java/tech/deepq/source/util/TokenUtils.java
  7. +2
    -2
      source/src/main/resources/bootstrap.yml
  8. +2
    -2
      transformer/src/main/resources/bootstrap.yml

+ 7
- 2
common/src/main/java/tech/deepq/common/enums/SignatureEnumGroup.java View File

@@ -10,14 +10,19 @@ public class SignatureEnumGroup {
* 加密字符串的位置
*/
public static enum SignatureLocation{
header
header,
body
}

public static enum KeyAlgorithm{
/**
* RSA
*/
RSA
RSA,
/**
* MD5
*/
MD5
}

public static enum KeyType{


+ 2
- 2
common/src/main/java/tech/deepq/common/utils/signature/AbstractSignatureService.java View File

@@ -15,13 +15,13 @@ public abstract class AbstractSignatureService {
protected SignatureContext signatureContext;

/**
* 使用私钥加密
* 对content进行加密
*
* @param content
* @return
* @throws GeneralSecurityException
*/
public abstract String signatureByPriKey(String content) throws GeneralSecurityException;
public abstract String signatureContent(String content) throws GeneralSecurityException;


void init(SignatureConfig signatureConfig) throws GeneralSecurityException {


+ 1
- 1
common/src/main/java/tech/deepq/common/utils/signature/RSASignatureService.java View File

@@ -32,7 +32,7 @@ class RSASignatureService extends AbstractSignatureService {
}

@Override
public String signatureByPriKey(String content) throws GeneralSecurityException {
public String signatureContent(String content) throws GeneralSecurityException {
if (signatureContext == null) {
throw new RuntimeException("未初始化公私钥对信息");
}


+ 3
- 0
common/src/main/java/tech/deepq/common/utils/signature/SignatureFactory.java View File

@@ -22,6 +22,9 @@ public class SignatureFactory {
case RSA:
result = RSASignatureService.getInstance(signatureConfig);
break;
case MD5:
result = MD5SignatureService.getInstance(signatureConfig);
break;
}
if (result == null) {
throw new RuntimeException(String.format("不支持的签名方式,signatureConfig:%s", signatureConfig));


+ 6
- 1
source/src/main/java/tech/deepq/source/handler/SourceHandler.java View File

@@ -32,6 +32,7 @@ import tech.deepq.common.utils.signature.AbstractSignatureService;
import tech.deepq.common.utils.signature.SignatureFactory;
import tech.deepq.source.request.RequestClient;
import tech.deepq.source.spel.CustomRequest;
import tech.deepq.source.util.TokenUtils;

import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
@@ -55,6 +56,7 @@ public class SourceHandler {
private final StreamBridge streamBridge;
private final TaskManager taskManager;
public final Gson gson;
public final TokenUtils tokenUtils;

public List<Message<HttpRes>> handle(Message<TaskMiniVO> message) {
TaskMiniVO taskMiniVO = message.getPayload();
@@ -164,6 +166,7 @@ public class SourceHandler {

/**
* 根据当前任务返回值与当前任务的分支配置信息构建将要发送的消息
*
* @return {@code Map<topic,list<Message>}
*/
private Map<String, List<Message>> buildNextNodeMessage(List<HttpRes> result, Task task, Message<TaskMiniVO> message) {
@@ -343,10 +346,12 @@ public class SourceHandler {
AbstractSignatureService signatureService = null;
try {
signatureService = SignatureFactory.getSignatureService(signatureConfig);
String signature = signatureService.signatureByPriKey(signatureContext);
String signature = signatureService.signatureContent(signatureContext);
// 优化
if (SignatureEnumGroup.SignatureLocation.header.equals(signatureConfig.getSignatureLocation())) {
requestParams.getHeaders().put(signatureConfig.getSignatureField(), signature);
} else if (SignatureEnumGroup.SignatureLocation.body.equals(signatureConfig.getSignatureLocation())) {
requestParams.getBody().put(signatureConfig.getSignatureField(), signature);
}
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);


+ 73
- 0
source/src/main/java/tech/deepq/source/util/TokenUtils.java View File

@@ -0,0 +1,73 @@
package tech.deepq.source.util;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import tech.deepq.common.dto.HttpRequestParams;
import tech.deepq.common.dto.HttpRes;
import tech.deepq.source.request.RequestClient;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
* @author yechuan
* @since 2023/9/1 09:58
**/
@Component
public class TokenUtils {

@Autowired
private RequestClient requestClient;

@Autowired
private RedisTemplate<String, String> redisTemplate;

@Lazy
@Autowired
private TokenUtils proxy;

private final String TokenFormQHRBCacheKey = "tokenFormQHRBCacheKey";

public String getTokenFormQHRB(String partnerId, String partnerNtr) {
proxy.checkTokenFormQHRB(partnerId, partnerNtr);
return redisTemplate.opsForValue()
.get(TokenFormQHRBCacheKey);
}

@Async
public void checkTokenFormQHRB(String partnerId, String partnerNtr) {
Long expire = redisTemplate.getExpire(TokenFormQHRBCacheKey, TimeUnit.MINUTES);
if (expire == null) {
expire = Long.MIN_VALUE;
}
if (expire.compareTo(15L) > 0) {
return;
}
Map<String, Object> body = new HashMap<>();
String randomNum = UUID.randomUUID().toString().replaceAll("-", "");
randomNum = randomNum.substring(randomNum.length() - 8);
body.put("partnerId", partnerId);
body.put("randomNum", randomNum);
body.put("partnerNtr", DigestUtils.md5DigestAsHex((partnerNtr + randomNum).getBytes(StandardCharsets.UTF_8)));
HttpRequestParams httpRequestParams = new HttpRequestParams();
httpRequestParams.setUrl("https://app.qhrb.com.cn/api/user/partner/getTicket");
httpRequestParams.setBody(body);
httpRequestParams.setMethod("POST");

HttpRes res = requestClient.execute(httpRequestParams);
if (!res.getStatus().is2xxSuccessful()) {
return;
}
redisTemplate.opsForValue().set(TokenFormQHRBCacheKey, JSON.parseObject(res.getBody()).getString("dataPoints"), 120, TimeUnit.MINUTES);
}


}

+ 2
- 2
source/src/main/resources/bootstrap.yml View File

@@ -7,9 +7,9 @@ spring:
nacos:
config:
enabled: true
server-addr: https://nacos-sit.deepq.tech
server-addr: https://nacos-dev.deepq.tech
file-extension: yml
namespace: std-news-process-dev
namespace: std-news-process-local_yechuan
shared-configs:
- data-id: common.yml
refresh: true


+ 2
- 2
transformer/src/main/resources/bootstrap.yml View File

@@ -5,9 +5,9 @@ spring:
nacos:
config:
enabled: true
server-addr: https://nacos-sit.deepq.tech
server-addr: https://nacos-dev.deepq.tech
file-extension: yml
namespace: std-news-process-dev
namespace: std-news-process-local_yechuan
shared-configs:
- data-id: common.yml
refresh: true


Loading…
Cancel
Save