Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down Expand Up @@ -375,6 +379,11 @@
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/starrier/common/utils/encrypt/AESUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
150 changes: 150 additions & 0 deletions src/main/java/org/starrier/common/utils/encrypt/GZipUtils.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
106 changes: 106 additions & 0 deletions src/main/java/org/starrier/common/utils/request/HttpsUtils.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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
* <blockquote><pre>
* List<NameValuePair> 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));
* </pre></blockquote>
* @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());
}

}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @author starrier
* @date 2021/1/25
*/
package org.starrier.common.utils.request;
Original file line number Diff line number Diff line change
@@ -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");
}
}