# Conflicts: # common/src/main/java/tech/deepq/common/enums/SignatureEnumGroup.java # common/src/main/java/tech/deepq/common/utils/signature/AbstractSignatureService.java # common/src/main/java/tech/deepq/common/utils/signature/RSASignatureService.java # common/src/main/java/tech/deepq/common/utils/signature/SignatureFactory.java # source/src/main/java/tech/deepq/source/handler/SourceHandler.javafea_dev_2023_09_07_zhidemaiAndQiHuo
@@ -11,6 +11,7 @@ public class SignatureEnumGroup { | |||||
*/ | */ | ||||
public static enum SignatureLocation{ | public static enum SignatureLocation{ | ||||
header, | header, | ||||
body, | |||||
url | url | ||||
} | } | ||||
@@ -19,6 +20,9 @@ public class SignatureEnumGroup { | |||||
* RSA | * RSA | ||||
*/ | */ | ||||
RSA, | RSA, | ||||
/** | |||||
* MD5 | |||||
*/ | |||||
MD5 | MD5 | ||||
} | } | ||||
@@ -15,13 +15,13 @@ public abstract class AbstractSignatureService { | |||||
protected SignatureContext signatureContext; | protected SignatureContext signatureContext; | ||||
/** | /** | ||||
* 加密 | |||||
* 对content进行加密 | |||||
* | * | ||||
* @param content | * @param content | ||||
* @return | * @return | ||||
* @throws GeneralSecurityException | * @throws GeneralSecurityException | ||||
*/ | */ | ||||
public abstract String signature(String content) throws GeneralSecurityException; | |||||
public abstract String signatureContent(String content) throws GeneralSecurityException; | |||||
void init(SignatureConfig signatureConfig) throws GeneralSecurityException { | void init(SignatureConfig signatureConfig) throws GeneralSecurityException { | ||||
@@ -0,0 +1,37 @@ | |||||
package tech.deepq.common.utils.signature; | |||||
import org.springframework.util.DigestUtils; | |||||
import tech.deepq.common.dto.SignatureConfig; | |||||
import java.nio.charset.StandardCharsets; | |||||
import java.security.GeneralSecurityException; | |||||
/** | |||||
* @author yechuan | |||||
* @since 2023/9/1 09:46 | |||||
**/ | |||||
public class MD5SignatureService extends AbstractSignatureService { | |||||
private final static MD5SignatureService instance = new MD5SignatureService(); | |||||
private MD5SignatureService() { | |||||
} | |||||
public static AbstractSignatureService getInstance(SignatureConfig signatureConfig) throws GeneralSecurityException { | |||||
return instance; | |||||
} | |||||
/** | |||||
* 对content进行加密 | |||||
*/ | |||||
@Override | |||||
public String signatureContent(String content) { | |||||
return DigestUtils.md5DigestAsHex((content).getBytes(StandardCharsets.UTF_8)); | |||||
} | |||||
@Override | |||||
void init(SignatureConfig signatureConfig) { | |||||
return; | |||||
} | |||||
} |
@@ -32,7 +32,7 @@ class RSASignatureService extends AbstractSignatureService { | |||||
} | } | ||||
@Override | @Override | ||||
public String signature(String content) throws GeneralSecurityException { | |||||
public String signatureContent(String content) throws GeneralSecurityException { | |||||
if (signatureContext == null) { | if (signatureContext == null) { | ||||
throw new RuntimeException("未初始化公私钥对信息"); | throw new RuntimeException("未初始化公私钥对信息"); | ||||
} | } | ||||
@@ -34,6 +34,7 @@ import tech.deepq.common.utils.signature.SignatureFactory; | |||||
import tech.deepq.source.request.RequestClient; | import tech.deepq.source.request.RequestClient; | ||||
import tech.deepq.source.spel.CustomRequest; | import tech.deepq.source.spel.CustomRequest; | ||||
import tech.deepq.source.spel.ZhiDeMaiRequest; | import tech.deepq.source.spel.ZhiDeMaiRequest; | ||||
import tech.deepq.source.util.TokenUtils; | |||||
import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||
import java.security.GeneralSecurityException; | import java.security.GeneralSecurityException; | ||||
@@ -58,6 +59,7 @@ public class SourceHandler { | |||||
private final TaskManager taskManager; | private final TaskManager taskManager; | ||||
public final Gson gson; | public final Gson gson; | ||||
public final ZhiDeMaiRequest zdm; | public final ZhiDeMaiRequest zdm; | ||||
public final TokenUtils tokenUtils; | |||||
public List<Message<HttpRes>> handle(Message<TaskMiniVO> message) { | public List<Message<HttpRes>> handle(Message<TaskMiniVO> message) { | ||||
TaskMiniVO taskMiniVO = message.getPayload(); | TaskMiniVO taskMiniVO = message.getPayload(); | ||||
@@ -220,6 +222,7 @@ public class SourceHandler { | |||||
return; | return; | ||||
} | } | ||||
} | } | ||||
// 节点命中 | // 节点命中 | ||||
List<Dispatcher.NextNodeConfig> nextNodes = filter.getNextNodes(); | List<Dispatcher.NextNodeConfig> nextNodes = filter.getNextNodes(); | ||||
if (ObjectUtils.isEmpty(nextNodes)) { | if (ObjectUtils.isEmpty(nextNodes)) { | ||||
@@ -354,10 +357,12 @@ public class SourceHandler { | |||||
AbstractSignatureService signatureService = null; | AbstractSignatureService signatureService = null; | ||||
try { | try { | ||||
signatureService = SignatureFactory.getSignatureService(signatureConfig); | signatureService = SignatureFactory.getSignatureService(signatureConfig); | ||||
String signature = signatureService.signature(signatureContext); | |||||
String signature = signatureService.signatureContent(signatureContext); | |||||
// 优化 | // 优化 | ||||
if (SignatureEnumGroup.SignatureLocation.header.equals(signatureConfig.getSignatureLocation())) { | if (SignatureEnumGroup.SignatureLocation.header.equals(signatureConfig.getSignatureLocation())) { | ||||
requestParams.getHeaders().put(signatureConfig.getSignatureField(), signature); | requestParams.getHeaders().put(signatureConfig.getSignatureField(), signature); | ||||
} else if (SignatureEnumGroup.SignatureLocation.body.equals(signatureConfig.getSignatureLocation())) { | |||||
requestParams.getBody().put(signatureConfig.getSignatureField(), signature); | |||||
} else if (SignatureEnumGroup.SignatureLocation.url.equals(signatureConfig.getSignatureLocation())) { | } else if (SignatureEnumGroup.SignatureLocation.url.equals(signatureConfig.getSignatureLocation())) { | ||||
String url = requestParams.getUrl(); | String url = requestParams.getUrl(); | ||||
String separator = ""; | String separator = ""; | ||||
@@ -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); | |||||
} | |||||
} |