forked from luyan/epmet-cloud-lingshan
28 changed files with 1160 additions and 142 deletions
@ -0,0 +1,19 @@ |
|||
package com.epmet.dto.result; |
|||
|
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @Description 第三方平台及其支持的action result |
|||
* @author wxz |
|||
* @date 2021.08.30 17:06:42 |
|||
*/ |
|||
@Data |
|||
public class ThirdPlatformActionsResultDTO { |
|||
|
|||
private String platformId; |
|||
private String platformKey; |
|||
private String platformName; |
|||
private String actionKey; |
|||
private String apiUrl; |
|||
|
|||
} |
@ -0,0 +1,18 @@ |
|||
package com.epmet.dto.result.privateepmet; |
|||
|
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @Description 获取accesstoken结果 |
|||
* @author wxz |
|||
* @date 2021.08.30 17:40:25 |
|||
*/ |
|||
@Data |
|||
public class GetAccessTokenResultDTO { |
|||
|
|||
// token
|
|||
private String accessToken; |
|||
// 有效时长
|
|||
private Long expireTime; |
|||
|
|||
} |
@ -0,0 +1,169 @@ |
|||
package com.epmet.apiservice.impl; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.TypeReference; |
|||
import com.epmet.apiservice.ApiService; |
|||
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|||
import com.epmet.commons.tools.exception.ExceptionUtils; |
|||
import com.epmet.commons.tools.exception.RenException; |
|||
import com.epmet.commons.tools.feign.ResultDataResolver; |
|||
import com.epmet.commons.tools.utils.ConvertUtils; |
|||
import com.epmet.commons.tools.utils.HttpClientManager; |
|||
import com.epmet.commons.tools.utils.Result; |
|||
import com.epmet.constant.ThirdPlatformActions; |
|||
import com.epmet.dto.form.AuthorizerAccessTokenFormDTO; |
|||
import com.epmet.dto.form.ComponentAccessTokenFormDTO; |
|||
import com.epmet.dto.result.privateepmet.GetAccessTokenResultDTO; |
|||
import com.epmet.openapi.sdk.sign.OpenApiSignUtils; |
|||
import com.epmet.redis.ThirdPlatformCache; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@Component("pingyinPrivateEpmetApiService") |
|||
@Slf4j |
|||
public class PingyinPrivateEpmetApiService extends ApiService<Result> implements ResultDataResolver { |
|||
|
|||
// 认证方式
|
|||
private String authType = "take_token"; |
|||
|
|||
@Override |
|||
public String getAccessTokenHeaderName() { |
|||
return "AccessToken"; |
|||
} |
|||
|
|||
@Override |
|||
public String getAccessToken(String platformId) { |
|||
String accessTokenFromCache = super.getAccessTokenFromCache(platformId); |
|||
if (StringUtils.isBlank(accessTokenFromCache)) { |
|||
ThirdPlatformCache thirdPlatformInfo = super.getThirdPlatformInfo(platformId); |
|||
String apiUrl = getThirdPlatformActionUrl(platformId, ThirdPlatformActions.GET_ACCESS_TOKEN); |
|||
String baseUrl = thirdPlatformInfo.getBaseUrl(); |
|||
String platformKey = thirdPlatformInfo.getPlatformKey(); |
|||
String platformSecret = thirdPlatformInfo.getPlatformSecret(); |
|||
String uuid = UUID.randomUUID().toString(); |
|||
long currentTimeMillis = System.currentTimeMillis(); |
|||
|
|||
try { |
|||
String sign = createSign(platformKey, platformSecret, uuid, currentTimeMillis, authType, null); |
|||
log.info("【调用平阴私有化平台获取AccessToken】参数列表:sign:{}, app_id:{}, auth_type:{}, nonce:{}, timestamp:{}", sign, platformKey, platformSecret, uuid, currentTimeMillis); |
|||
String urlParams = super.convertQueryParams2String(constructCommonUrlParamsMap(platformKey, authType, uuid, currentTimeMillis, sign)); |
|||
String requestUrl = baseUrl.concat(apiUrl).concat(urlParams); |
|||
Result<String> stringResult = HttpClientManager.getInstance().sendPostByHttps(requestUrl, "{}"); |
|||
String remoteResultString = getResultDataOrThrowsException(stringResult, "【调用平阴私有化平台获取AccessToken】", EpmetErrorCode.SERVER_ERROR.getCode(), null); |
|||
Result<GetAccessTokenResultDTO> remoteResult = parsePlatformResponseResult(remoteResultString, GetAccessTokenResultDTO.class); |
|||
log.info("【调用平阴私有化平台获取AccessToken】结果:{}", remoteResultString); |
|||
accessTokenFromCache = remoteResult.getData().getAccessToken(); |
|||
//(15 * 60 * 1000)为提前获取token。对方token有效期2小时,我们设置有效期为1小时45分钟,留15分钟容错时间,防止时间差
|
|||
long expire = remoteResult.getData().getExpireTime() - System.currentTimeMillis() - (15 * 60 * 1000); |
|||
super.addAccessTokenToCache(platformId, accessTokenFromCache, expire, TimeUnit.MILLISECONDS); |
|||
} catch (Exception e) { |
|||
throw new RenException(ExceptionUtils.getErrorStackTrace(e)); |
|||
} |
|||
} |
|||
return accessTokenFromCache; |
|||
} |
|||
|
|||
/** |
|||
* @Description 创建签名 |
|||
* @param platformKey |
|||
* @param platformSecret |
|||
* @param nonce |
|||
* @param timeMillis |
|||
* @param authType |
|||
* @return java.lang.String |
|||
* @author wxz |
|||
* @date 2021.08.31 15:37:58 |
|||
*/ |
|||
private String createSign(String platformKey, String platformSecret, String nonce, Long timeMillis, String authType, Map<String, String> requestBodyMap) { |
|||
HashMap<String, String> contentMap = new HashMap<>(); |
|||
contentMap.put("app_id", platformKey); |
|||
contentMap.put("auth_type", authType); |
|||
contentMap.put("nonce", nonce); |
|||
contentMap.put("timestamp", String.valueOf(timeMillis)); |
|||
|
|||
if (requestBodyMap != null && requestBodyMap.size() > 0) { |
|||
contentMap.putAll(requestBodyMap); |
|||
} |
|||
|
|||
String sign; |
|||
try { |
|||
sign = OpenApiSignUtils.createSign(contentMap, platformSecret); |
|||
} catch (Exception e) { |
|||
String detail = ExceptionUtils.getErrorStackTrace(e); |
|||
log.error(detail); |
|||
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), "【平阴私有化平台apService】创建签名失败"); |
|||
} |
|||
return sign; |
|||
} |
|||
|
|||
/** |
|||
* @Description 构造url参数map |
|||
* @param platformKey |
|||
* @param authType |
|||
* @param nonce |
|||
* @param timeMillis |
|||
* @param sign |
|||
* @return java.util.Map<java.lang.String,java.lang.String> |
|||
* @author wxz |
|||
* @date 2021.08.31 14:19:41 |
|||
*/ |
|||
private Map<String, String> constructCommonUrlParamsMap(String platformKey, String authType, String nonce, Long timeMillis, String sign) { |
|||
HashMap<String, String> map = new HashMap<>(); |
|||
map.put("app_id", platformKey); |
|||
map.put("auth_type", authType); |
|||
map.put("nonce", nonce); |
|||
map.put("timestamp", String.valueOf(timeMillis)); |
|||
map.put("sign", sign); |
|||
return map; |
|||
} |
|||
|
|||
@Override |
|||
public <T> Result<T> parsePlatformResponseResult(String resultString, Class<T> clazz) { |
|||
Result<T> result; |
|||
try { |
|||
result = JSON.parseObject(resultString, new TypeReference<Result<T>>(clazz){}); |
|||
} catch (Exception e) { |
|||
String errorStackTrace = ExceptionUtils.getErrorStackTrace(e); |
|||
log.error(errorStackTrace); |
|||
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), "【调用平阴私有化服务器,解析返回结果失败】"); |
|||
} |
|||
if (!result.success()) { |
|||
throw new RenException(String.format("【请求平阴私有化平台】失败,返回结果string:%s", resultString)); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
public void pushComponentAccessToken(ComponentAccessTokenFormDTO form, String platformId, String platformKey) { |
|||
ThirdPlatformCache thirdPlatformInfo = getThirdPlatformInfo(platformId); |
|||
String nonce = UUID.randomUUID().toString(); |
|||
long timeMillis = System.currentTimeMillis(); |
|||
|
|||
Map<String, String> bodyMap = null; |
|||
try { |
|||
bodyMap = ConvertUtils.entityToMap(form); |
|||
} catch (Exception e) { |
|||
String detail = ExceptionUtils.getErrorStackTrace(e); |
|||
log.error(detail); |
|||
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), "【推送ComponentAccessToken】requestBody转化为map失败"); |
|||
} |
|||
|
|||
String sign = createSign(platformKey, thirdPlatformInfo.getPlatformSecret(), nonce, timeMillis, authType, bodyMap); |
|||
Map<String, String> urlParams = constructCommonUrlParamsMap(platformKey, authType, nonce, timeMillis, sign); |
|||
|
|||
String jsonString = JSON.toJSONString(form); |
|||
Result result = super.sendPostRequest(platformId, ThirdPlatformActions.PUSH_COMPONENT_ACCESS_TOKEN, jsonString, null, urlParams); |
|||
getResultDataOrThrowsException(result, "【平阴私有化平台】推送ComponentAccessToken", EpmetErrorCode.SERVER_ERROR.getCode(), null); |
|||
} |
|||
|
|||
@Override |
|||
public void pushAuthorizerAccessToken(AuthorizerAccessTokenFormDTO form, String platformId, String platformKey) { |
|||
|
|||
} |
|||
} |
@ -0,0 +1,50 @@ |
|||
package com.epmet.controller; |
|||
|
|||
import com.epmet.commons.tools.utils.Result; |
|||
import com.epmet.dto.form.AuthorizerAccessTokenFormDTO; |
|||
import com.epmet.dto.form.ComponentAccessTokenFormDTO; |
|||
import com.epmet.service.PrivateEpmetService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
/** |
|||
* @Description 私有化平台相关controller |
|||
* @author wxz |
|||
* @date 2021.08.30 15:18:51 |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("/private-epmet") |
|||
public class PrivateEpmetController { |
|||
|
|||
@Autowired |
|||
private PrivateEpmetService privateEpmetService; |
|||
|
|||
/** |
|||
* @Description 接收推送component access |
|||
* @param input |
|||
* @return com.epmet.commons.tools.utils.Result |
|||
* @author wxz |
|||
* @date 2021.08.30 15:18:57 |
|||
*/ |
|||
@PostMapping("push-component-access-token") |
|||
public Result pushComponentAccessToken(@RequestBody ComponentAccessTokenFormDTO input) { |
|||
privateEpmetService.pushComponentAccessToken(input); |
|||
return new Result(); |
|||
} |
|||
|
|||
/** |
|||
* @Description 接收推送AuthorizerTokens |
|||
* @param input |
|||
* @return com.epmet.commons.tools.utils.Result |
|||
* @author wxz |
|||
* @date 2021.08.30 16:36:02 |
|||
*/ |
|||
@PostMapping("push-authorizer-tokens") |
|||
public Result pushAuthorizerTokens(@RequestBody AuthorizerAccessTokenFormDTO input) { |
|||
privateEpmetService.pushAuthorizerTokens(input); |
|||
return new Result(); |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
package com.epmet.controller; |
|||
|
|||
import com.epmet.commons.tools.utils.Result; |
|||
import com.epmet.dto.form.ComponentAccessTokenFormDTO; |
|||
import com.epmet.service.ComponentVerifyTicketService; |
|||
import com.epmet.service.PrivateEpmetService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import java.util.Date; |
|||
|
|||
@RestController |
|||
@RequestMapping("test") |
|||
public class TestConttroller { |
|||
|
|||
@Autowired |
|||
private ComponentVerifyTicketService componentVerifyTicketService; |
|||
|
|||
@PostMapping("push-component-access-token") |
|||
public Result testPushComponentAccessToken() { |
|||
ComponentAccessTokenFormDTO form = new ComponentAccessTokenFormDTO(); |
|||
form.setComponentAccessToken("token..."); |
|||
form.setExpiresInTime(new Date()); |
|||
componentVerifyTicketService.pushComponentAccessToken2PrivateEpmetPlatforms(form); |
|||
return new Result(); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,19 @@ |
|||
package com.epmet.redis; |
|||
|
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @Description 第三方平台缓存对象 |
|||
* @author wxz |
|||
* @date 2021.08.31 14:41:49 |
|||
*/ |
|||
@Data |
|||
public class ThirdPlatformCache { |
|||
//String platformKey, String platformSecret, String platformName, String apiService, String baseUrl
|
|||
private String platformId; |
|||
private String platformKey; |
|||
private String platformSecret; |
|||
private String platformName; |
|||
private String apiService; |
|||
private String baseUrl; |
|||
} |
@ -0,0 +1,19 @@ |
|||
package com.epmet.service; |
|||
|
|||
import com.epmet.dto.form.AuthorizerAccessTokenFormDTO; |
|||
import com.epmet.dto.form.ComponentAccessTokenFormDTO; |
|||
|
|||
public interface PrivateEpmetService { |
|||
void pushAuthorizerTokens(AuthorizerAccessTokenFormDTO input); |
|||
|
|||
/** |
|||
* @Description 推送component access token |
|||
* @param input |
|||
* @return void |
|||
* @author wxz |
|||
* @date 2021.08.30 16:17:48 |
|||
*/ |
|||
void pushComponentAccessToken(ComponentAccessTokenFormDTO input); |
|||
|
|||
void pushVerifyTicket(String ticket); |
|||
} |
@ -0,0 +1,71 @@ |
|||
package com.epmet.service.impl; |
|||
|
|||
import com.epmet.constant.ThirdRunTimeInfoConstant; |
|||
import com.epmet.dao.AuthorizationInfoDao; |
|||
import com.epmet.dao.ComponentAccessTokenDao; |
|||
import com.epmet.dao.ComponentVerifyTicketDao; |
|||
import com.epmet.dto.form.AuthorizationInfoFormDTO; |
|||
import com.epmet.dto.form.AuthorizerAccessTokenFormDTO; |
|||
import com.epmet.dto.form.ComponentAccessTokenFormDTO; |
|||
import com.epmet.dto.result.AuthorizationInfoResultDTO; |
|||
import com.epmet.redis.RedisThird; |
|||
import com.epmet.service.PrivateEpmetService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.BeanUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
public class PrivateEpmetServiceImpl implements PrivateEpmetService { |
|||
|
|||
@Autowired |
|||
private ComponentVerifyTicketDao componentVerifyTicketDao; |
|||
|
|||
@Autowired |
|||
private AuthorizationInfoDao authorizationInfoDao; |
|||
|
|||
@Autowired |
|||
private ComponentAccessTokenDao componentAccessTokenDao; |
|||
|
|||
@Autowired |
|||
private RedisThird redisThird; |
|||
|
|||
@Transactional(rollbackFor = RuntimeException.class) |
|||
@Override |
|||
public void pushAuthorizerTokens(AuthorizerAccessTokenFormDTO input) { |
|||
System.out.println("收到AuthorizerAccessTokenFormDTO:" + input); |
|||
|
|||
//AuthorizationInfoFormDTO authorizationInfo = new AuthorizationInfoFormDTO();
|
|||
//BeanUtils.copyProperties(input,authorizationInfo);
|
|||
//authorizationInfo.setAuthorizerAppid(input.getAuthAppid());
|
|||
////先逻辑删除,在插入
|
|||
//authorizationInfoDao.deleteOldAuthorizerAccessToken(input.getCustomerId(), input.getClientType());
|
|||
//authorizationInfoDao.insertAuthorizerAccessToken(input);
|
|||
////缓存 refreshAuthorizerAccessToken
|
|||
//redisThird.setAuthorizerRefreshToken(authorizationInfo);
|
|||
//AuthorizationInfoResultDTO resultDTO = new AuthorizationInfoResultDTO();
|
|||
//resultDTO.setAuthorizer_access_token(input.getAuthorizerAccessToken());
|
|||
//resultDTO.setAuthorizer_refresh_token(input.getAuthorizerRefreshToken());
|
|||
//resultDTO.setAuthorizer_appid(input.getAuthAppid());
|
|||
//resultDTO.setExpires_in(7200);
|
|||
//redisThird.setAuthInfo(resultDTO,input.getCustomerId(),input.getClientType());
|
|||
} |
|||
|
|||
@Override |
|||
@Transactional(rollbackFor = RuntimeException.class) |
|||
public void pushComponentAccessToken(ComponentAccessTokenFormDTO componentAccessToken) { |
|||
System.out.println("收到componentAccessToken:" + componentAccessToken); |
|||
//先逻辑删,在插入
|
|||
//componentAccessTokenDao.deleteOldComponentAccessToken();
|
|||
//componentAccessTokenDao.insertComponentAccessToken(componentAccessToken);
|
|||
////存缓存
|
|||
//redisThird.setComponentAccessToken(componentAccessToken.getComponentAccessToken());
|
|||
} |
|||
|
|||
@Override |
|||
public void pushVerifyTicket(String ticket) { |
|||
|
|||
} |
|||
} |
@ -0,0 +1,15 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<parent> |
|||
<artifactId>epmet-openapi</artifactId> |
|||
<groupId>com.epmet</groupId> |
|||
<version>2.0.0</version> |
|||
</parent> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<artifactId>epmet-openapi-sdk</artifactId> |
|||
|
|||
|
|||
</project> |
@ -0,0 +1,133 @@ |
|||
package com.epmet.openapi.sdk.encrypt; |
|||
|
|||
import java.io.UnsupportedEncodingException; |
|||
import java.security.MessageDigest; |
|||
import java.security.NoSuchAlgorithmException; |
|||
|
|||
public class Md5Util { |
|||
/** |
|||
* 加密盐 值 |
|||
*/ |
|||
public static final String SALT = "EPMET_UMD_SALT"; |
|||
|
|||
public static String md5(String string) throws Exception { |
|||
if (string == null || string.trim().length() == 0) { |
|||
return null; |
|||
} |
|||
return getMD5(string.getBytes("GBK")); |
|||
} |
|||
|
|||
private static final char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
|
|||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
|||
|
|||
private static String getMD5(byte[] source) throws Exception { |
|||
String s = null; |
|||
MessageDigest md = MessageDigest.getInstance("MD5"); |
|||
md.update(source); |
|||
byte tmp[] = md.digest(); // MD5 的计算结果是一个 128 位的长整数,
|
|||
// 用字节表示就是 16 个字节
|
|||
char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符,
|
|||
// 所以表示成 16 进制需要 32 个字符
|
|||
int k = 0; // 表示转换结果中对应的字符位置
|
|||
for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节
|
|||
// 转换成 16 进制字符的转换
|
|||
byte byte0 = tmp[i]; // 取第 i 个字节
|
|||
str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
|
|||
// >>> 为逻辑右移,将符号位一起右移
|
|||
str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
|
|||
} |
|||
s = new String(str); // 换后的结果转换为字符串
|
|||
return s; |
|||
} |
|||
|
|||
private static String byteArrayToHexString(byte b[]) { |
|||
StringBuffer resultSb = new StringBuffer(); |
|||
for (int i = 0; i < b.length; i++) |
|||
resultSb.append(byteToHexString(b[i])); |
|||
|
|||
return resultSb.toString(); |
|||
} |
|||
|
|||
private static String byteToHexString(byte b) { |
|||
int n = b; |
|||
if (n < 0) |
|||
n += 256; |
|||
int d1 = n / 16; |
|||
int d2 = n % 16; |
|||
return hexDigits[d1] + "" + hexDigits[d2]; |
|||
} |
|||
|
|||
public static String MD5Encode(String origin, String charsetname) throws Exception { |
|||
String resultString = null; |
|||
resultString = new String(origin); |
|||
MessageDigest md = MessageDigest.getInstance("MD5"); |
|||
if (charsetname == null || "".equals(charsetname)) |
|||
resultString = byteArrayToHexString(md.digest(resultString |
|||
.getBytes())); |
|||
else |
|||
resultString = byteArrayToHexString(md.digest(resultString |
|||
.getBytes(charsetname))); |
|||
return resultString; |
|||
} |
|||
|
|||
|
|||
public static void main(String[] args) { |
|||
for (int i = 0; i < 5; i++) { |
|||
String uuid = "03a1dcd8cb1811eabac1c03fd56f7847"; |
|||
System.out.println(get12Char(uuid)); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取短字符 |
|||
* |
|||
* @param str |
|||
* @return 大写 |
|||
*/ |
|||
public static String get12Char(String str) { |
|||
String arr[] = ShortText(str); |
|||
String rst = (arr[0] + arr[1]).toUpperCase(); |
|||
return rst.substring(0, 4) + rst.substring(4, 8) + rst.substring(8, 12); |
|||
} |
|||
|
|||
private static String[] ShortText(String string) { |
|||
String[] chars = new String[]{ // 要使用生成URL的字符
|
|||
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", |
|||
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", |
|||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", |
|||
"C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", |
|||
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; |
|||
|
|||
String hex = ""; |
|||
|
|||
MessageDigest md = null; |
|||
try { |
|||
md = MessageDigest.getInstance("MD5"); |
|||
hex = byteArrayToHexString(md.digest(SALT.concat(string) |
|||
.getBytes("utf-8"))); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
int hexLen = hex.length(); |
|||
int subHexLen = hexLen / 8; |
|||
String[] ShortStr = new String[4]; |
|||
|
|||
for (int i = 0; i < subHexLen; i++) { |
|||
String outChars = ""; |
|||
int j = i + 1; |
|||
String subHex = hex.substring(i * 8, j * 8); |
|||
long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16); |
|||
|
|||
for (int k = 0; k < 6; k++) { |
|||
int index = (int) (Long.valueOf("0000003D", 16) & idx); |
|||
outChars += chars[index]; |
|||
idx = idx >> 5; |
|||
} |
|||
ShortStr[i] = outChars; |
|||
} |
|||
|
|||
return ShortStr; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,109 @@ |
|||
package com.epmet.openapi.sdk.sign; |
|||
|
|||
import com.epmet.openapi.sdk.encrypt.Md5Util; |
|||
|
|||
import java.util.*; |
|||
|
|||
/** |
|||
* OpenApi签名工具 |
|||
*/ |
|||
public class OpenApiSignUtils { |
|||
|
|||
/** |
|||
* @Description 创建签名 |
|||
* @return |
|||
* @author wxz |
|||
* @date 2021.03.22 16:47 |
|||
*/ |
|||
public static String createSign(Map<String, String> contentMap, String signKey) throws Exception { |
|||
String str2beSigned = mapToSignStr(contentMap); |
|||
str2beSigned = str2beSigned.concat("&sign_key=").concat(signKey); |
|||
return Md5Util.md5(str2beSigned); |
|||
} |
|||
|
|||
/** |
|||
* @Description 验签 |
|||
* @return |
|||
* @author wxz |
|||
* @date 2021.03.22 16:51 |
|||
*/ |
|||
public static boolean checkSign(Map<String, String> contentMap, String signKey) throws Exception { |
|||
String newSign = createSign(contentMap, signKey); |
|||
return newSign.equals(contentMap.get("sign")); |
|||
} |
|||
|
|||
/** |
|||
* @Description map转化为签名明文 |
|||
* @return |
|||
* @author wxz |
|||
* @date 2021.03.22 16:47 |
|||
*/ |
|||
public static String mapToSignStr(Map<String, String> map) { |
|||
Set<String> keySet = map.keySet(); |
|||
String[] keyArray = (String[])keySet.toArray(new String[keySet.size()]); |
|||
Arrays.sort(keyArray); |
|||
StringBuilder sb = new StringBuilder(); |
|||
|
|||
for(int i = 0; i < keyArray.length; ++i) { |
|||
String key = keyArray[i]; |
|||
String val = (String)map.get(key); |
|||
if (val != null && val.trim().length() > 0 && !"sign".equals(key)) { |
|||
if (!sb.toString().isEmpty()) { |
|||
sb.append("&"); |
|||
} |
|||
|
|||
sb.append(key).append("=").append((String)map.get(key)); |
|||
} |
|||
} |
|||
|
|||
return sb.toString(); |
|||
} |
|||
|
|||
public static void main(String[] args) throws Exception { |
|||
generateGetAccessTokenSign(); |
|||
System.out.println("=============="); |
|||
generateGetOrgDetailSign(); |
|||
} |
|||
|
|||
private static void generateGetAccessTokenSign() throws Exception { |
|||
long now = System.currentTimeMillis(); |
|||
System.out.println(now); |
|||
|
|||
String uuid = UUID.randomUUID().toString().replace("-", ""); |
|||
|
|||
HashMap<String, String> content = new HashMap<>(); |
|||
content.put("app_id", "7d98b8af2d05752b4225709c4cfd4bd0"); |
|||
content.put("timestamp", String.valueOf(now)); |
|||
content.put("nonce", uuid); |
|||
content.put("auth_type", "take_token"); |
|||
|
|||
String secret = "3209ee9f41704482be1a1fb5873a25376f2899191ca846119d44168316bc3e44"; |
|||
|
|||
String sign = createSign(content, secret); |
|||
|
|||
System.out.println("时间戳:" + now); |
|||
System.out.println("随机数:" + uuid); |
|||
System.out.println("签名:" + sign); |
|||
} |
|||
|
|||
private static void generateGetOrgDetailSign() throws Exception { |
|||
long now = System.currentTimeMillis(); |
|||
String uuid = UUID.randomUUID().toString().replace("-", "");; |
|||
System.out.println("时间戳:" + now); |
|||
System.out.println("随机数:" + uuid); |
|||
|
|||
HashMap<String, String> content = new HashMap<>(); |
|||
//content.put("orgId", "aaa");
|
|||
//content.put("test", null);
|
|||
content.put("gridId", "12128e0f614f1c00a058ea9a107033b2"); |
|||
content.put("app_id", "7d98b8af2d05752b4225709c4cfd4bd0"); |
|||
content.put("timestamp", String.valueOf(now)); |
|||
content.put("nonce", uuid); |
|||
content.put("auth_type", "take_token"); |
|||
|
|||
String secret = "3209ee9f41704482be1a1fb5873a25376f2899191ca846119d44168316bc3e44"; |
|||
|
|||
String sign = createSign(content, secret); |
|||
System.out.println("签名:" + sign); |
|||
} |
|||
} |
Loading…
Reference in new issue