mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-23 04:25:21 +08:00
移动utils
This commit is contained in:
@@ -41,10 +41,10 @@ import com.jsowell.thirdparty.common.CommonService;
|
||||
import com.jsowell.thirdparty.lianlian.domain.*;
|
||||
import com.jsowell.pile.thirdparty.CommonParamsDTO;
|
||||
import com.jsowell.thirdparty.lianlian.service.LianLianService;
|
||||
import com.jsowell.thirdparty.lianlian.util.Cryptos;
|
||||
import com.jsowell.thirdparty.lianlian.util.Encodes;
|
||||
import com.jsowell.thirdparty.lianlian.util.GBSignUtils;
|
||||
import com.jsowell.thirdparty.lianlian.util.HttpRequestUtil;
|
||||
import com.jsowell.thirdparty.platform.util.Cryptos;
|
||||
import com.jsowell.thirdparty.platform.util.Encodes;
|
||||
import com.jsowell.thirdparty.platform.util.GBSignUtils;
|
||||
import com.jsowell.thirdparty.platform.util.HttpRequestUtil;
|
||||
import com.jsowell.thirdparty.lianlian.vo.*;
|
||||
import com.jsowell.thirdparty.yongchengboche.dto.YCBCGetTokenDTO;
|
||||
import com.jsowell.thirdparty.yongchengboche.service.YCBCService;
|
||||
|
||||
@@ -1,263 +0,0 @@
|
||||
/**
|
||||
*/
|
||||
package com.jsowell.thirdparty.lianlian.util;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 数据加/解密工具类
|
||||
*/
|
||||
public class Cryptos {
|
||||
|
||||
private static final String AES = "AES";
|
||||
private static final String AES_CBC = "AES/CBC/PKCS5Padding";
|
||||
private static final String HMACSHA1 = "HmacSHA1";
|
||||
|
||||
private static final String DEFAULT_URL_ENCODING = "UTF-8";
|
||||
private static final int DEFAULT_HMACSHA1_KEYSIZE = 160; //RFC2401
|
||||
private static final int DEFAULT_AES_KEYSIZE = 128;
|
||||
private static final int DEFAULT_IVSIZE = 16;
|
||||
|
||||
private static final byte[] DEFAULT_KEY = new byte[]{-97,88,-94,9,70,-76,126,25,0,3,-20,113,108,28,69,125};
|
||||
|
||||
private static SecureRandom random = new SecureRandom();
|
||||
|
||||
//-- HMAC-SHA1 funciton --//
|
||||
/**
|
||||
* 使用HMAC-SHA1进行消息签名, 返回字节数组,长度为20字节.
|
||||
*
|
||||
* @param input 原始输入字符数组
|
||||
* @param key HMAC-SHA1密钥
|
||||
*/
|
||||
public static byte[] hmacSha1(byte[] input, byte[] key) {
|
||||
try {
|
||||
SecretKey secretKey = new SecretKeySpec(key, HMACSHA1);
|
||||
Mac mac = Mac.getInstance(HMACSHA1);
|
||||
mac.init(secretKey);
|
||||
return mac.doFinal(input);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验HMAC-SHA1签名是否正确.
|
||||
*
|
||||
* @param expected 已存在的签名
|
||||
* @param input 原始输入字符串
|
||||
* @param key 密钥
|
||||
*/
|
||||
public static boolean isMacValid(byte[] expected, byte[] input, byte[] key) {
|
||||
byte[] actual = hmacSha1(input, key);
|
||||
return Arrays.equals(expected, actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成HMAC-SHA1密钥,返回字节数组,长度为160位(20字节).
|
||||
* HMAC-SHA1算法对密钥无特殊要求, RFC2401建议最少长度为160位(20字节).
|
||||
*/
|
||||
public static byte[] generateHmacSha1Key() {
|
||||
try {
|
||||
KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1);
|
||||
keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE);
|
||||
SecretKey secretKey = keyGenerator.generateKey();
|
||||
return secretKey.getEncoded();
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
//-- AES funciton --//
|
||||
|
||||
/**
|
||||
* 使用AES加密原始字符串.
|
||||
*
|
||||
* @param input 原始输入字符数组
|
||||
*/
|
||||
public static String aesEncrypt(String input) {
|
||||
try {
|
||||
return Encodes.encodeHex(aesEncrypt(input.getBytes(DEFAULT_URL_ENCODING), DEFAULT_KEY));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES加密原始字符串.
|
||||
*
|
||||
* @param input 原始输入字符数组
|
||||
* @param key 符合AES要求的密钥
|
||||
*/
|
||||
public static String aesEncrypt(String input, String key) {
|
||||
try {
|
||||
return Encodes.encodeHex(aesEncrypt(input.getBytes(DEFAULT_URL_ENCODING), Encodes.decodeHex(key)));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES加密原始字符串.
|
||||
*
|
||||
* @param input 原始输入字符数组
|
||||
* @param key 符合AES要求的密钥
|
||||
*/
|
||||
public static byte[] aesEncrypt(byte[] input, byte[] key) {
|
||||
return aes(input, key, Cipher.ENCRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
* @param input
|
||||
* @param key
|
||||
* @param iv
|
||||
* @return
|
||||
*/
|
||||
public static String aesEncrypt(String input, String key, String iv) {
|
||||
byte[] aes = aes(input.getBytes(), key.getBytes(), iv.getBytes(), Cipher.ENCRYPT_MODE);
|
||||
return Encodes.encodeBase64(aes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES加密原始字符串.
|
||||
*
|
||||
* @param input 原始输入字符数组
|
||||
* @param key 符合AES要求的密钥
|
||||
* @param iv 初始向量
|
||||
*/
|
||||
public static byte[] aesEncrypt(byte[] input, byte[] key, byte[] iv) {
|
||||
return aes(input, key, iv, Cipher.ENCRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES解密字符串, 返回原始字符串.
|
||||
*
|
||||
* @param input Hex编码的加密字符串
|
||||
*/
|
||||
public static String aesDecrypt(String input) {
|
||||
try {
|
||||
return new String(aesDecrypt(Encodes.decodeHex(input), DEFAULT_KEY), DEFAULT_URL_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES解密字符串, 返回原始字符串.
|
||||
*
|
||||
* @param input Hex编码的加密字符串
|
||||
* @param key 符合AES要求的密钥
|
||||
*/
|
||||
public static String aesDecrypt(String input, String key) {
|
||||
try {
|
||||
return new String(aesDecrypt(Encodes.decodeHex(input), Encodes.decodeHex(key)), DEFAULT_URL_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES解密字符串, 返回原始字符串.
|
||||
*
|
||||
* @param input Hex编码的加密字符串
|
||||
* @param key 符合AES要求的密钥
|
||||
*/
|
||||
public static byte[] aesDecrypt(byte[] input, byte[] key) {
|
||||
return aes(input, key, Cipher.DECRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES解密字符串, 返回原始字符串.
|
||||
*
|
||||
* @param input Hex编码的加密字符串
|
||||
* @param key 符合AES要求的密钥
|
||||
* @param iv 初始向量
|
||||
*/
|
||||
public static byte[] aesDecrypt(byte[] input, byte[] key, byte[] iv) {
|
||||
return aes(input, key, iv, Cipher.DECRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
|
||||
*
|
||||
* @param input 原始字节数组
|
||||
* @param key 符合AES要求的密钥
|
||||
* @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
|
||||
*/
|
||||
private static byte[] aes(byte[] input, byte[] key, int mode) {
|
||||
try {
|
||||
SecretKey secretKey = new SecretKeySpec(key, AES);
|
||||
Cipher cipher = Cipher.getInstance(AES);
|
||||
cipher.init(mode, secretKey);
|
||||
return cipher.doFinal(input);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
|
||||
*
|
||||
* @param input 原始字节数组
|
||||
* @param key 符合AES要求的密钥
|
||||
* @param iv 初始向量
|
||||
* @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
|
||||
*/
|
||||
private static byte[] aes(byte[] input, byte[] key, byte[] iv, int mode) {
|
||||
try {
|
||||
SecretKey secretKey = new SecretKeySpec(key, AES);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
Cipher cipher = Cipher.getInstance(AES_CBC);
|
||||
cipher.init(mode, secretKey, ivSpec);
|
||||
return cipher.doFinal(input);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成AES密钥,返回字节数组, 默认长度为128位(16字节).
|
||||
*/
|
||||
public static String generateAesKeyString() {
|
||||
return Encodes.encodeHex(generateAesKey(DEFAULT_AES_KEYSIZE));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成AES密钥,返回字节数组, 默认长度为128位(16字节).
|
||||
*/
|
||||
public static byte[] generateAesKey() {
|
||||
return generateAesKey(DEFAULT_AES_KEYSIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成AES密钥,可选长度为128,192,256位.
|
||||
*/
|
||||
public static byte[] generateAesKey(int keysize) {
|
||||
try {
|
||||
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
|
||||
keyGenerator.init(keysize);
|
||||
SecretKey secretKey = keyGenerator.generateKey();
|
||||
return secretKey.getEncoded();
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机向量,默认大小为cipher.getBlockSize(), 16字节.
|
||||
*/
|
||||
public static byte[] generateIV() {
|
||||
byte[] bytes = new byte[DEFAULT_IVSIZE];
|
||||
random.nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2005-2012 springside.org.cn
|
||||
*/
|
||||
package com.jsowell.thirdparty.lianlian.util;
|
||||
|
||||
import org.apache.commons.codec.DecoderException;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class Encodes {
|
||||
|
||||
private static final String DEFAULT_URL_ENCODING = "UTF-8";
|
||||
private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
/**
|
||||
* Hex编码.
|
||||
*/
|
||||
public static String encodeHex(byte[] input) {
|
||||
return new String(Hex.encodeHex(input));
|
||||
}
|
||||
|
||||
/**
|
||||
* Hex解码.
|
||||
*/
|
||||
public static byte[] decodeHex(String input) {
|
||||
try {
|
||||
return Hex.decodeHex(input.toCharArray());
|
||||
} catch (DecoderException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64编码.
|
||||
*/
|
||||
public static String encodeBase64(byte[] input) {
|
||||
return new String(Base64.encodeBase64(input));
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64编码.
|
||||
*/
|
||||
public static String encodeBase64(String input) {
|
||||
try {
|
||||
return new String(Base64.encodeBase64(input.getBytes(DEFAULT_URL_ENCODING)));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548).
|
||||
// */
|
||||
// public static String encodeUrlSafeBase64(byte[] input) {
|
||||
// return Base64.encodeBase64URLSafe(input);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Base64解码.
|
||||
*/
|
||||
public static byte[] decodeBase64(String input) {
|
||||
return Base64.decodeBase64(input.getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64解码.
|
||||
*/
|
||||
public static String decodeBase64String(String input) {
|
||||
try {
|
||||
return new String(Base64.decodeBase64(input.getBytes()), DEFAULT_URL_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base62编码。
|
||||
*/
|
||||
public static String encodeBase62(byte[] input) {
|
||||
char[] chars = new char[input.length];
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
chars[i] = BASE62[((input[i] & 0xFF) % BASE62.length)];
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Html 转码.
|
||||
*/
|
||||
public static String escapeHtml(String html) {
|
||||
return StringEscapeUtils.escapeHtml4(html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Html 解码.
|
||||
*/
|
||||
public static String unescapeHtml(String htmlEscaped) {
|
||||
return StringEscapeUtils.unescapeHtml4(htmlEscaped);
|
||||
}
|
||||
|
||||
/**
|
||||
* Xml 转码.
|
||||
*/
|
||||
public static String escapeXml(String xml) {
|
||||
return StringEscapeUtils.escapeXml10(xml);
|
||||
}
|
||||
|
||||
/**
|
||||
* Xml 解码.
|
||||
*/
|
||||
public static String unescapeXml(String xmlEscaped) {
|
||||
return StringEscapeUtils.unescapeXml(xmlEscaped);
|
||||
}
|
||||
|
||||
/**
|
||||
* URL 编码, Encode默认为UTF-8.
|
||||
*/
|
||||
public static String urlEncode(String part) {
|
||||
try {
|
||||
return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* URL 解码, Encode默认为UTF-8.
|
||||
*/
|
||||
public static String urlDecode(String part) {
|
||||
|
||||
try {
|
||||
return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.jsowell.thirdparty.lianlian.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* 关于异常的工具类.
|
||||
*/
|
||||
public class Exceptions {
|
||||
private static Logger logger = LoggerFactory.getLogger(Exceptions.class);
|
||||
|
||||
/**
|
||||
* 将CheckedException转换为UncheckedException.
|
||||
*/
|
||||
public static RuntimeException unchecked(Exception e) {
|
||||
if (e instanceof RuntimeException) {
|
||||
return (RuntimeException) e;
|
||||
} else {
|
||||
return new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将ErrorStack转化为String.
|
||||
*/
|
||||
public static String getStackTraceAsString(Throwable e) {
|
||||
if (e == null) {
|
||||
return "";
|
||||
}
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(stringWriter));
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断异常是否由某些底层的异常引起.
|
||||
*/
|
||||
public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses) {
|
||||
Throwable cause = ex.getCause();
|
||||
while (cause != null) {
|
||||
for (Class<? extends Exception> causeClass : causeExceptionClasses) {
|
||||
if (causeClass.isInstance(cause)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
cause = cause.getCause();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在request中获取异常类
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static Throwable getThrowable(HttpServletRequest request) {
|
||||
Throwable ex = null;
|
||||
if (request.getAttribute("exception") != null) {
|
||||
ex = (Throwable) request.getAttribute("exception");
|
||||
} else if (request.getAttribute("javax.servlet.error.exception") != null) {
|
||||
ex = (Throwable) request.getAttribute("javax.servlet.error.exception");
|
||||
}
|
||||
return ex;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package com.jsowell.thirdparty.lianlian.util;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
public class GBSignUtils {
|
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(GBSignUtils.class);
|
||||
|
||||
public static String sign(Map<String, String> paramValues, String secret) {
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String value : paramValues.values()) {
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
sb.append(value);
|
||||
}
|
||||
}
|
||||
logger.debug("需要签名的内容:{},密钥{}", sb.toString(), secret);
|
||||
byte[] md5Digest = HmacMD5Encrypt(sb.toString(), secret);
|
||||
String result = Encodes.encodeHex(md5Digest).toUpperCase();
|
||||
logger.debug("HmacSHA1的签名内容:{}", result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("数据签名出错", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 HMAC-MD5 签名方法对对encryptText进行签名
|
||||
*
|
||||
* @param encryptText
|
||||
* 被签名的字符串
|
||||
* @param encryptKey
|
||||
* 密钥
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] HmacMD5Encrypt(String encryptText, String encryptKey) throws Exception {
|
||||
byte[] data = encryptKey.getBytes(StandardCharsets.UTF_8);
|
||||
// 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
|
||||
SecretKey secretKey = new SecretKeySpec(data, "HmacMD5");
|
||||
// 生成一个指定 Mac 算法 的 Mac 对象
|
||||
Mac mac = Mac.getInstance("HmacMD5");
|
||||
// 用给定密钥初始化 Mac 对象
|
||||
mac.init(secretKey);
|
||||
|
||||
byte[] text = encryptText.getBytes(StandardCharsets.UTF_8);
|
||||
// 完成 Mac 操作
|
||||
return mac.doFinal(text);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,307 +0,0 @@
|
||||
package com.jsowell.thirdparty.lianlian.util;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.jsowell.common.enums.thirdparty.ThirdPartyOperatorIdEnum;
|
||||
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
|
||||
import com.jsowell.common.util.DateUtils;
|
||||
import com.jsowell.common.util.StringUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 联联充电
|
||||
*/
|
||||
@Slf4j
|
||||
@SuppressWarnings(value = "unused")
|
||||
public class HttpRequestUtil {
|
||||
|
||||
|
||||
/**
|
||||
* httpClient--post请求--http
|
||||
*
|
||||
* @param url
|
||||
* @param json
|
||||
* @return
|
||||
*/
|
||||
public static String httpPost(String url, String json, String token) {
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
//创建httpPost
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
//设置Content-Type
|
||||
httpPost.setHeader("Content-Type", "application/json");
|
||||
httpPost.setHeader("Authorization", token);
|
||||
|
||||
//写入json数据
|
||||
httpPost.setEntity(new StringEntity(json));
|
||||
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
.setConnectTimeout(50000)
|
||||
.setConnectionRequestTimeout(50000)
|
||||
.setSocketTimeout(50000)
|
||||
.build();
|
||||
httpPost.setConfig(requestConfig);
|
||||
|
||||
//发起请求,获取response对象
|
||||
response = httpClient.execute(httpPost);
|
||||
|
||||
//获取结果状态码
|
||||
int resultCode = response.getStatusLine().getStatusCode();
|
||||
if (resultCode == 200) {
|
||||
//获取返回数据实体对象
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
//转化为字符串
|
||||
String result = EntityUtils.toString(entity, "UTF-8");
|
||||
//封装统一的返回数据接收类
|
||||
//ResponseMsg responseMsg = (ResponseMsg) JSON.parse(result);
|
||||
return result;
|
||||
} else {
|
||||
log.info("http请求失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.info("http请求异常");
|
||||
} finally {
|
||||
try {
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
*
|
||||
* @param token 令牌
|
||||
* @param data 要传输的JsonString格式数据
|
||||
* @param url 请求地址
|
||||
* @param dataSecret 消息密钥
|
||||
* @param dataSecretIV 消息密钥初始化向量
|
||||
* @param operatorId 运营商id
|
||||
* @param sigSecret 签名密钥
|
||||
* @return
|
||||
*/
|
||||
public static String sendPost(String token, String data, String url, String dataSecret,
|
||||
String dataSecretIV, String operatorId, String sigSecret) {
|
||||
return HttpRequestUtil.sendPost(token, data, url, dataSecret, dataSecretIV, operatorId, sigSecret, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
*
|
||||
* @param token 令牌
|
||||
* @param data 要传输的JsonString格式数据
|
||||
* @param url 请求地址
|
||||
* @param dataSecret 消息密钥
|
||||
* @param dataSecretIV 消息密钥初始化向量
|
||||
* @param operatorId 运营商id
|
||||
* @param sigSecret 签名密钥
|
||||
* @return
|
||||
*/
|
||||
public static String sendPost(String token, String data, String url, String dataSecret,
|
||||
String dataSecretIV, String operatorId, String sigSecret,
|
||||
String thirdPlatformType) {
|
||||
String type = ThirdPartyOperatorIdEnum.getTypeByOperatorId(operatorId);
|
||||
String label = ThirdPlatformTypeEnum.getTypeLabelByTypeCode(type);
|
||||
if(StringUtils.isBlank(label)) {
|
||||
label = operatorId + "(" + url + ")";
|
||||
}
|
||||
log.info(label + "发送请求 data:{}", data);
|
||||
//加密
|
||||
byte[] encryptText = Cryptos.aesEncrypt(data.getBytes(),
|
||||
dataSecret.getBytes(), dataSecretIV.getBytes());
|
||||
String encryptData = Encodes.encodeBase64(encryptText);
|
||||
log.info(label + "发送请求 加密数据:" + encryptData);
|
||||
|
||||
Map<String, String> params = Maps.newLinkedHashMap();
|
||||
params.put("OperatorID", operatorId);
|
||||
params.put("Data", encryptData);
|
||||
params.put("TimeStamp", DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date()));
|
||||
params.put("Seq", "001");
|
||||
|
||||
// 此处是与其他获取令牌方法唯一不同之处,甬城泊车获取令牌需要添加此字段
|
||||
if (ThirdPlatformTypeEnum.YONG_CHENG_BO_CHE.getTypeCode().equals(thirdPlatformType)) {
|
||||
params.put("Portname", "wcc-pro");
|
||||
}
|
||||
|
||||
String sign = GBSignUtils.sign(params, sigSecret);
|
||||
params.put("Sig", sign);
|
||||
|
||||
String postData = JSON.toJSONString(params);
|
||||
log.info(label + "发送请求 最终提交数据:{}, 加密数据:{}", params, postData);
|
||||
// System.out.println("最终提交数据:" + postData);
|
||||
|
||||
String hutoolRequest = HttpRequest.post(url).header("Authorization", "Bearer " + token).body(postData).execute().body();
|
||||
|
||||
log.info("发送请求 接收到返回数据:{}", hutoolRequest);
|
||||
|
||||
if (StringUtils.isBlank(hutoolRequest)) {
|
||||
return "返回数据为空";
|
||||
}
|
||||
Map<String, Object> map = (Map<String, Object>) JSON.parse(hutoolRequest);
|
||||
|
||||
// log.info("联联平台发送请求 返回数据map:{}", JSON.toJSONString(map));
|
||||
|
||||
int ret = (int) map.get("Ret");
|
||||
String resultMsg = (String) map.get("Msg");
|
||||
if (ret != 0) {
|
||||
// 表示请求有异常
|
||||
log.error(label + "发送请求 error:{}, 源数据:{}", resultMsg, data);
|
||||
return resultMsg;
|
||||
}
|
||||
String rData = (String) map.get("Data");
|
||||
|
||||
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(rData),
|
||||
dataSecret.getBytes(), dataSecretIV.getBytes());
|
||||
String plainData = "";
|
||||
try {
|
||||
plainData = new String(plainText, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info(label + "发送请求 返回数据map:{}, 解密数据:{}", JSON.toJSONString(map), plainData);
|
||||
return resultMsg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 江苏平台发送请求
|
||||
*
|
||||
* TODO(和联联平台一样的方法,若测试通过,则统一更换成上面的方法)
|
||||
*
|
||||
* @param token 江苏平台令牌
|
||||
* @param data 要传输的JsonString格式数据
|
||||
* @param url 请求地址
|
||||
* @param dataSecret 消息密钥
|
||||
* @param dataSecretIV 消息密钥初始化向量
|
||||
* @param operatorId 运营商id
|
||||
* @param sigSecret 签名密钥
|
||||
* @return
|
||||
*/
|
||||
public static String nrSendPost(String token, String data, String url, String dataSecret,
|
||||
String dataSecretIV, String operatorId, String sigSecret){
|
||||
log.info("江苏平台发送请求 data:{}", data);
|
||||
//加密
|
||||
byte[] encryptText = Cryptos.aesEncrypt(data.getBytes(),
|
||||
dataSecret.getBytes(), dataSecretIV.getBytes());
|
||||
String encryptData = Encodes.encodeBase64(encryptText);
|
||||
|
||||
Map<String, String> params = Maps.newLinkedHashMap();
|
||||
params.put("OperatorID", operatorId);
|
||||
params.put("Data", encryptData);
|
||||
params.put("TimeStamp", DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date()));
|
||||
params.put("Seq", "001");
|
||||
String sign = GBSignUtils.sign(params, sigSecret);
|
||||
params.put("Sig", sign);
|
||||
|
||||
String postData = JSON.toJSONString(params);
|
||||
log.info("江苏平台发送请求 最终提交数据:{}", postData);
|
||||
|
||||
String hutoolRequest = HttpRequest.post(url).header("Authorization", "Bearer " + token).body(postData).execute().body();
|
||||
|
||||
// log.info("江苏平台发送请求 接收到返回数据:{}", hutoolRequest);
|
||||
if (StringUtils.isBlank(hutoolRequest)) {
|
||||
return "SUCCESS";
|
||||
}
|
||||
Map<String, Object> map = (Map<String, Object>) JSON.parse(hutoolRequest);
|
||||
|
||||
// log.info("江苏平台发送请求 返回数据map:{}", JSON.toJSONString(map));
|
||||
|
||||
int ret = (int) map.get("Ret");
|
||||
String resultMsg = (String) map.get("Msg");
|
||||
if (ret != 0) {
|
||||
// 表示请求有异常
|
||||
log.error("江苏平台发送请求 error:{}, 源数据:{}", resultMsg, data);
|
||||
return resultMsg;
|
||||
}
|
||||
String rData = (String) map.get("Data");
|
||||
|
||||
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(rData),
|
||||
dataSecret.getBytes(), dataSecretIV.getBytes());
|
||||
String plainData = "";
|
||||
try {
|
||||
plainData = new String(plainText, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
log.info("江苏平台发送请求 返回数据map:{}, 解密数据:{}", JSON.toJSONString(map), plainData);
|
||||
return resultMsg;
|
||||
}
|
||||
|
||||
public static String YCBCSendPost(String token, String data, String url, String dataSecret,
|
||||
String dataSecretIV, String operatorId, String sigSecret){
|
||||
log.info("甬城泊车平台发送请求 data:{}", data);
|
||||
//加密
|
||||
byte[] encryptText = Cryptos.aesEncrypt(data.getBytes(),
|
||||
dataSecret.getBytes(), dataSecretIV.getBytes());
|
||||
String encryptData = Encodes.encodeBase64(encryptText);
|
||||
// log.info("甬城泊车平台发送请求 加密数据:" + encryptData);
|
||||
|
||||
Map<String, String> params = Maps.newLinkedHashMap();
|
||||
params.put("OperatorID", operatorId);
|
||||
params.put("Data", encryptData);
|
||||
params.put("TimeStamp", DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date()));
|
||||
params.put("Seq", "001");
|
||||
String sign = GBSignUtils.sign(params, sigSecret);
|
||||
params.put("Portname", "wcc-pro");
|
||||
params.put("Sig", sign);
|
||||
|
||||
String postData = JSON.toJSONString(params);
|
||||
log.info("甬城泊车平台发送请求 最终提交数据:{}, 加密数据:{}", params, postData);
|
||||
|
||||
String hutoolRequest = HttpRequest.post(url).header("Authorization", "Bearer " + token).body(postData).execute().body();
|
||||
|
||||
// log.info("甬城泊车平台发送请求 接收到返回数据:{}", hutoolRequest);
|
||||
|
||||
if (StringUtils.isBlank(hutoolRequest)) {
|
||||
return "返回数据为空";
|
||||
}
|
||||
Map<String, Object> map = (Map<String, Object>) JSON.parse(hutoolRequest);
|
||||
|
||||
// log.info("甬城泊车平台发送请求 返回数据map:{}", JSON.toJSONString(map));
|
||||
|
||||
int ret = (int) map.get("Ret");
|
||||
String resultMsg = (String) map.get("Msg");
|
||||
if (ret != 0) {
|
||||
// 表示请求有异常
|
||||
log.error("甬城泊车平台发送请求 error:{}, 源数据:{}", resultMsg, data);
|
||||
return resultMsg;
|
||||
}
|
||||
String rData = (String) map.get("Data");
|
||||
|
||||
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(rData),
|
||||
dataSecret.getBytes(), dataSecretIV.getBytes());
|
||||
String plainData = "";
|
||||
try {
|
||||
plainData = new String(plainText, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
log.info("甬城泊车平台发送请求 返回数据map:{}, 解密数据:{}", JSON.toJSONString(map), plainData);
|
||||
return resultMsg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user