|
@@ -1,13 +1,16 @@
|
|
|
package com.citygis.common.boot.util;
|
|
|
|
|
|
-import com.citygis.common.boot.define.HttpHeaderNames;
|
|
|
-import com.citygis.common.boot.enums.UserAgentTypeEnum;
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.UnknownHostException;
|
|
|
+
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import java.net.InetAddress;
|
|
|
-import java.net.UnknownHostException;
|
|
|
+import com.citygis.common.boot.constant.constant;
|
|
|
+import com.citygis.common.boot.define.HttpHeaderNames;
|
|
|
+import com.citygis.common.boot.enums.UserAgentTypeEnum;
|
|
|
+
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
|
* net帮助类
|
|
@@ -27,7 +30,7 @@ public final class NetAssist {
|
|
|
} catch (UnknownHostException e) {
|
|
|
LOG.error(e.getMessage(), e);
|
|
|
}
|
|
|
- return "";
|
|
|
+ return StringUtil.EMPTY;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -41,23 +44,12 @@ public final class NetAssist {
|
|
|
return UserAgentTypeEnum.UNKNOWN;
|
|
|
}
|
|
|
userAgent = userAgent.toLowerCase();
|
|
|
- String[] mobileAgents = {"iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi", "opera mini", "ucweb",
|
|
|
- "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod", "nokia", "samsung", "palmsource", "xda", "pieplus",
|
|
|
- "meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra",
|
|
|
- "coolpad", "webos", "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", "wellcom",
|
|
|
- "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", "pantech", "gionee", "portalmmm", "jig browser",
|
|
|
- "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq",
|
|
|
- "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno",
|
|
|
- "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper",
|
|
|
- "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-",
|
|
|
- "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp",
|
|
|
- "wapr", "webc", "winw", "winw", "xda", "xda-", "googlebot-mobile"};
|
|
|
- for (String mobileAgent : mobileAgents) {
|
|
|
+ for (String mobileAgent : constant.NET_ASSIST.MOBILE_AGENTS) {
|
|
|
if (userAgent.toLowerCase().contains(mobileAgent)) {
|
|
|
return UserAgentTypeEnum.MOBILE;
|
|
|
}
|
|
|
}
|
|
|
- if (userAgent.indexOf("micromessenger") > 0) {
|
|
|
+ if (userAgent.indexOf(constant.NET_ASSIST.WECHAT_BUILT_IN_BROWSER) > 0) {
|
|
|
return UserAgentTypeEnum.WECHAT;
|
|
|
}
|
|
|
return UserAgentTypeEnum.BROWSER;
|
|
@@ -70,27 +62,27 @@ public final class NetAssist {
|
|
|
* @return 主机IP地址
|
|
|
*/
|
|
|
public static String getClientIpAddress(HttpServletRequest request) {
|
|
|
- String ip = request.getHeader("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");
|
|
|
+ String ip = request.getHeader(constant.NET_ASSIST.IP_HEADER);
|
|
|
+ if (ip == null || ip.length() == 0 || constant.NET_ASSIST.UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ if (ip == null || ip.length() == 0 || constant.NET_ASSIST.UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader(constant.NET_ASSIST.PROXY_CLIENT_IP_HEADER);
|
|
|
}
|
|
|
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
- ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ if (ip == null || ip.length() == 0 || constant.NET_ASSIST.UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader(constant.NET_ASSIST.WL_PROXY_CLIENT_IP_HEADER);
|
|
|
}
|
|
|
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
- ip = request.getHeader("HTTP_CLIENT_IP");
|
|
|
+ if (ip == null || ip.length() == 0 || constant.NET_ASSIST.UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader(constant.NET_ASSIST.HTTP_CLIENT_IP_HEADER);
|
|
|
}
|
|
|
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
- ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
|
|
+ if (ip == null || ip.length() == 0 || constant.NET_ASSIST.UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader(constant.NET_ASSIST.HTTP_X_FORWARDED_FOR_HEADER);
|
|
|
}
|
|
|
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ if (ip == null || ip.length() == 0 || constant.NET_ASSIST.UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
ip = request.getRemoteAddr();
|
|
|
}
|
|
|
} else if (ip.length() > 15) {
|
|
|
- String[] ips = ip.split(",");
|
|
|
+ String[] ips = ip.split(constant.STRING.COMMA);
|
|
|
for (String s : ips) {
|
|
|
- if (!("unknown".equalsIgnoreCase(s))) {
|
|
|
+ if (!(constant.NET_ASSIST.UNKNOWN.equalsIgnoreCase(s))) {
|
|
|
ip = s;
|
|
|
break;
|
|
|
}
|