高德加签测试
This commit is contained in:
parent
df4f952240
commit
5827c98851
|
@ -22,6 +22,7 @@ import java.security.NoSuchAlgorithmException;
|
|||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
@Slf4j
|
||||
public class SignUtils {
|
||||
|
||||
|
@ -95,7 +96,7 @@ public class SignUtils {
|
|||
|
||||
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
|
||||
// String encodedValue = URLEncoder.encode(value.toString(), StandardCharsets.UTF_8);
|
||||
joiner.add(fieldName + "=" + value);
|
||||
|
@ -112,6 +113,7 @@ public class SignUtils {
|
|||
|
||||
//不加签的参数名
|
||||
private final static List<String> UN_SIGN_KEYS = ImmutableList.of("keyt", "ent", "sign");
|
||||
|
||||
/**
|
||||
* 1、过滤不加签参数
|
||||
* 2、对参数的key值按ASCII码自然排序
|
||||
|
|
|
@ -23,6 +23,10 @@ public class AmapUserRequest {
|
|||
@Autowired
|
||||
private UserHttpRequest userHttpRequest;
|
||||
|
||||
//应用ID
|
||||
@Value("${amap.appId}")
|
||||
private String appId;
|
||||
|
||||
@Value("${amap.eId}")
|
||||
private String eId;
|
||||
|
||||
|
@ -35,7 +39,7 @@ public class AmapUserRequest {
|
|||
public BaseResponse createUser(User user) {
|
||||
String createUserUrl = BaseUrl + USER_CREATE_PATH;
|
||||
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) {
|
||||
UserParam userParam = new UserParam();
|
||||
userParam.setAppId(appId);
|
||||
userParam.setEnt(4);
|
||||
userParam.setTimestamp(String.valueOf(System.currentTimeMillis()));
|
||||
userParam.setEid(eId);
|
||||
userParam.setUserId(user.getEmployeeNo());
|
||||
userParam.setEmail(user.getEmail());
|
||||
|
|
|
@ -75,7 +75,7 @@ public class AmapRequest {
|
|||
baseRequestParam.setTimestamp(String.valueOf(System.currentTimeMillis()));
|
||||
//将sign值添加到请求参数中
|
||||
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 lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
|
||||
@Data
|
||||
public class TokenResponse extends BaseResponse {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.chint.interfaces.rest.base;
|
||||
|
||||
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.gson.Gson;
|
||||
import lombok.Data;
|
||||
|
@ -8,8 +10,10 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
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.HttpGet;
|
||||
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.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
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.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
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) {
|
||||
HttpPost post = new HttpPost(url);
|
||||
String json = JSON.toJSONString(jsonRequest);
|
||||
|
|
Loading…
Reference in New Issue