高德加签测试
This commit is contained in:
parent
6748fe647e
commit
d2619520eb
|
@ -5,4 +5,7 @@ public class AmapConstant {
|
|||
public static final String USER_UPDATE_PATH = "/ws/boss/enterprise/openapi/employee/query";
|
||||
public static final String USER_QUERY_PATH = "/ws/boss/enterprise/openapi/employee/update";
|
||||
public static final String USER_DELETE_PATH = "/ws/boss/enterprise/openapi/employee/delete";
|
||||
|
||||
//token地址
|
||||
public static final String TOKEN_URL = "/ws/car/open/enterprise/auth/get_token";
|
||||
}
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
package com.chint.infrastructure.util;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.hash.Hashing;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
@ -14,8 +20,9 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Map.Entry;
|
||||
@Slf4j
|
||||
public class SignUtils {
|
||||
|
||||
|
||||
|
@ -96,7 +103,36 @@ public class SignUtils {
|
|||
}
|
||||
|
||||
String res = joiner + "@" + signKey;
|
||||
return Hashing.md5().hashString(res, Charsets.UTF_8).toString().toUpperCase();
|
||||
log.info("参数拼接后的原始签名串:{}", res);
|
||||
String sign = Hashing.md5().hashString(res, Charsets.UTF_8).toString().toUpperCase();
|
||||
log.info("参数签名串:{}", sign);
|
||||
return sign;
|
||||
}
|
||||
|
||||
|
||||
//不加签的参数名
|
||||
private final static List<String> UN_SIGN_KEYS = ImmutableList.of("keyt", "ent", "sign");
|
||||
/**
|
||||
* 1、过滤不加签参数
|
||||
* 2、对参数的key值按ASCII码自然排序
|
||||
* 3、参数按照 key=value&拼接
|
||||
* 4、最后拼接 @signKey
|
||||
* 5、对拼接后的字符串做MD5,得到sign值
|
||||
*
|
||||
* @param paramsMap
|
||||
* @return
|
||||
*/
|
||||
public static String calculateSign(Map<String, String> paramsMap,String signKey) {
|
||||
String originParamStr = paramsMap.entrySet().stream()
|
||||
.filter(entry -> !UN_SIGN_KEYS.contains(entry.getKey()))
|
||||
.sorted(Comparator.comparing(Entry::getKey))
|
||||
.map(entry -> entry.getKey() + "=" + entry.getValue())
|
||||
.collect(Collectors.joining("&"));
|
||||
originParamStr = originParamStr + "@" + signKey;
|
||||
log.info("参数拼接后的原始签名串:{}", originParamStr);
|
||||
String sign = String.valueOf(Hashing.md5().hashString(originParamStr, Charsets.UTF_8)).toUpperCase();
|
||||
log.info("参数签名串:{}", sign);
|
||||
return sign;
|
||||
}
|
||||
|
||||
}
|
|
@ -24,7 +24,7 @@ public class AmapUserRequest {
|
|||
private UserHttpRequest userHttpRequest;
|
||||
|
||||
@Value("${amap.eId}")
|
||||
private Integer eId;
|
||||
private String eId;
|
||||
|
||||
@Value("${amap.baseUrl}")
|
||||
private String BaseUrl;
|
||||
|
|
|
@ -8,7 +8,7 @@ public class BaseRequestParam {
|
|||
// 应用ID,根据业务场景填写
|
||||
private String appId;
|
||||
// 设备ID,某些业务场景下需要
|
||||
private Integer eid;
|
||||
private String eid;
|
||||
// 企业编号,根据企业分配
|
||||
private Integer ent;
|
||||
// 请求签名,保障请求安全
|
||||
|
|
|
@ -4,12 +4,10 @@ import lombok.Data;
|
|||
|
||||
@Data
|
||||
public class BaseResponse {
|
||||
// 响应代码,0表示成功,其他值表示错误
|
||||
// 响应代码,1表示成功,非1表示失败
|
||||
private Integer code;
|
||||
// 响应消息,如"Success"或错误信息
|
||||
// 响应消息,code=1时为"Successful",code!=1时为错误信息
|
||||
private String message;
|
||||
// 返回的数据对象,具体内容根据业务定义
|
||||
private Object data;
|
||||
// 响应时间戳,标记响应生成时间
|
||||
private long timestamp;
|
||||
// 跟踪ID,用于日志跟踪等
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package com.chint.interfaces.rest.amap.request;
|
||||
|
||||
import com.chint.infrastructure.util.SignUtils;
|
||||
import com.chint.interfaces.rest.amap.BaseRequestParam;
|
||||
|
||||
import com.chint.interfaces.rest.amap.BaseResponse;
|
||||
import com.chint.interfaces.rest.base.PostRequest;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
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;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
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.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.chint.infrastructure.constant.AmapConstant.USER_CREATE_PATH;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AmapRequest {
|
||||
|
||||
//应用ID
|
||||
@Value("${amap.appId}")
|
||||
private String appId;
|
||||
|
||||
//企业id
|
||||
@Value("${amap.eId}")
|
||||
private String eid;
|
||||
|
||||
//密钥key
|
||||
@Value("${amap.keyt}")
|
||||
private String keyt;
|
||||
|
||||
@Value("${amap.signKey}")
|
||||
private String signKey;
|
||||
|
||||
@Autowired
|
||||
private PostRequest request;
|
||||
|
||||
public <V> V get(String url, Map<String, String> params, Class<V> responseType) {
|
||||
params.put("appId", appId);
|
||||
params.put("keyt", keyt);
|
||||
params.put("eid", eid);
|
||||
params.put("ent", "4");
|
||||
//构造签名
|
||||
String sign = SignUtils.calculateSign(params, signKey);
|
||||
//将sign值添加到请求参数中
|
||||
params.put("sign", sign);
|
||||
//发送get请求
|
||||
return request.getSend(url, params, responseType);
|
||||
}
|
||||
|
||||
public <V> V post(String url, BaseRequestParam baseRequestParam, Class<V> responseType) {
|
||||
baseRequestParam.setAppId(appId);
|
||||
baseRequestParam.setEid(eid);
|
||||
baseRequestParam.setEnt(4);
|
||||
baseRequestParam.setTimestamp(String.valueOf(System.currentTimeMillis()));
|
||||
//将sign值添加到请求参数中
|
||||
baseRequestParam.generateSign(signKey);
|
||||
return request.post(url, baseRequestParam, responseType);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.chint.interfaces.rest.amap.request;
|
||||
|
||||
|
||||
import com.chint.interfaces.rest.amap.token.TokenDto;
|
||||
import com.chint.interfaces.rest.amap.token.TokenResponse;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.chint.infrastructure.constant.AmapConstant.TOKEN_URL;
|
||||
|
||||
|
||||
@Data
|
||||
@Component
|
||||
public class TokenRequest {
|
||||
@Value("${amap.baseUrl}")
|
||||
private String baseUrl;
|
||||
|
||||
@Autowired
|
||||
private AmapRequest gaoDeRequest;
|
||||
|
||||
public TokenResponse getToken(TokenDto.UserInfo userInfo) {
|
||||
TokenDto tokenDto = new TokenDto();
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(userInfo);
|
||||
tokenDto.setUserInfo(json);
|
||||
return gaoDeRequest.post(baseUrl + TOKEN_URL, tokenDto, TokenResponse.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.chint.interfaces.rest.amap.token;
|
||||
|
||||
import com.chint.interfaces.rest.amap.BaseRequestParam;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TokenDto extends BaseRequestParam {
|
||||
private String userInfo;
|
||||
@Data
|
||||
public static class UserInfo {
|
||||
private String userId;//企业用户ID
|
||||
private String mobile;//企业用户手机号
|
||||
private int[] rideTypes;//运力类型
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.chint.interfaces.rest.amap.token;
|
||||
|
||||
|
||||
import com.chint.interfaces.rest.amap.BaseResponse;
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
@Data
|
||||
public class TokenResponse extends BaseResponse {
|
||||
private Boolean result;
|
||||
private Data data;
|
||||
private String version;
|
||||
|
||||
@lombok.Data
|
||||
public static class Data {
|
||||
private String token;//token
|
||||
private String expiretime;//过期时间
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.chint.interfaces.rest.base;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -9,6 +10,7 @@ import org.apache.http.HttpEntity;
|
|||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
|
@ -20,6 +22,7 @@ import org.apache.http.util.EntityUtils;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -103,6 +106,24 @@ public class PostRequest {
|
|||
}
|
||||
|
||||
|
||||
public <T> T getSend(String url, Map<String, String> paramsMap, Class<T> responseType) {
|
||||
String responseBody;
|
||||
try {
|
||||
StringBuilder getUrl = new StringBuilder(url + "?");
|
||||
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
|
||||
if (param.getValue() != null && !param.getValue().trim().isEmpty()) {
|
||||
getUrl.append(param.getKey()).append("=").append(URLEncoder.encode(param.getValue(), Charsets.UTF_8)).append("&");
|
||||
}
|
||||
}
|
||||
log.info("HttpClientUtil getUrl is {}", getUrl);
|
||||
HttpUriRequest request = new HttpGet(getUrl.toString());
|
||||
responseBody = EntityUtils.toString(client.execute(request).getEntity(), "UTF-8");
|
||||
return gson.fromJson(responseBody, responseType);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T postANBPM(String url, Object jsonRequest, Class<T> responseType) {
|
||||
HttpPost post = new HttpPost(url);
|
||||
String json = JSON.toJSONString(jsonRequest);
|
||||
|
|
|
@ -3,6 +3,9 @@ package com.chint;
|
|||
import com.chint.domain.aggregates.user.User;
|
||||
import com.chint.interfaces.rest.amap.AmapUserRequest;
|
||||
import com.chint.interfaces.rest.amap.BaseResponse;
|
||||
import com.chint.interfaces.rest.amap.token.TokenDto;
|
||||
import com.chint.interfaces.rest.amap.token.TokenResponse;
|
||||
import com.chint.interfaces.rest.amap.request.TokenRequest;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -18,9 +21,24 @@ public class AmapTest {
|
|||
|
||||
private User user = new User(1L, "230615020", 1, "卢麟哲", "1033719135@qq.com", "15857193365", "A30000001");
|
||||
|
||||
@Test
|
||||
// @Test
|
||||
public void createUser(){
|
||||
BaseResponse baseResponse = amapUserRequest.createUser(user);
|
||||
System.out.println(gson.toJson(baseResponse));
|
||||
}
|
||||
|
||||
|
||||
@Autowired
|
||||
private TokenRequest tokenRequest;
|
||||
|
||||
// @Test
|
||||
public void gaoDe() {
|
||||
TokenDto.UserInfo userInfo = new TokenDto.UserInfo();
|
||||
userInfo.setUserId("230615020");
|
||||
userInfo.setMobile("15857193365");
|
||||
userInfo.setRideTypes(new int[]{1, 2});
|
||||
TokenResponse response = tokenRequest.getToken(userInfo);
|
||||
String token = response.getData().getToken();
|
||||
System.out.println("token = " + token);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue