diff --git a/pom.xml b/pom.xml index 5f4f806..a13cb7f 100644 --- a/pom.xml +++ b/pom.xml @@ -317,6 +317,10 @@ commons-collections4 4.4 + + org.apache.httpcomponents + httpclient + @@ -375,6 +379,11 @@ org.redisson redisson + + org.apache.httpcomponents + httpclient + 4.5.13 + diff --git a/src/main/java/org/starrier/common/utils/encrypt/AESUtils.java b/src/main/java/org/starrier/common/utils/encrypt/AESUtils.java index e42fe0d..0b6f01d 100644 --- a/src/main/java/org/starrier/common/utils/encrypt/AESUtils.java +++ b/src/main/java/org/starrier/common/utils/encrypt/AESUtils.java @@ -10,9 +10,20 @@ import java.util.Base64; /** + * + * + * + * + * + * + * + * + * * @author starrier * @date 2020/12/28 + * @since 0.0.1 */ +@Deprecated public class AESUtils { private static final String SEED = "custom.ase.key"; diff --git a/src/main/java/org/starrier/common/utils/encrypt/GZipUtils.java b/src/main/java/org/starrier/common/utils/encrypt/GZipUtils.java new file mode 100644 index 0000000..fd41a0c --- /dev/null +++ b/src/main/java/org/starrier/common/utils/encrypt/GZipUtils.java @@ -0,0 +1,150 @@ +package org.starrier.common.utils.encrypt; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import static org.springframework.util.StreamUtils.BUFFER_SIZE; + +/** + * @author starrier + * @date 2021/2/4 + */ +public class GZipUtils { + + /** + * GZIP 加密 + * + * @param str + * @return + */ + public static byte[] encryptGZIP(String str) { + if (str == null || str.length() == 0) { + return null; + } + + try { + // gzip压缩 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + GZIPOutputStream gzip = new GZIPOutputStream(baos); + gzip.write(str.getBytes(StandardCharsets.UTF_8)); + + gzip.close(); + + byte[] encode = baos.toByteArray(); + + baos.flush(); + baos.close(); + + // base64 加密 + return encode; +// return new String(encode, "UTF-8"); + + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * GZIP 解密 + * + * @param str + * @return + */ + public static String decryptGZIP(String str) { + if (str == null || str.length() == 0) { + return null; + } + + try { + + byte[] decode = str.getBytes("UTF-8"); + + //gzip 解压缩 + ByteArrayInputStream bais = new ByteArrayInputStream(decode); + GZIPInputStream gzip = new GZIPInputStream(bais); + + byte[] buf = new byte[BUFFER_SIZE]; + int len = 0; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while ((len = gzip.read(buf, 0, BUFFER_SIZE)) != -1) { + baos.write(buf, 0, len); + } + gzip.close(); + baos.flush(); + + decode = baos.toByteArray(); + + baos.close(); + + return new String(decode, StandardCharsets.UTF_8); + + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * 十六进制字符串 转换为 byte[] + * + * @param hexString the hex string + * @return byte[] + */ + public static byte[] hexStringToBytes(String hexString) { + if (hexString == null || hexString.equals("")) { + return null; + } + int length = hexString.length() / 2; + char[] hexChars = hexString.toCharArray(); + byte[] d = new byte[length]; + for (int i = 0; i < length; i++) { + int pos = i * 2; + d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); + } + return d; + } + + /** + * Convert char to byte + * + * @param c char + * @return byte + */ + private static byte charToByte(char c) { + return (byte) "0123456789abcdef".indexOf(c); + // return (byte) "0123456789ABCDEF".indexOf(c); + } + + /** + * byte[] 转换为 十六进制字符串 + * + * @param src + * @return + */ + public static String bytesToHexString(byte[] src) { + StringBuilder stringBuilder = new StringBuilder(""); + + if (src == null || src.length <= 0) { + return null; + } + + for (int i = 0; i < src.length; i++) { + int v = src[i] & 0xFF; + String hv = Integer.toHexString(v); + if (hv.length() < 2) { + stringBuilder.append(0); + } + stringBuilder.append(hv); + } + return stringBuilder.toString(); + } +} diff --git a/src/main/java/org/starrier/common/utils/HttpUtils.java b/src/main/java/org/starrier/common/utils/request/HttpUtils.java similarity index 74% rename from src/main/java/org/starrier/common/utils/HttpUtils.java rename to src/main/java/org/starrier/common/utils/request/HttpUtils.java index 9477de8..c97df29 100644 --- a/src/main/java/org/starrier/common/utils/HttpUtils.java +++ b/src/main/java/org/starrier/common/utils/request/HttpUtils.java @@ -1,4 +1,4 @@ -package org.starrier.common.utils; +package org.starrier.common.utils.request; import com.google.common.collect.Maps; import org.springframework.web.context.request.RequestContextHolder; @@ -58,23 +58,19 @@ public static String getIpAddress() { * @return ip地址 */ public static String getIpAddress(HttpServletRequest request) { - String ip = request.getHeader(X_FORWARDED_FOR); + + String ip = null; + + ip = getIpAddress(request,ip,X_FORWARDED_FOR); + if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { - if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { - ip = request.getHeader(PROXY_CLIENT_IP); - } - if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { - ip = request.getHeader(WL_PROXY_CLIENT_IP); - } - if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { - ip = request.getHeader(HTTP_CLIENT_IP); - } - if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { - ip = request.getHeader(HTTP_X_FORWARDED_FOR); - } - if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { - ip = request.getRemoteAddr(); - } + + ip = getIpAddress(request, ip, PROXY_CLIENT_IP); + ip = getIpAddress(request, ip, WL_PROXY_CLIENT_IP); + ip = getIpAddress(request, ip, HTTP_X_FORWARDED_FOR); + ip = getIpAddress(request, ip, HTTP_CLIENT_IP); + ip = getIpAddress(request, ip, request.getRemoteAddr()); + } else if (ip.length() > 15) { String[] ips = POINT.split(ip); for (String s : ips) { @@ -86,4 +82,11 @@ public static String getIpAddress(HttpServletRequest request) { } return ip; } + + private static String getIpAddress(HttpServletRequest request, String ip, String proxyClientIp) { + if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { + ip = request.getHeader(proxyClientIp); + } + return ip; + } } diff --git a/src/main/java/org/starrier/common/utils/request/HttpsUtils.java b/src/main/java/org/starrier/common/utils/request/HttpsUtils.java new file mode 100644 index 0000000..a58f272 --- /dev/null +++ b/src/main/java/org/starrier/common/utils/request/HttpsUtils.java @@ -0,0 +1,106 @@ +package org.starrier.common.utils.request; + +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpEntity; +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.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.apache.logging.log4j.util.Strings; + +import java.io.IOException; +import java.util.Map; + +/** + * @author starrier + * @date 2021/1/25 + */ +public class HttpsUtils { + + /** + * HTTPS post request using JSON transport + * + * @param url target https url + * @param json transport content with json + * @return request result + * @throws IOException throws exception + */ + public static String sendPostWithJson(String url, String json) throws IOException { + + HttpPost post = new HttpPost(url); + + // send a JSON data + post.setEntity(new StringEntity(json)); + + try (CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpResponse response = httpClient.execute(post)) { + return EntityUtils.toString(response.getEntity()); + } + + } + + /** + * + * @param url + * @param headers + * @return + * @throws IOException + */ + public static String sendGetWithHeader(String url, Map headers) throws IOException { + + HttpGet request = new HttpGet(url); + + try (CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpResponse response = httpClient.execute(request)) { + + // add request headers + if (MapUtils.isNotEmpty(headers)) { + for (String key : headers.keySet()) { + if (StringUtils.isNotBlank(headers.get(key))) { + request.addHeader(key, headers.get(key)); + } + } + } + + HttpEntity entity = response.getEntity(); + if (entity != null) { + return EntityUtils.toString(entity); + } + + return Strings.EMPTY; + } + + } + + /** + * + * add request parameters or form parameters + *
+     *         List urlParameters = new ArrayList<>();
+     *         urlParameters.add(new BasicNameValuePair("username", "abc"));
+     *         urlParameters.add(new BasicNameValuePair("password", "123"));
+     *         urlParameters.add(new BasicNameValuePair("custom", "secret"));
+     *
+     *         post.setEntity(new UrlEncodedFormEntity(urlParameters));
+     * 
+ * @param url + * @return + * @throws IOException + */ + public static String sendPost(String url) throws IOException { + + HttpPost post = new HttpPost(url); + + try (CloseableHttpClient httpClient = HttpClients.createDefault(); + + CloseableHttpResponse response = httpClient.execute(post)){ + + return EntityUtils.toString(response.getEntity()); + } + + } +} diff --git a/src/main/java/org/starrier/common/utils/request/README.md b/src/main/java/org/starrier/common/utils/request/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/org/starrier/common/utils/request/package-info.java b/src/main/java/org/starrier/common/utils/request/package-info.java new file mode 100644 index 0000000..021ac09 --- /dev/null +++ b/src/main/java/org/starrier/common/utils/request/package-info.java @@ -0,0 +1,5 @@ +/** + * @author starrier + * @date 2021/1/25 + */ +package org.starrier.common.utils.request; \ No newline at end of file diff --git a/src/main/test/org/starrier/common/utils/request/HttpsUtilsTest.java b/src/main/test/org/starrier/common/utils/request/HttpsUtilsTest.java new file mode 100644 index 0000000..f0ac781 --- /dev/null +++ b/src/main/test/org/starrier/common/utils/request/HttpsUtilsTest.java @@ -0,0 +1,30 @@ +package org.starrier.common.utils.request; + +import org.apache.http.util.Asserts; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author starrier + * @date 2021/1/25 + */ +class HttpsUtilsTest { + + @Test + void sendPostWithJson() { + } + + @Test + void sendGetWithHeader() { + } + + @Test + void sendPost() throws IOException { + + String starrierRepos = HttpsUtils.sendPost("https://api.github.com/users/Starrier/repos"); + Asserts.notNull(starrierRepos,"not null"); + } +} \ No newline at end of file