优化新增功能

This commit is contained in:
qitz
2024-02-06 15:56:03 +08:00
parent ff175c98df
commit abb96916e3
6 changed files with 655 additions and 8 deletions
@@ -0,0 +1,65 @@
package com.ruoyi.common.utils;
import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class CheckSignatuerUtils {
public static String getSign(String paramsbody, String accessSec, long timestamp) {
String paramsStr = getParamsStr(paramsbody, timestamp);
return generateSign(paramsStr, accessSec);
}
public static String getParamsStr(String paramsbody,long timestamp) {
StringBuilder strbuild = new StringBuilder();
if (StringUtils.isNotBlank(paramsbody)) {
strbuild.append(paramsbody).append('#');
}
strbuild.append("#timestamp=").append(timestamp);
return strbuild.toString();
}
public static String generateSign(String paramsStr, String accessSec) {
HMac hMac = new HMac(HmacAlgorithm.HmacSHA256, accessSec.getBytes(StandardCharsets.UTF_8));
return hMac.digestHex(paramsStr);
}
public static boolean checkSignuter(String paramsbody) throws Exception {
HttpServletRequest reqParam = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
String timestampstr = reqParam.getHeader("timestampstr");
String signstr = reqParam.getHeader("signstr");
String accessSec = "mCFMA6ffe938v79m";
// String bodyParams = getRequestBodyStr(reqParam);
String newSignuter = getSign(paramsbody, accessSec,Long.parseLong(timestampstr));
if (StringUtils.equals(signstr, newSignuter)) {
return true;
}else {
return false;
}
}
// private static String getRequestBodyStr(HttpServletRequest request) throws IOException {
// String body = null;
// if (request instanceof BodyReaderRequestWrapper) {
// body = IoUtil.readUtf8(request.getInputStream());
// }
// return body;
// }
}