调用b端接口增加签名 日志打印格式

master
youys 3 years ago
parent b03c6684f2
commit 4acc706021

@ -32,6 +32,9 @@ public class CallbackController {
@Autowired
private RedisUtil redisUtil;
private final Object LOCK = new Object();
private static final String RESULT = "success";
private static List<String> ERROR_MSG_LIST = null;
@ -64,7 +67,7 @@ public class CallbackController {
// 通知之后清空
redisUtil.remove(incrKey);
return RESULT;
} else if(incr == 0L){
} else if (incr == 0L) {
log.info("tpiID:{},评测通过", tpiID);
}
@ -78,14 +81,28 @@ public class CallbackController {
* @return
*/
private boolean matchOutPut(String output) {
if (ERROR_MSG_LIST == null) {
ERROR_MSG_LIST = Arrays.asList(propertiesConfig.getErrorOutPuts().split("\n"));
}
for (String s : ERROR_MSG_LIST) {
for (String s : getErrorOutputList()) {
if (output.contains(s)) {
return true;
}
}
return false;
}
/**
* .
*
* @return list
*/
private List<String> getErrorOutputList() {
if (ERROR_MSG_LIST == null) {
synchronized (LOCK) {
if (ERROR_MSG_LIST == null) {
ERROR_MSG_LIST = Arrays.asList(propertiesConfig.getErrorOutPuts().split("\n"));
}
}
}
return ERROR_MSG_LIST;
}
}

@ -1,11 +1,13 @@
package net.educoder.schedule;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.educoder.config.PropertiesConfig;
import net.educoder.model.AutoEvaParamConfig;
import net.educoder.service.AutoEvaParamConfigService;
import net.educoder.util.SignatureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
@ -35,6 +37,10 @@ public class EvaCheckWarningTask {
@Autowired
private AutoEvaParamConfigService autoEvaParamConfigService;
@Value("${bridge.sign.accessKeySecret}")
private String accessKeySecret;
@Async
@Scheduled(cron = "${task.cron.evalCheck}")
public void check() {
@ -75,7 +81,7 @@ public class EvaCheckWarningTask {
private void postEvaluation(AutoEvaParamConfig autoEvaParamConfig) {
JSONObject extrasObject = JSONObject.parseObject(autoEvaParamConfig.getExtras());
Map<String, Object> param = new HashMap<>();
Map<String, Object> param = new HashMap<>(32);
param.put("isPublished", extrasObject.getInteger("isPublished"));
param.put("trimBlank", extrasObject.getInteger("trimBlank"));
param.put("containers", extrasObject.getString("containers"));
@ -92,6 +98,14 @@ public class EvaCheckWarningTask {
param.put("buildID", autoEvaParamConfig.getBuildId());
param.put("sec_key", UUID.randomUUID().toString());
param.put("callBackUrl", propertiesConfig.getCallbackUrl());
param.put("ak", RandomUtil.randomString(16));
param.put("nonce", RandomUtil.randomString(16));
String sign = SignatureUtil.genSignature(accessKeySecret, param);
log.info("postEvaluation---------签名值sign:{}", sign);
param.put("sign", sign);
String result = HttpUtil.post(propertiesConfig.getGameEvaluationUrl(), param);
JSONObject jsonResult = JSONObject.parseObject(result);
log.info("实训评测接口返回:{},tpiId:{}", jsonResult, autoEvaParamConfig.getTpiId());
@ -103,12 +117,20 @@ public class EvaCheckWarningTask {
* @param autoEvaParamConfig
*/
private void postOjEvaluation(AutoEvaParamConfig autoEvaParamConfig) {
Map<String, Object> param = new HashMap<>(8);
Map<String, Object> param = new HashMap<>(16);
param.put("tpiID", autoEvaParamConfig.getTpiId());
param.put("testCases", autoEvaParamConfig.getTestCases());
param.put("codeFileContent", autoEvaParamConfig.getCodeFileContent());
param.put("sec_key", UUID.randomUUID().toString());
param.put("callBackUrl", propertiesConfig.getCallbackUrl());
param.put("ak", RandomUtil.randomString(16));
param.put("nonce", RandomUtil.randomString(16));
String sign = SignatureUtil.genSignature(accessKeySecret, param);
log.info("postEvaluation---------签名值:{}", sign);
param.put("sign", sign);
String result = HttpUtil.post(propertiesConfig.getOjEvaluationUrl(), param);
JSONObject jsonResult = JSONObject.parseObject(result);
log.info("Oj评测接口返回:{},tpiId:{}", jsonResult, autoEvaParamConfig.getTpiId());

@ -0,0 +1,47 @@
package net.educoder.util;
import org.springframework.util.DigestUtils;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;
/**
* @Author: youys
* @Date: 2022/4/20
* @Description:
*/
public class SignatureUtil {
/**
* .
*
* @param secretKey
* @param params
* @return
* @throws UnsupportedEncodingException
*/
public static String genSignature(String secretKey, Map<String, Object> params) {
if (secretKey == null || params == null || params.size() == 0) {
return "";
}
// 1. 参数名按照ASCII码表升序排序
String[] keys = params.keySet().toArray(new String[0]);
Arrays.sort(keys);
// 2. 按照排序拼接参数名与参数值
StringBuffer paramBuffer = new StringBuffer();
for (String key : keys) {
paramBuffer.append("&" + key).append(params.get(key) == null ? "" : "=" + params.get(key));
}
// 3. 将secretKey拼接到最后
paramBuffer = paramBuffer.append("&sk=" + secretKey);
String pa = paramBuffer.substring(1);
// 4. MD5是128位长度的摘要算法用16进制表示一个十六进制的字符能表示4个位所以签名后的字符串长度固定为32个十六进制字符。
try {
return DigestUtils.md5DigestAsHex(pa.getBytes("UTF-8")).toUpperCase();
} catch (UnsupportedEncodingException e) {
return "";
}
}
}

@ -26,6 +26,8 @@ bridge:
当前实验环境正在更新中,请稍后重试或联系系统管理员!
当前网络较差,代码下载超时,请稍后重试!
评测脚本设置异常,建议您在实训的配置页面重新选择或修改评测脚本
sign:
accessKeySecret: 9NMU8ushmFu8SN1EKHOhvo9jmv1qp0
# 定时任务cron表达式
task:
cron:

@ -26,6 +26,8 @@ bridge:
当前实验环境正在更新中,请稍后重试或联系系统管理员!
当前网络较差,代码下载超时,请稍后重试!
评测脚本设置异常,建议您在实训的配置页面重新选择或修改评测脚本
sign:
accessKeySecret: 9NMU8ushmFu8SN1EKHOhvo9jmv1qp0
# 定时任务cron表达式
task:
cron:

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- <springProperty scope="context" name="logPath" source="log.path"/>-->
<property name="logPattern" value="%d{MM-dd HH:mm:ss} %-5level [%thread] %c -- %msg%n"/>
<property name="logPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %c -- %msg%n"/>
<property name="logPath" value="logs/"/>
<!-- 标准输出 -->

Loading…
Cancel
Save