高德加签测试
This commit is contained in:
parent
df4f952240
commit
5827c98851
|
@ -22,6 +22,7 @@ import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SignUtils {
|
public class SignUtils {
|
||||||
|
|
||||||
|
@ -95,7 +96,7 @@ public class SignUtils {
|
||||||
|
|
||||||
Object value = method.invoke(obj);
|
Object value = method.invoke(obj);
|
||||||
|
|
||||||
if (value != null && !fieldName.equals("sign")) { // Skip the 'sign' field
|
if (value != null && !fieldName.equals("sign") && !"ent".equals(fieldName) && !"keyt".equals(fieldName)) { // Skip the 'sign' field
|
||||||
// URL-encode the value to handle special characters
|
// URL-encode the value to handle special characters
|
||||||
// String encodedValue = URLEncoder.encode(value.toString(), StandardCharsets.UTF_8);
|
// String encodedValue = URLEncoder.encode(value.toString(), StandardCharsets.UTF_8);
|
||||||
joiner.add(fieldName + "=" + value);
|
joiner.add(fieldName + "=" + value);
|
||||||
|
@ -112,6 +113,7 @@ public class SignUtils {
|
||||||
|
|
||||||
//不加签的参数名
|
//不加签的参数名
|
||||||
private final static List<String> UN_SIGN_KEYS = ImmutableList.of("keyt", "ent", "sign");
|
private final static List<String> UN_SIGN_KEYS = ImmutableList.of("keyt", "ent", "sign");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1、过滤不加签参数
|
* 1、过滤不加签参数
|
||||||
* 2、对参数的key值按ASCII码自然排序
|
* 2、对参数的key值按ASCII码自然排序
|
||||||
|
@ -122,7 +124,7 @@ public class SignUtils {
|
||||||
* @param paramsMap
|
* @param paramsMap
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String calculateSign(Map<String, String> paramsMap,String signKey) {
|
public static String calculateSign(Map<String, String> paramsMap, String signKey) {
|
||||||
String originParamStr = paramsMap.entrySet().stream()
|
String originParamStr = paramsMap.entrySet().stream()
|
||||||
.filter(entry -> !UN_SIGN_KEYS.contains(entry.getKey()))
|
.filter(entry -> !UN_SIGN_KEYS.contains(entry.getKey()))
|
||||||
.sorted(Comparator.comparing(Entry::getKey))
|
.sorted(Comparator.comparing(Entry::getKey))
|
||||||
|
|
|
@ -23,6 +23,10 @@ public class AmapUserRequest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserHttpRequest userHttpRequest;
|
private UserHttpRequest userHttpRequest;
|
||||||
|
|
||||||
|
//应用ID
|
||||||
|
@Value("${amap.appId}")
|
||||||
|
private String appId;
|
||||||
|
|
||||||
@Value("${amap.eId}")
|
@Value("${amap.eId}")
|
||||||
private String eId;
|
private String eId;
|
||||||
|
|
||||||
|
@ -35,7 +39,7 @@ public class AmapUserRequest {
|
||||||
public BaseResponse createUser(User user) {
|
public BaseResponse createUser(User user) {
|
||||||
String createUserUrl = BaseUrl + USER_CREATE_PATH;
|
String createUserUrl = BaseUrl + USER_CREATE_PATH;
|
||||||
BaseRequestParam baseRequestParam = createUserParamByUser(user).generateSign(signKey);
|
BaseRequestParam baseRequestParam = createUserParamByUser(user).generateSign(signKey);
|
||||||
return postRequest.post(createUserUrl, baseRequestParam, BaseResponse.class);
|
return postRequest.postSend(createUserUrl, baseRequestParam, BaseResponse.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,6 +63,9 @@ public class AmapUserRequest {
|
||||||
|
|
||||||
public UserParam createUserParamByUser(User user) {
|
public UserParam createUserParamByUser(User user) {
|
||||||
UserParam userParam = new UserParam();
|
UserParam userParam = new UserParam();
|
||||||
|
userParam.setAppId(appId);
|
||||||
|
userParam.setEnt(4);
|
||||||
|
userParam.setTimestamp(String.valueOf(System.currentTimeMillis()));
|
||||||
userParam.setEid(eId);
|
userParam.setEid(eId);
|
||||||
userParam.setUserId(user.getEmployeeNo());
|
userParam.setUserId(user.getEmployeeNo());
|
||||||
userParam.setEmail(user.getEmail());
|
userParam.setEmail(user.getEmail());
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class AmapRequest {
|
||||||
baseRequestParam.setTimestamp(String.valueOf(System.currentTimeMillis()));
|
baseRequestParam.setTimestamp(String.valueOf(System.currentTimeMillis()));
|
||||||
//将sign值添加到请求参数中
|
//将sign值添加到请求参数中
|
||||||
baseRequestParam.generateSign(signKey);
|
baseRequestParam.generateSign(signKey);
|
||||||
return request.post(url, baseRequestParam, responseType);
|
return request.postSend(url, baseRequestParam, responseType);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ package com.chint.interfaces.rest.amap.token;
|
||||||
import com.chint.interfaces.rest.amap.BaseResponse;
|
import com.chint.interfaces.rest.amap.BaseResponse;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class TokenResponse extends BaseResponse {
|
public class TokenResponse extends BaseResponse {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.chint.interfaces.rest.base;
|
package com.chint.interfaces.rest.base;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -8,8 +10,10 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
@ -18,13 +22,17 @@ import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static com.chint.infrastructure.constant.CommonMessageConstant.REDIRECT_NOT_EXIST;
|
import static com.chint.infrastructure.constant.CommonMessageConstant.REDIRECT_NOT_EXIST;
|
||||||
|
|
||||||
|
@ -124,6 +132,68 @@ public class PostRequest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public <T> T postSend(String url, Object requestObject, Class<T> responseType) {
|
||||||
|
HttpPost post = new HttpPost(url);
|
||||||
|
// 将请求对象转换为键值对形式的参数
|
||||||
|
List<NameValuePair> params = new ArrayList<>();
|
||||||
|
addFieldsToList(params, requestObject);
|
||||||
|
try {
|
||||||
|
// 设置请求体
|
||||||
|
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
||||||
|
// 设置请求头部
|
||||||
|
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
|
// 执行请求
|
||||||
|
HttpResponse response = client.execute(post);
|
||||||
|
HttpEntity responseEntity = response.getEntity();
|
||||||
|
String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
|
||||||
|
log.info("{}",responseBody);
|
||||||
|
// 将响应体转换为指定类型的对象并返回
|
||||||
|
return gson.fromJson(responseBody, responseType);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addFieldsToList(List<NameValuePair> params, Object object) {
|
||||||
|
Class<?> clazz = object.getClass();
|
||||||
|
Field[] fields = clazz.getDeclaredFields();
|
||||||
|
//获取上一级父类的字段
|
||||||
|
Class<?> parentClazz = clazz.getSuperclass();
|
||||||
|
Field[] parentFields = parentClazz.getDeclaredFields();
|
||||||
|
List<Field> fieldArrayList = new ArrayList<>(fields.length + parentFields.length);
|
||||||
|
fieldArrayList.addAll(Arrays.asList(fields));
|
||||||
|
fieldArrayList.addAll(Arrays.asList(parentFields));
|
||||||
|
for (Field field : fieldArrayList) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
try {
|
||||||
|
Object value = field.get(object);
|
||||||
|
if (value != null) {
|
||||||
|
if (!isSimpleType(field.getType())) {
|
||||||
|
// 如果是数组类型,将其转换为 JSON 格式的字符串
|
||||||
|
params.add(new BasicNameValuePair(field.getName(), gson.toJson(value)));
|
||||||
|
} else {
|
||||||
|
// 其他类型直接转换为字符串
|
||||||
|
params.add(new BasicNameValuePair(field.getName(), String.valueOf(value)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("字段转换异常:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSimpleType(Class<?> type) {
|
||||||
|
return type.isPrimitive() ||
|
||||||
|
type.equals(String.class) ||
|
||||||
|
Number.class.isAssignableFrom(type) ||
|
||||||
|
type.equals(Boolean.class) ||
|
||||||
|
type.equals(Character.class) ||
|
||||||
|
type.equals(BigDecimal.class) ||
|
||||||
|
type.equals(BigInteger.class);
|
||||||
|
}
|
||||||
|
|
||||||
public <T> T postANBPM(String url, Object jsonRequest, Class<T> responseType) {
|
public <T> T postANBPM(String url, Object jsonRequest, Class<T> responseType) {
|
||||||
HttpPost post = new HttpPost(url);
|
HttpPost post = new HttpPost(url);
|
||||||
String json = JSON.toJSONString(jsonRequest);
|
String json = JSON.toJSONString(jsonRequest);
|
||||||
|
|
Loading…
Reference in New Issue