From d2619520ebf2f8d6c7ebf721a3b1a0d1b1f3a362 Mon Sep 17 00:00:00 2001 From: dengwc Date: Tue, 16 Apr 2024 16:59:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AB=98=E5=BE=B7=E5=8A=A0=E7=AD=BE=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infrastructure/constant/AmapConstant.java | 3 + .../chint/infrastructure/util/SignUtils.java | 42 +++++++++- .../interfaces/rest/amap/AmapUserRequest.java | 2 +- .../rest/amap/BaseRequestParam.java | 2 +- .../interfaces/rest/amap/BaseResponse.java | 6 +- .../rest/amap/request/AmapRequest.java | 81 +++++++++++++++++++ .../rest/amap/request/TokenRequest.java | 31 +++++++ .../interfaces/rest/amap/token/TokenDto.java | 15 ++++ .../rest/amap/token/TokenResponse.java | 19 +++++ .../interfaces/rest/base/PostRequest.java | 21 +++++ src/test/java/com/chint/AmapTest.java | 20 ++++- 11 files changed, 232 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/chint/interfaces/rest/amap/request/AmapRequest.java create mode 100644 src/main/java/com/chint/interfaces/rest/amap/request/TokenRequest.java create mode 100644 src/main/java/com/chint/interfaces/rest/amap/token/TokenDto.java create mode 100644 src/main/java/com/chint/interfaces/rest/amap/token/TokenResponse.java diff --git a/src/main/java/com/chint/infrastructure/constant/AmapConstant.java b/src/main/java/com/chint/infrastructure/constant/AmapConstant.java index 29524380..d9328c64 100644 --- a/src/main/java/com/chint/infrastructure/constant/AmapConstant.java +++ b/src/main/java/com/chint/infrastructure/constant/AmapConstant.java @@ -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"; } diff --git a/src/main/java/com/chint/infrastructure/util/SignUtils.java b/src/main/java/com/chint/infrastructure/util/SignUtils.java index 0d3cd36c..c6a38adf 100644 --- a/src/main/java/com/chint/infrastructure/util/SignUtils.java +++ b/src/main/java/com/chint/infrastructure/util/SignUtils.java @@ -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 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 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; } } \ No newline at end of file diff --git a/src/main/java/com/chint/interfaces/rest/amap/AmapUserRequest.java b/src/main/java/com/chint/interfaces/rest/amap/AmapUserRequest.java index 998f6b16..7b81da43 100644 --- a/src/main/java/com/chint/interfaces/rest/amap/AmapUserRequest.java +++ b/src/main/java/com/chint/interfaces/rest/amap/AmapUserRequest.java @@ -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; diff --git a/src/main/java/com/chint/interfaces/rest/amap/BaseRequestParam.java b/src/main/java/com/chint/interfaces/rest/amap/BaseRequestParam.java index c06274c9..b86842f6 100644 --- a/src/main/java/com/chint/interfaces/rest/amap/BaseRequestParam.java +++ b/src/main/java/com/chint/interfaces/rest/amap/BaseRequestParam.java @@ -8,7 +8,7 @@ public class BaseRequestParam { // 应用ID,根据业务场景填写 private String appId; // 设备ID,某些业务场景下需要 - private Integer eid; + private String eid; // 企业编号,根据企业分配 private Integer ent; // 请求签名,保障请求安全 diff --git a/src/main/java/com/chint/interfaces/rest/amap/BaseResponse.java b/src/main/java/com/chint/interfaces/rest/amap/BaseResponse.java index a6a33c93..1cc289de 100644 --- a/src/main/java/com/chint/interfaces/rest/amap/BaseResponse.java +++ b/src/main/java/com/chint/interfaces/rest/amap/BaseResponse.java @@ -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,用于日志跟踪等 diff --git a/src/main/java/com/chint/interfaces/rest/amap/request/AmapRequest.java b/src/main/java/com/chint/interfaces/rest/amap/request/AmapRequest.java new file mode 100644 index 00000000..9d4299bf --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/amap/request/AmapRequest.java @@ -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 get(String url, Map params, Class 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 post(String url, BaseRequestParam baseRequestParam, Class 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); + } + +} diff --git a/src/main/java/com/chint/interfaces/rest/amap/request/TokenRequest.java b/src/main/java/com/chint/interfaces/rest/amap/request/TokenRequest.java new file mode 100644 index 00000000..6b63e9a7 --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/amap/request/TokenRequest.java @@ -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); + } +} diff --git a/src/main/java/com/chint/interfaces/rest/amap/token/TokenDto.java b/src/main/java/com/chint/interfaces/rest/amap/token/TokenDto.java new file mode 100644 index 00000000..cfc88bb6 --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/amap/token/TokenDto.java @@ -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;//运力类型 + } +} diff --git a/src/main/java/com/chint/interfaces/rest/amap/token/TokenResponse.java b/src/main/java/com/chint/interfaces/rest/amap/token/TokenResponse.java new file mode 100644 index 00000000..99d02eda --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/amap/token/TokenResponse.java @@ -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;//过期时间 + } +} diff --git a/src/main/java/com/chint/interfaces/rest/base/PostRequest.java b/src/main/java/com/chint/interfaces/rest/base/PostRequest.java index c66b8d16..7dbc07b0 100644 --- a/src/main/java/com/chint/interfaces/rest/base/PostRequest.java +++ b/src/main/java/com/chint/interfaces/rest/base/PostRequest.java @@ -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 getSend(String url, Map paramsMap, Class responseType) { + String responseBody; + try { + StringBuilder getUrl = new StringBuilder(url + "?"); + for (Map.Entry 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 postANBPM(String url, Object jsonRequest, Class responseType) { HttpPost post = new HttpPost(url); String json = JSON.toJSONString(jsonRequest); diff --git a/src/test/java/com/chint/AmapTest.java b/src/test/java/com/chint/AmapTest.java index 1eff4718..fe0da53a 100644 --- a/src/test/java/com/chint/AmapTest.java +++ b/src/test/java/com/chint/AmapTest.java @@ -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); + } }