ip地址优化
This commit is contained in:
parent
e1cfe605ea
commit
482f78aee2
@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
|
||||
public class AddressUtils {
|
||||
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
|
||||
|
||||
|
||||
// 未知地址
|
||||
public static final String UNKNOWN = "未知地址";
|
||||
|
||||
@ -24,10 +25,11 @@ public class AddressUtils {
|
||||
String rspStr = RegionUtil.getRegion(ip);
|
||||
if (StringUtils.isNotEmpty(rspStr)) {
|
||||
String[] obj = rspStr.split("\\|");
|
||||
// String region = obj[2];
|
||||
String region = obj[2];
|
||||
String city = obj[3];
|
||||
|
||||
// return String.format("%s%s", region, city);
|
||||
if("0".equals(city)){
|
||||
return String.format("%s", region);
|
||||
}
|
||||
return String.format("%s", city);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -73,7 +75,8 @@ public class AddressUtils {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
String realAddress = getRealAddress("117.136.79.113");
|
||||
String realAddress = getRealAddress("103.85.74.92");
|
||||
;
|
||||
System.out.println("realAddress = " + realAddress);
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
package com.dd.admin.common.utils;
import javax.servlet.http.HttpServletRequest;
public class IPUtils {
/**
* 获取IP地址
*
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
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("WL-Proxy-Client-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" : ip;
}
}
|
||||
package com.dd.admin.common.utils;
import javax.servlet.http.HttpServletRequest;
public class IPUtils {
/**
* 获取IP地址
*
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
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("WL-Proxy-Client-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" : ip;
}
}
|
@ -2,6 +2,7 @@ package com.dd.admin.common.utils;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.sun.javaws.Main;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -17,6 +18,8 @@ import java.util.Map;
|
||||
*/
|
||||
public class PureNetUtils {
|
||||
|
||||
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(PureNetUtils.class);
|
||||
|
||||
/**
|
||||
|
@ -28,6 +28,10 @@ public class RegionUtil {
|
||||
static DbConfig config = null;
|
||||
static DbSearcher searcher = null;
|
||||
|
||||
public static void main(String[] args) {
|
||||
getRegion("103.85.74.92");
|
||||
}
|
||||
|
||||
// 初始化IP库
|
||||
static {
|
||||
try {
|
||||
|
BIN
web/dist.rar
Normal file
BIN
web/dist.rar
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user