Authored by tanling

签名方法抽出

... ... @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.yohoufo.common.exception.SignatureNotMatchException;
import com.yohoufo.common.utils.MD5Utils;
import com.yohoufo.common.utils.SignUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -82,7 +83,7 @@ public class BodySignatureCheckInterceptor implements HandlerInterceptor {
throw new SignatureNotMatchException();
}
String cacuSign = this.getSign(bodyParam, privateKey);
String cacuSign = SignUtils.getSign(bodyParam, privateKey);
if(!cacuSign.equalsIgnoreCase(signParam))
{
logger.warn("sign not match. request:{}, caculate:{}", signParam, cacuSign );
... ... @@ -112,46 +113,6 @@ public class BodySignatureCheckInterceptor implements HandlerInterceptor {
return objectMapper.readValue(request.getInputStream(), Map.class);
}
private String getSign(Map<String, Object> bodyParam, String privateKey)throws SignatureNotMatchException
{
//remove some headers
ImmutableList list = ImmutableList.of("sign","business_type");
SortedMap<String,Object> filtedMap = new TreeMap<>();
for(Map.Entry<String,Object> entry : bodyParam.entrySet())
{
String k = entry.getKey();
if(!list.contains(k)){
filtedMap.put(k,entry.getValue());
}
}
//string: k1=v1&k2=v2
List<String> array = new LinkedList<>();
for(Map.Entry<String,Object> entry : filtedMap.entrySet())
{
if (entry.getValue() instanceof ArrayList){
array.add(StringUtils.trim(entry.getKey() + "=" + JSON.toJSONString(entry.getValue())));
}else {
array.add(StringUtils.trim(entry.getKey() + "=" + entry.getValue()));
}
}
String signStr = String.join("&", array);
String sign = "";
try {
sign = MD5Utils.signMd5(signStr + privateKey, "utf-8");
logger.info("signBeforeStr is {}, sign is {}", signStr, sign);
} catch (Exception e) {
logger.error("body param sign failed: {}", e);
throw new SignatureNotMatchException();
}
//sign md5
return sign;
}
public void setIsDebugEnable(boolean isDebugEnable) {
... ...
package com.yohoufo.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.yoho.core.common.utils.MD5;
import com.yohoufo.common.exception.SignatureNotMatchException;
import com.yohoufo.common.interceptor.SecurityInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
public class SignUtils {
private static final Logger logger = LoggerFactory.getLogger(SignUtils.class);
/**
* 获取签名
... ... @@ -29,4 +36,47 @@ public class SignUtils {
String sign = MD5.md5(param+signKey);
return sign;
}
public static String getSign(Map<String, Object> bodyParam, String privateKey)throws SignatureNotMatchException
{
//remove some headers
ImmutableList list = ImmutableList.of("sign","business_type");
SortedMap<String,Object> filtedMap = new TreeMap<>();
for(Map.Entry<String,Object> entry : bodyParam.entrySet())
{
String k = entry.getKey();
if(!list.contains(k)){
filtedMap.put(k,entry.getValue());
}
}
//string: k1=v1&k2=v2
List<String> array = new LinkedList<>();
for(Map.Entry<String,Object> entry : filtedMap.entrySet())
{
if (entry.getValue() instanceof ArrayList){
array.add(StringUtils.trim(entry.getKey() + "=" + JSON.toJSONString(entry.getValue())));
}else {
array.add(StringUtils.trim(entry.getKey() + "=" + entry.getValue()));
}
}
String signStr = String.join("&", array);
String sign = "";
try {
sign = MD5Utils.signMd5(signStr + privateKey, "utf-8");
logger.info("signBeforeStr is {}, sign is {}", signStr, sign);
} catch (Exception e) {
logger.error("body param sign failed: {}", e);
throw new SignatureNotMatchException();
}
//sign md5
return sign;
}
}
... ...