Skip to content

Commit

Permalink
Merge pull request #122 from hocgin/develop
Browse files Browse the repository at this point in the history
[新增功能](develop): 用户配置
  • Loading branch information
hocgin authored May 4, 2023
2 parents de7130d + 0b85909 commit 80e0864
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package in.hocg.boot.cps.autoconfiguration.impl.dataoke.lib;


import cn.hutool.core.util.StrUtil;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand All @@ -16,13 +15,14 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.util.Strings;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -33,6 +33,7 @@ public class HttpUtil {
private static PoolingHttpClientConnectionManager cm;
private static String EMPTY_STR = "";
private static String UTF_8 = "UTF-8";

private static void init() {
if (cm == null) {
cm = new PoolingHttpClientConnectionManager();
Expand All @@ -50,18 +51,13 @@ private static void init() {
*/
private static CloseableHttpClient getHttpClient() {
init();
return HttpClients.custom()
.setRetryHandler(new HttpRequestRetryHandler(){
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 1) {
// Do not retry if over max retry count
return false;
}
return true;
}
})
.setConnectionManager(cm).setConnectionManagerShared(true).build();
return HttpClients.custom().setRetryHandler((exception, executionCount, context) -> {
if (executionCount >= 1) {
// Do not retry if over max retry count
return false;
}
return true;
}).setConnectionManager(cm).setConnectionManagerShared(true).build();
}

/**
Expand All @@ -71,14 +67,14 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
public static String httpGetRequest(String url) {
HttpGet httpGet = new HttpGet(url);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000) //设置连接超时时间
.setConnectionRequestTimeout(5000) // 设置请求超时时间
.setSocketTimeout(5000)
.setRedirectsEnabled(false)//默认允许自动重定向
.build();
.setConnectTimeout(5000) //设置连接超时时间
.setConnectionRequestTimeout(5000) // 设置请求超时时间
.setSocketTimeout(5000)
.setRedirectsEnabled(false)//默认允许自动重定向
.build();
httpGet.setConfig(config);
httpGet.setHeader("Accept", "application/json");
httpGet.addHeader("Accept-Encoding" ,"gzip"); //请求使用数据压缩
httpGet.addHeader("Accept-Encoding", "gzip"); //请求使用数据压缩
return getResult(httpGet);
}

Expand All @@ -87,19 +83,19 @@ public static String httpGetRequest(String url, Map<String, String> params) thro
url = url.contains("?") ? url : url + "?";
HttpGet httpGet = new HttpGet(url + paramsStr);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000) //设置连接超时时间
.setConnectionRequestTimeout(5000) // 设置请求超时时间
.setSocketTimeout(5000)
.setRedirectsEnabled(false)//默认允许自动重定向
.build();
.setConnectTimeout(5000) //设置连接超时时间
.setConnectionRequestTimeout(5000) // 设置请求超时时间
.setSocketTimeout(5000)
.setRedirectsEnabled(false)//默认允许自动重定向
.build();
httpGet.setConfig(config);
httpGet.setHeader("Accept", "application/json");
httpGet.addHeader("Accept-Encoding" ,"gzip"); //请求使用数据压缩
httpGet.addHeader("Accept-Encoding", "gzip"); //请求使用数据压缩
return getResult(httpGet);
}

public static String httpGetRequest(String url, Map<String, Object> headers, Map<String, Object> params)
throws URISyntaxException {
throws URISyntaxException {
URIBuilder ub = new URIBuilder();
ub.setPath(url);
ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
Expand All @@ -116,9 +112,9 @@ public static String httpPostRequest(String url) {
return getResult(httpPost);
}

public static String httpPostRequest(String url,String json) {
public static String httpPostRequest(String url, String json) {
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(json,"utf-8");//解决中文乱码问题
StringEntity entity = new StringEntity(json, "utf-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
Expand All @@ -133,7 +129,7 @@ public static String httpPostRequest(String url, Map<String, Object> params) thr
}

public static String httpPostRequest(String url, Map<String, Object> headers, Map<String, Object> params)
throws UnsupportedEncodingException {
throws UnsupportedEncodingException {
HttpPost httpPost = new HttpPost(url);
for (Map.Entry<String, Object> param : headers.entrySet()) {
httpPost.addHeader(param.getKey(), String.valueOf(param.getValue()));
Expand All @@ -145,39 +141,31 @@ public static String httpPostRequest(String url, Map<String, Object> headers, Ma
}

private static ArrayList<NameValuePair> covertParams2NVPS(Map<String, Object> params) {
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
ArrayList<NameValuePair> pairs = new ArrayList<>();
for (Map.Entry<String, Object> param : params.entrySet()) {
pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue())));
}
return pairs;
}

private static ArrayList<NameValuePair> covertParams2NVPSForStr(Map<String, String> params) {
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
ArrayList<NameValuePair> pairs = new ArrayList<>();
for (Map.Entry<String, String> param : params.entrySet()) {
try {
pairs.add(new BasicNameValuePair(param.getKey(), URLEncoder.encode(param.getValue(), "utf-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
pairs.add(new BasicNameValuePair(param.getKey(), HttpUtil.encodeStr(param.getValue())));
}
return pairs;
}

private static String covertParamsForStr(Map<String, String> paraMap) {
if(paraMap == null){
if (paraMap == null) {
paraMap = new HashMap<>();
}
paraMap= new TreeMap<>(paraMap);
paraMap = new TreeMap<>(paraMap);
StringBuilder sb = new StringBuilder();
paraMap.entrySet().stream().forEach(entry ->{
sb.append(entry.getKey());
paraMap.forEach((key, value) -> {
sb.append(key);
sb.append("=");
try {
sb.append(URLEncoder.encode(entry.getValue(),"utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
sb.append(HttpUtil.encodeStr(value));
sb.append("&");
});
return sb.toString();
Expand All @@ -198,15 +186,27 @@ private static String getResult(HttpRequestBase request) {
response.close();
} catch (IOException e) {
//e.printStackTrace();
}finally {
try{
if(httpClient != null){
} finally {
try {
if (httpClient != null) {
httpClient.close(); //释放资源
}
}catch (Exception e){
} catch (Exception ignored) {

}
}
return result;
}


public static String encodeStr(String value) {
if (StrUtil.isBlank(value)) {
return Strings.EMPTY;
}
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8);
} catch (Exception e) {
return Strings.EMPTY;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -20,6 +18,7 @@
* @createTime: 2019/04/24 14:55
* @description:
*/
@Deprecated
public class HttpUtils {

public static String doGet(String url) {
Expand Down Expand Up @@ -56,14 +55,10 @@ public static String sendGet(String getUrl, Map<String, String> paraMap) throws
}
paraMap = new TreeMap<>(paraMap);
StringBuilder sb = new StringBuilder();
paraMap.entrySet().stream().forEach(entry -> {
sb.append(entry.getKey());
paraMap.forEach((key, value) -> {
sb.append(key);
sb.append("=");
try {
sb.append(URLEncoder.encode(entry.getValue(), "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
sb.append(HttpUtil.encodeStr(value));
sb.append("&");
});
getUrl = getUrl.contains("?") ? getUrl : getUrl + "?";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package in.hocg.boot.cps.autoconfiguration.impl.dataoke.lib;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Set;
Expand All @@ -14,13 +15,14 @@ public class SignMD5Util {

/**
* 获取签名的util
* @param map 请求参数
*
* @param map 请求参数
* @param secretKey 密钥
* @return
*/
public static String getSignStr(TreeMap<String,String> map, String secretKey){
public static String getSignStr(TreeMap<String, String> map, String secretKey) {

if (map.size() == 0 ){
if (map.size() == 0) {
return "";
}

Expand All @@ -30,21 +32,22 @@ public static String getSignStr(TreeMap<String,String> map, String secretKey){
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
sb.append("&"+ key + "=" + map.get(key));
sb.append("&" + key + "=" + map.get(key));
}
sb.deleteCharAt(0);
return sign(sb.toString(), secretKey);
}

/**
* 获取签名的util
* @param map 请求参数
*
* @param map 请求参数
* @param secretKey 密钥
* @return
*/
public static String getSignStrNew(TreeMap<String,String> map, String secretKey){
public static String getSignStrNew(TreeMap<String, String> map, String secretKey) {

if (map.size() == 0 ){
if (map.size() == 0) {
return "";
}
StringBuilder sb = new StringBuilder();
Expand All @@ -58,16 +61,14 @@ public static String getSignStrNew(TreeMap<String,String> map, String secretKey)
return sign(sb.toString(), secretKey);
}

public static String sign(String content, String key)
{
public static String sign(String content, String key) {
String signStr = "";
signStr = content + "&key=" + key;
//MD5加密后,字符串所有字符转换为大写
return MD5(signStr).toUpperCase();
}

/**
*
* MD5加密算法
*
* @param s
Expand All @@ -77,15 +78,14 @@ public static String sign(String content, String key)
public final static String MD5(String s) {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
try {
byte[] btInput = s.getBytes("utf-8");
byte[] btInput = s.getBytes(StandardCharsets.UTF_8);
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++)
{
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
Expand Down

0 comments on commit 80e0864

Please sign in to comment.