优化代码

This commit is contained in:
Richie 2025-04-24 20:43:31 +08:00
parent a9b48463f8
commit bed055fa1d
10 changed files with 12 additions and 1280 deletions

View File

@ -43,7 +43,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.0.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.kafka</groupId>-->
<!-- <artifactId>spring-kafka</artifactId>-->

View File

@ -1,11 +1,11 @@
package cn.qihangerp.sys.feign;
package cn.qihangerp.oms.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
@FeignClient(name = "tao-api")
@FeignClient(name = "open-api")
public interface EchoService {
@GetMapping(value = "/test/na")
String echo(@RequestHeader(name = "Authorization",required = true) String Token);

View File

@ -87,11 +87,11 @@
<!-- <artifactId>spring-cloud-starter-gateway</artifactId>-->
<!-- <version>4.0.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.0.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-openfeign</artifactId>-->
<!-- <version>4.0.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>mysql</groupId>

View File

@ -1,188 +0,0 @@
package cn.qihangerp.sys.utils;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;
/**
* 时间工具类
*
* @author qihang
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/**
* 获取当前Date型日期
*
* @return Date() 当前日期
*/
public static Date getNowDate()
{
return new Date();
}
/**
* 获取当前日期, 默认格式为yyyy-MM-dd
*
* @return String
*/
public static String getDate()
{
return dateTimeNow(YYYY_MM_DD);
}
public static final String getTime()
{
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}
public static final String dateTimeNow()
{
return dateTimeNow(YYYYMMDDHHMMSS);
}
public static final String dateTimeNow(final String format)
{
return parseDateToStr(format, new Date());
}
public static final String dateTime(final Date date)
{
return parseDateToStr(YYYY_MM_DD, date);
}
public static final String parseDateToStr(final String format, final Date date)
{
return new SimpleDateFormat(format).format(date);
}
public static final Date dateTime(final String format, final String ts)
{
try
{
return new SimpleDateFormat(format).parse(ts);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
/**
* 日期路径 即年// 如2018/08/08
*/
public static final String datePath()
{
Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd");
}
/**
* 日期路径 即年// 如20180808
*/
public static final String dateTime()
{
Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd");
}
/**
* 日期型字符串转化为日期 格式
*/
public static Date parseDate(Object str)
{
if (str == null)
{
return null;
}
try
{
return parseDate(str.toString(), parsePatterns);
}
catch (ParseException e)
{
return null;
}
}
/**
* 获取服务器启动时间
*/
public static Date getServerStartDate()
{
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
/**
* 计算相差天数
*/
public static int differentDaysByMillisecond(Date date1, Date date2)
{
return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
}
/**
* 计算时间差
*
* @param endDate 最后时间
* @param startTime 开始时间
* @return 时间差/小时/分钟
*/
public static String timeDistance(Date endDate, Date startTime)
{
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - startTime.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "" + hour + "小时" + min + "分钟";
}
/**
* 增加 LocalDateTime ==> Date
*/
public static Date toDate(LocalDateTime temporalAccessor)
{
ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
/**
* 增加 LocalDate ==> Date
*/
public static Date toDate(LocalDate temporalAccessor)
{
LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
}

View File

@ -1,49 +0,0 @@
package cn.qihangerp.sys.utils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset;
/**
* Redis使用FastJson序列化
*
* @author qihang
*/
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
public FastJson2JsonRedisSerializer(Class<T> clazz)
{
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException
{
if (t == null)
{
return new byte[0];
}
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException
{
if (bytes == null || bytes.length <= 0)
{
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, clazz, JSONReader.Feature.SupportAutoType);
}
}

View File

@ -1,49 +0,0 @@
//package cn.qihangerp.sys.utils;
//
///**
// * ID生成器工具类
// *
// * @author qihang
// */
//public class IdUtils
//{
// /**
// * 获取随机UUID
// *
// * @return 随机UUID
// */
// public static String randomUUID()
// {
// return UUID.randomUUID().toString();
// }
//
// /**
// * 简化的UUID去掉了横线
// *
// * @return 简化的UUID去掉了横线
// */
// public static String simpleUUID()
// {
// return UUID.randomUUID().toString(true);
// }
//
// /**
// * 获取随机UUID使用性能更好的ThreadLocalRandom生成UUID
// *
// * @return 随机UUID
// */
// public static String fastUUID()
// {
// return UUID.fastUUID().toString();
// }
//
// /**
// * 简化的UUID去掉了横线使用性能更好的ThreadLocalRandom生成UUID
// *
// * @return 简化的UUID去掉了横线
// */
// public static String fastSimpleUUID()
// {
// return UUID.fastUUID().toString(true);
// }
//}

View File

@ -1,492 +0,0 @@
package cn.qihangerp.sys.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
* 提供通用唯一识别码universally unique identifierUUID实现
*
* @author qihang
*/
public final class UUID implements java.io.Serializable, Comparable<UUID>
{
private static final long serialVersionUID = -1185015143654744140L;
/**
* SecureRandom 的单例
*
*/
private static class Holder
{
static final SecureRandom numberGenerator;
static {
try {
numberGenerator = getSecureRandom();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/** 此UUID的最高64有效位 */
private final long mostSigBits;
/** 此UUID的最低64有效位 */
private final long leastSigBits;
/**
* 私有构造
*
* @param data 数据
*/
private UUID(byte[] data)
{
long msb = 0;
long lsb = 0;
assert data.length == 16 : "data must be 16 bytes in length";
for (int i = 0; i < 8; i++)
{
msb = (msb << 8) | (data[i] & 0xff);
}
for (int i = 8; i < 16; i++)
{
lsb = (lsb << 8) | (data[i] & 0xff);
}
this.mostSigBits = msb;
this.leastSigBits = lsb;
}
/**
* 使用指定的数据构造新的 UUID
*
* @param mostSigBits 用于 {@code UUID} 的最高有效 64
* @param leastSigBits 用于 {@code UUID} 的最低有效 64
*/
public UUID(long mostSigBits, long leastSigBits)
{
this.mostSigBits = mostSigBits;
this.leastSigBits = leastSigBits;
}
/**
* 获取类型 4伪随机生成的UUID 的静态工厂
*
* @return 随机生成的 {@code UUID}
*/
public static UUID fastUUID()
{
return randomUUID(false);
}
/**
* 获取类型 4伪随机生成的UUID 的静态工厂 使用加密的强伪随机数生成器生成该 UUID
*
* @return 随机生成的 {@code UUID}
*/
public static UUID randomUUID()
{
return randomUUID(true);
}
/**
* 获取类型 4伪随机生成的UUID 的静态工厂 使用加密的强伪随机数生成器生成该 UUID
*
* @param isSecure 是否使用{@link SecureRandom}如果是可以获得更安全的随机码否则可以得到更好的性能
* @return 随机生成的 {@code UUID}
*/
public static UUID randomUUID(boolean isSecure)
{
final Random ng = isSecure ? Holder.numberGenerator : getRandom();
byte[] randomBytes = new byte[16];
ng.nextBytes(randomBytes);
randomBytes[6] &= 0x0f; /* clear version */
randomBytes[6] |= 0x40; /* set to version 4 */
randomBytes[8] &= 0x3f; /* clear variant */
randomBytes[8] |= 0x80; /* set to IETF variant */
return new UUID(randomBytes);
}
/**
* 根据指定的字节数组获取类型 3基于名称的UUID 的静态工厂
*
* @param name 用于构造 UUID 的字节数组
*
* @return 根据指定数组生成的 {@code UUID}
*/
public static UUID nameUUIDFromBytes(byte[] name)
{
MessageDigest md;
try
{
md = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException nsae)
{
throw new InternalError("MD5 not supported");
}
byte[] md5Bytes = md.digest(name);
md5Bytes[6] &= 0x0f; /* clear version */
md5Bytes[6] |= 0x30; /* set to version 3 */
md5Bytes[8] &= 0x3f; /* clear variant */
md5Bytes[8] |= 0x80; /* set to IETF variant */
return new UUID(md5Bytes);
}
/**
* 根据 {@link #toString()} 方法中描述的字符串标准表示形式创建{@code UUID}
*
* @param name 指定 {@code UUID} 字符串
* @return 具有指定值的 {@code UUID}
* @throws IllegalArgumentException 如果 name {@link #toString} 中描述的字符串表示形式不符抛出此异常
*
*/
public static UUID fromString(String name)
{
String[] components = name.split("-");
if (components.length != 5)
{
throw new IllegalArgumentException("Invalid UUID string: " + name);
}
for (int i = 0; i < 5; i++)
{
components[i] = "0x" + components[i];
}
long mostSigBits = Long.decode(components[0]).longValue();
mostSigBits <<= 16;
mostSigBits |= Long.decode(components[1]).longValue();
mostSigBits <<= 16;
mostSigBits |= Long.decode(components[2]).longValue();
long leastSigBits = Long.decode(components[3]).longValue();
leastSigBits <<= 48;
leastSigBits |= Long.decode(components[4]).longValue();
return new UUID(mostSigBits, leastSigBits);
}
/**
* 返回此 UUID 128 位值中的最低有效 64
*
* @return UUID 128 位值中的最低有效 64
*/
public long getLeastSignificantBits()
{
return leastSigBits;
}
/**
* 返回此 UUID 128 位值中的最高有效 64
*
* @return UUID 128 位值中最高有效 64
*/
public long getMostSignificantBits()
{
return mostSigBits;
}
/**
* 与此 {@code UUID} 相关联的版本号. 版本号描述此 {@code UUID} 是如何生成的
* <p>
* 版本号具有以下含意:
* <ul>
* <li>1 基于时间的 UUID
* <li>2 DCE 安全 UUID
* <li>3 基于名称的 UUID
* <li>4 随机生成的 UUID
* </ul>
*
* @return {@code UUID} 的版本号
*/
public int version()
{
// Version is bits masked by 0x000000000000F000 in MS long
return (int) ((mostSigBits >> 12) & 0x0f);
}
/**
* 与此 {@code UUID} 相关联的变体号变体号描述 {@code UUID} 的布局
* <p>
* 变体号具有以下含意
* <ul>
* <li>0 NCS 向后兼容保留
* <li>2 <a href="http://www.ietf.org/rfc/rfc4122.txt">IETF&nbsp;RFC&nbsp;4122</a>(Leach-Salz), 用于此类
* <li>6 保留微软向后兼容
* <li>7 保留供以后定义使用
* </ul>
*
* @return {@code UUID} 相关联的变体号
*/
public int variant()
{
// This field is composed of a varying number of bits.
// 0 - - Reserved for NCS backward compatibility
// 1 0 - The IETF aka Leach-Salz variant (used by this class)
// 1 1 0 Reserved, Microsoft backward compatibility
// 1 1 1 Reserved for future definition.
return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62))) & (leastSigBits >> 63));
}
/**
* 与此 UUID 相关联的时间戳值
*
* <p>
* 60 位的时间戳值根据此 {@code UUID} time_lowtime_mid time_hi 字段构造<br>
* 所得到的时间戳以 100 毫微秒为单位 UTC通用协调时间 1582 10 15 日零时开始
*
* <p>
* 时间戳值仅在在基于时间的 UUID version 类型为 1中才有意义<br>
* 如果此 {@code UUID} 不是基于时间的 UUID则此方法抛出 UnsupportedOperationException
*
* @throws UnsupportedOperationException 如果此 {@code UUID} 不是 version 1 UUID
*/
public long timestamp() throws UnsupportedOperationException
{
checkTimeBase();
return (mostSigBits & 0x0FFFL) << 48//
| ((mostSigBits >> 16) & 0x0FFFFL) << 32//
| mostSigBits >>> 32;
}
/**
* 与此 UUID 相关联的时钟序列值
*
* <p>
* 14 位的时钟序列值根据此 UUID clock_seq 字段构造clock_seq 字段用于保证在基于时间的 UUID 中的时间唯一性
* <p>
* {@code clockSequence} 值仅在基于时间的 UUID version 类型为 1中才有意义 如果此 UUID 不是基于时间的 UUID则此方法抛出
* UnsupportedOperationException
*
* @return {@code UUID} 的时钟序列
*
* @throws UnsupportedOperationException 如果此 UUID version 不为 1
*/
public int clockSequence() throws UnsupportedOperationException
{
checkTimeBase();
return (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48);
}
/**
* 与此 UUID 相关的节点值
*
* <p>
* 48 位的节点值根据此 UUID node 字段构造此字段旨在用于保存机器的 IEEE 802 地址该地址用于生成此 UUID 以保证空间唯一性
* <p>
* 节点值仅在基于时间的 UUID version 类型为 1中才有意义<br>
* 如果此 UUID 不是基于时间的 UUID则此方法抛出 UnsupportedOperationException
*
* @return {@code UUID} 的节点值
*
* @throws UnsupportedOperationException 如果此 UUID version 不为 1
*/
public long node() throws UnsupportedOperationException
{
checkTimeBase();
return leastSigBits & 0x0000FFFFFFFFFFFFL;
}
/**
* 返回此{@code UUID} 的字符串表现形式
*
* <p>
* UUID 的字符串表示形式由此 BNF 描述
*
* <pre>
* {@code
* UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node>
* time_low = 4*<hexOctet>
* time_mid = 2*<hexOctet>
* time_high_and_version = 2*<hexOctet>
* variant_and_sequence = 2*<hexOctet>
* node = 6*<hexOctet>
* hexOctet = <hexDigit><hexDigit>
* hexDigit = [0-9a-fA-F]
* }
* </pre>
*
* </blockquote>
*
* @return {@code UUID} 的字符串表现形式
* @see #toString(boolean)
*/
@Override
public String toString()
{
return toString(false);
}
/**
* 返回此{@code UUID} 的字符串表现形式
*
* <p>
* UUID 的字符串表示形式由此 BNF 描述
*
* <pre>
* {@code
* UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node>
* time_low = 4*<hexOctet>
* time_mid = 2*<hexOctet>
* time_high_and_version = 2*<hexOctet>
* variant_and_sequence = 2*<hexOctet>
* node = 6*<hexOctet>
* hexOctet = <hexDigit><hexDigit>
* hexDigit = [0-9a-fA-F]
* }
* </pre>
*
* </blockquote>
*
* @param isSimple 是否简单模式简单模式为不带'-'的UUID字符串
* @return {@code UUID} 的字符串表现形式
*/
public String toString(boolean isSimple)
{
final StringBuilder builder = new StringBuilder(isSimple ? 32 : 36);
// time_low
builder.append(digits(mostSigBits >> 32, 8));
if (!isSimple)
{
builder.append('-');
}
// time_mid
builder.append(digits(mostSigBits >> 16, 4));
if (!isSimple)
{
builder.append('-');
}
// time_high_and_version
builder.append(digits(mostSigBits, 4));
if (!isSimple)
{
builder.append('-');
}
// variant_and_sequence
builder.append(digits(leastSigBits >> 48, 4));
if (!isSimple)
{
builder.append('-');
}
// node
builder.append(digits(leastSigBits, 12));
return builder.toString();
}
/**
* 返回此 UUID 的哈希码
*
* @return UUID 的哈希码值
*/
@Override
public int hashCode()
{
long hilo = mostSigBits ^ leastSigBits;
return ((int) (hilo >> 32)) ^ (int) hilo;
}
/**
* 将此对象与指定对象比较
* <p>
* 当且仅当参数不为 {@code null}而是一个 UUID 对象具有与此 UUID 相同的 varriant包含相同的值每一位均相同结果才为 {@code true}
*
* @param obj 要与之比较的对象
*
* @return 如果对象相同则返回 {@code true}否则返回 {@code false}
*/
@Override
public boolean equals(Object obj)
{
if ((null == obj) || (obj.getClass() != UUID.class))
{
return false;
}
UUID id = (UUID) obj;
return (mostSigBits == id.mostSigBits && leastSigBits == id.leastSigBits);
}
// Comparison Operations
/**
* 将此 UUID 与指定的 UUID 比较
*
* <p>
* 如果两个 UUID 不同且第一个 UUID 的最高有效字段大于第二个 UUID 的对应字段则第一个 UUID 大于第二个 UUID
*
* @param val 与此 UUID 比较的 UUID
*
* @return 在此 UUID 小于等于或大于 val 分别返回 -10 1
*
*/
@Override
public int compareTo(UUID val)
{
// The ordering is intentionally set up so that the UUIDs
// can simply be numerically compared as two numbers
return (this.mostSigBits < val.mostSigBits ? -1 : //
(this.mostSigBits > val.mostSigBits ? 1 : //
(this.leastSigBits < val.leastSigBits ? -1 : //
(this.leastSigBits > val.leastSigBits ? 1 : //
0))));
}
// -------------------------------------------------------------------------------------------------------------------
// Private method start
/**
* 返回指定数字对应的hex值
*
* @param val
* @param digits
* @return
*/
private static String digits(long val, int digits)
{
long hi = 1L << (digits * 4);
return Long.toHexString(hi | (val & (hi - 1))).substring(1);
}
/**
* 检查是否为time-based版本UUID
*/
private void checkTimeBase()
{
if (version() != 1)
{
throw new UnsupportedOperationException("Not a time-based UUID");
}
}
/**
* 获取{@link SecureRandom}类提供加密的强随机数生成器 (RNG)
*
* @return {@link SecureRandom}
*/
public static SecureRandom getSecureRandom() throws Exception {
try
{
return SecureRandom.getInstance("SHA1PRNG");
}
catch (NoSuchAlgorithmException e)
{
throw new Exception(e);
}
}
/**
* 获取随机数生成器对象<br>
* ThreadLocalRandom是JDK 7之后提供并发产生随机数能够解决多个线程发生的竞争争夺
*
* @return {@link ThreadLocalRandom}
*/
public static ThreadLocalRandom getRandom()
{
return ThreadLocalRandom.current();
}
}

View File

@ -1,56 +0,0 @@
package cn.qihangerp.sys.utils.http;
import jakarta.servlet.ServletRequest;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
* 通用http工具封装
*
* @author qihang
*/
public class HttpHelper
{
private static final Logger LOGGER = LoggerFactory.getLogger(HttpHelper.class);
public static String getBodyString(ServletRequest request)
{
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try (InputStream inputStream = request.getInputStream())
{
reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String line = "";
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
}
catch (IOException e)
{
LOGGER.warn("getBodyString出现问题");
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e)
{
LOGGER.error(ExceptionUtils.getMessage(e));
}
}
}
return sb.toString();
}
}

View File

@ -1,54 +0,0 @@
//package com.qihang.oms.api.utils.ip;
//
//import com.alibaba.fastjson2.JSON;
//import com.alibaba.fastjson2.JSONObject;
//import com.qihang.oms.api.constant.Constants;
//import com.qihang.oms.api.utils.http.HttpUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.util.StringUtils;
//
///**
// * 获取地址类
// *
// * @author qihang
// */
//public class AddressUtils
//{
// private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
//
// // IP地址查询
// public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
//
// // 未知地址
// public static final String UNKNOWN = "XX XX";
//
// public static String getRealAddressByIP(String ip)
// {
// // 内网不查询
// if (IpUtils.internalIp(ip))
// {
// return "内网IP";
// }
//
// try
// {
// String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
// if (StringUtils.isEmpty(rspStr))
// {
// log.error("获取地理位置异常 {}", ip);
// return UNKNOWN;
// }
// JSONObject obj = JSON.parseObject(rspStr);
// String region = obj.getString("pro");
// String city = obj.getString("city");
// return String.format("%s %s", region, city);
// }
// catch (Exception e)
// {
// log.error("获取地理位置异常 {}", ip);
// }
//
// return UNKNOWN;
// }
//}

View File

@ -1,384 +0,0 @@
//package com.qihang.oms.api.utils.ip;
//
//
//import com.qihang.oms.api.utils.ServletUtils;
//import com.qihang.oms.api.utils.StringUtils;
//import jakarta.servlet.http.HttpServletRequest;
//
//import java.net.InetAddress;
//import java.net.UnknownHostException;
//
///**
// * 获取IP方法
// *
// * @author qihang
// */
//public class IpUtils
//{
// public final static String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
// // 匹配 ip
// public final static String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")";
// public final static String REGX_IP_WILDCARD = "(((\\*\\.){3}\\*)|(" + REGX_0_255 + "(\\.\\*){3})|(" + REGX_0_255 + "\\." + REGX_0_255 + ")(\\.\\*){2}" + "|((" + REGX_0_255 + "\\.){3}\\*))";
// // 匹配网段
// public final static String REGX_IP_SEG = "(" + REGX_IP + "\\-" + REGX_IP + ")";
//
// /**
// * 获取客户端IP
// *
// * @return IP地址
// */
// public static String getIpAddr()
// {
// return getIpAddr(ServletUtils.getRequest());
// }
//
// /**
// * 获取客户端IP
// *
// * @param request 请求对象
// * @return IP地址
// */
// public static String getIpAddr(HttpServletRequest request)
// {
// if (request == null)
// {
// return "unknown";
// }
// String ip = request.getHeader("x-forwarded-for");
// 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("X-Forwarded-For");
// }
// 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("X-Real-IP");
// }
//
// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
// {
// ip = request.getRemoteAddr();
// }
//
// return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
// }
//
// /**
// * 检查是否为内部IP地址
// *
// * @param ip IP地址
// * @return 结果
// */
// public static boolean internalIp(String ip)
// {
// byte[] addr = textToNumericFormatV4(ip);
// return internalIp(addr) || "127.0.0.1".equals(ip);
// }
//
// /**
// * 检查是否为内部IP地址
// *
// * @param addr byte地址
// * @return 结果
// */
// private static boolean internalIp(byte[] addr)
// {
// if (StringUtils.isNull(addr) || addr.length < 2)
// {
// return true;
// }
// final byte b0 = addr[0];
// final byte b1 = addr[1];
// // 10.x.x.x/8
// final byte SECTION_1 = 0x0A;
// // 172.16.x.x/12
// final byte SECTION_2 = (byte) 0xAC;
// final byte SECTION_3 = (byte) 0x10;
// final byte SECTION_4 = (byte) 0x1F;
// // 192.168.x.x/16
// final byte SECTION_5 = (byte) 0xC0;
// final byte SECTION_6 = (byte) 0xA8;
// switch (b0)
// {
// case SECTION_1:
// return true;
// case SECTION_2:
// if (b1 >= SECTION_3 && b1 <= SECTION_4)
// {
// return true;
// }
// case SECTION_5:
// switch (b1)
// {
// case SECTION_6:
// return true;
// }
// default:
// return false;
// }
// }
//
// /**
// * 将IPv4地址转换成字节
// *
// * @param text IPv4地址
// * @return byte 字节
// */
// public static byte[] textToNumericFormatV4(String text)
// {
// if (text.length() == 0)
// {
// return null;
// }
//
// byte[] bytes = new byte[4];
// String[] elements = text.split("\\.", -1);
// try
// {
// long l;
// int i;
// switch (elements.length)
// {
// case 1:
// l = Long.parseLong(elements[0]);
// if ((l < 0L) || (l > 4294967295L))
// {
// return null;
// }
// bytes[0] = (byte) (int) (l >> 24 & 0xFF);
// bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
// bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
// bytes[3] = (byte) (int) (l & 0xFF);
// break;
// case 2:
// l = Integer.parseInt(elements[0]);
// if ((l < 0L) || (l > 255L))
// {
// return null;
// }
// bytes[0] = (byte) (int) (l & 0xFF);
// l = Integer.parseInt(elements[1]);
// if ((l < 0L) || (l > 16777215L))
// {
// return null;
// }
// bytes[1] = (byte) (int) (l >> 16 & 0xFF);
// bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
// bytes[3] = (byte) (int) (l & 0xFF);
// break;
// case 3:
// for (i = 0; i < 2; ++i)
// {
// l = Integer.parseInt(elements[i]);
// if ((l < 0L) || (l > 255L))
// {
// return null;
// }
// bytes[i] = (byte) (int) (l & 0xFF);
// }
// l = Integer.parseInt(elements[2]);
// if ((l < 0L) || (l > 65535L))
// {
// return null;
// }
// bytes[2] = (byte) (int) (l >> 8 & 0xFF);
// bytes[3] = (byte) (int) (l & 0xFF);
// break;
// case 4:
// for (i = 0; i < 4; ++i)
// {
// l = Integer.parseInt(elements[i]);
// if ((l < 0L) || (l > 255L))
// {
// return null;
// }
// bytes[i] = (byte) (int) (l & 0xFF);
// }
// break;
// default:
// return null;
// }
// }
// catch (NumberFormatException e)
// {
// return null;
// }
// return bytes;
// }
//
// /**
// * 获取IP地址
// *
// * @return 本地IP地址
// */
// public static String getHostIp()
// {
// try
// {
// return InetAddress.getLocalHost().getHostAddress();
// }
// catch (UnknownHostException e)
// {
// }
// return "127.0.0.1";
// }
//
// /**
// * 获取主机名
// *
// * @return 本地主机名
// */
// public static String getHostName()
// {
// try
// {
// return InetAddress.getLocalHost().getHostName();
// }
// catch (UnknownHostException e)
// {
// }
// return "未知";
// }
//
// /**
// * 从多级反向代理中获得第一个非unknown IP地址
// *
// * @param ip 获得的IP地址
// * @return 第一个非unknown IP地址
// */
// public static String getMultistageReverseProxyIp(String ip)
// {
// // 多级反向代理检测
// if (ip != null && ip.indexOf(",") > 0)
// {
// final String[] ips = ip.trim().split(",");
// for (String subIp : ips)
// {
// if (false == isUnknown(subIp))
// {
// ip = subIp;
// break;
// }
// }
// }
// return StringUtils.substring(ip, 0, 255);
// }
//
// /**
// * 检测给定字符串是否为未知多用于检测HTTP请求相关
// *
// * @param checkString 被检测的字符串
// * @return 是否未知
// */
// public static boolean isUnknown(String checkString)
// {
// return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
// }
//
// /**
// * 是否为IP
// */
// public static boolean isIP(String ip)
// {
// return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP);
// }
//
// /**
// * 是否为IP *为间隔的通配符地址
// */
// public static boolean isIpWildCard(String ip)
// {
// return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP_WILDCARD);
// }
//
// /**
// * 检测参数是否在ip通配符里
// */
// public static boolean ipIsInWildCardNoCheck(String ipWildCard, String ip)
// {
// String[] s1 = ipWildCard.split("\\.");
// String[] s2 = ip.split("\\.");
// boolean isMatchedSeg = true;
// for (int i = 0; i < s1.length && !s1[i].equals("*"); i++)
// {
// if (!s1[i].equals(s2[i]))
// {
// isMatchedSeg = false;
// break;
// }
// }
// return isMatchedSeg;
// }
//
// /**
// * 是否为特定格式如:10.10.10.1-10.10.10.99的ip段字符串
// */
// public static boolean isIPSegment(String ipSeg)
// {
// return StringUtils.isNotBlank(ipSeg) && ipSeg.matches(REGX_IP_SEG);
// }
//
// /**
// * 判断ip是否在指定网段中
// */
// public static boolean ipIsInNetNoCheck(String iparea, String ip)
// {
// int idx = iparea.indexOf('-');
// String[] sips = iparea.substring(0, idx).split("\\.");
// String[] sipe = iparea.substring(idx + 1).split("\\.");
// String[] sipt = ip.split("\\.");
// long ips = 0L, ipe = 0L, ipt = 0L;
// for (int i = 0; i < 4; ++i)
// {
// ips = ips << 8 | Integer.parseInt(sips[i]);
// ipe = ipe << 8 | Integer.parseInt(sipe[i]);
// ipt = ipt << 8 | Integer.parseInt(sipt[i]);
// }
// if (ips > ipe)
// {
// long t = ips;
// ips = ipe;
// ipe = t;
// }
// return ips <= ipt && ipt <= ipe;
// }
//
// /**
// * 校验ip是否符合过滤串规则
// *
// * @param filter 过滤IP列表,支持后缀'*'通配,支持网段如:`10.10.10.1-10.10.10.99`
// * @param ip 校验IP地址
// * @return boolean 结果
// */
// public static boolean isMatchedIp(String filter, String ip)
// {
// if (StringUtils.isEmpty(filter) || StringUtils.isEmpty(ip))
// {
// return false;
// }
// String[] ips = filter.split(";");
// for (String iStr : ips)
// {
// if (isIP(iStr) && iStr.equals(ip))
// {
// return true;
// }
// else if (isIpWildCard(iStr) && ipIsInWildCardNoCheck(iStr, ip))
// {
// return true;
// }
// else if (isIPSegment(iStr) && ipIsInNetNoCheck(iStr, ip))
// {
// return true;
// }
// }
// return false;
// }
//}