cn.bestwu.api.sign.ApiSignAdpter Maven / Gradle / Ivy
package cn.bestwu.api.sign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
/**
* 签名适配
*
* 默认签名算法为MD5
*
* SIGN_TYPE = "MD5";
*
* @author Peter Wu
*/
@Slf4j
public abstract class ApiSignAdpter {
/**
* @param client_id client_id
* @param requestParams 请求参数
* @param timestamp timestamp
* @return 签名
*/
public String sign(Map requestParams, String client_id, String timestamp) {
Map params = new HashMap<>();
for (String name : requestParams.keySet()) {
String[] values = requestParams.get(name);
String valueStr = "";
int length = values.length;
for (int i = 0; i < length; i++) {
valueStr = (i == length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
params.put(name, valueStr);
}
return getSign(params, client_id, timestamp);
}
/**
* @param client_id client_id
* @param requestParams 请求参数
* @param timestamp timestamp
* @return 签名
*/
public String sign(MultiValueMap requestParams, String client_id, String timestamp) {
Map params = new HashMap<>();
for (String name : requestParams.keySet()) {
List