第三方接口对接之鉴权
import com.alibaba.fastjson.JSON;
import com.elab.core.utils.ObjectUtils;
import org.apache.catalina.util.URLEncoder;
import org.apache.commons.net.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanMap;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* 数据对接数据处理
*
* @author : liukx
* @time : 2019/4/22 - 16:24
*/
public class DataUtils {
private static Logger logger = LoggerFactory.getLogger(JMFDataUtils.class);
/**
* 通用参数中要带
*/
public static String accessKey = "xxxxxxxxxxxxxxx";
/**
* 同整个参数进行加密
*/
public static String accessSecret = "xxxxxxxxxxxxxxxxx";
/**
* 将对象装换为map
*
* @param bean
* @return
*/
public static <T> Map<String, Object> beanToMap(Object bean, boolean isFirstCase) {
Map<String, Object> map = new TreeMap<>();
try {
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
for (Object key : beanMap.keySet()) {
if (Objects.nonNull(beanMap.get(key))) {
if (isFirstCase) {
map.put(toUpperCaseFirstOne(key + ""), beanMap.get(key).toString());
} else {
map.put(key + "", beanMap.get(key).toString());
}
}
}
}
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 首字母大写
*
* @param value 值
* @return
*/
public static String toUpperCaseFirstOne(String value) {
if (Character.isUpperCase(value.charAt(0)))
return value;
else
return (new StringBuilder()).append(Character.toUpperCase(value.charAt(0))).append(value.substring(1)).toString();
}
/**
* 鉴权
*
* @param randomString 随机字符串
* @param dateTime 时间
* @param obj 参数
* @param isFirstCase 是否首字母大写
* @return
* @throws Exception
*/
public static String getJMFSignature(String randomString, String dateTime, Object obj, boolean isFirstCase) throws
Exception {
logger.info("明文 : " + JSON.toJSONString(obj));
StringBuffer sb = new StringBuffer();
Map<String, Object> paramsMap = null;
if (obj instanceof Map) {
paramsMap = (Map<String, Object>) obj;
} else {
paramsMap = beanToMap(obj, isFirstCase);
}
// 通过treemap对key的属性进行排序,
TreeMap<String, Object> sortMap = new TreeMap<>(String::compareToIgnoreCase);
sortMap.putAll(paramsMap);
sortMap.put("accessKey", accessKey);
sortMap.put("dateTime", dateTime);
sortMap.put("randomNumbers", randomString);
AtomicBoolean isFirst = new AtomicBoolean(true);
sortMap.forEach((K, V) -> {
if (!isFirst.get()) {
sb.append("&");
}
isFirst.set(false);
if (ObjectUtils.isNotEmpty(V)) {
if (V instanceof List) {
V = JSON.toJSONString(V);
}
sb.append(K + "=" + getEncoderValue(V.toString()).replaceAll("==", "%3D%3D"));
} else {
sb.append(K + "=");
}
});
logger.info("编码后的明文 : " + sb.toString());
byte[] bytes = HMACSHA1.HmacSHA1Encrypt(sb.toString(), accessSecret);
String encode = Base64.encodeBase64String(bytes, false);
logger.info("得到的密钥串 : " + encode);
return encode;
}
private static String getEncoderValue(String value) {
return URLEncoder.QUERY.encode(value, Charset.forName("UTF-8")).toUpperCase();
}
}