广告:
信号屏蔽器:守护你的数字生活,为你带来宁静与专注
1.首先导入依赖
<dependencies>
<!-- Alibaba FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<!-- Bitwalker UserAgent Utils -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>user-agent-utils</artifactId>
<version>1.22</version>
</dependency>
<!-- Apache Commons Lang -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<!-- Lionsoul ip2region -->
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.3</version>
</dependency>
</dependencies>
2.需要准备一个ip地址查询库'ip2region.xdb',用于获取ip对应大致地理位置信息。并获取这个文件绝对位置,用于赋值给静态构造函数的filePath。这里库很难获取,需要库的请进入sz-sstx.com留言,免费发送
3.下面直接展示代码
package com.sstx.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.util.FileCopyUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author zp
* @date 2023/4/11 19:25
*/
@Slf4j
public class IpUtils {
private final static String localIp = "127.0.0.1";
private static Searcher searcher = null;
/**
* 在服务启动时加载 ip2region.db 到内存中
* 解决打包jar后找不到 ip2region.db 的问题
*/
static {
try {
String filePath = "/myusage/java/jar/ip2region.xdb";
InputStream ris = new FileInputStream(filePath);
if(ris==null){
log.error("/ip2region/ip2region.xdb文件没找到");
}else {
byte[] dbBinStr = FileCopyUtils.copyToByteArray(ris);
searcher = Searcher.newWithBuffer(dbBinStr);
//注意:不能使用文件类型,打成jar包后,会找不到文件
log.info("缓存成功!!!!");
}
} catch (IOException e) {
log.error("解析ip地址失败,无法创建搜索器: {}", e);
throw new RuntimeException(e);
}
}
/**
* 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
* 参考文章: http://developer.51cto.com/art/201111/305181.htm
*
* 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?
* 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
*
* 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130,
* 192.168.1.100
*
* 用户真实IP为: 192.168.1.110
*
* @param request
* @return
*/
public static String getIp(HttpServletRequest request) {
String ipAddress;
try {
// 以下两个获取在k8s中,将真实的客户端IP,放到了x-Original-Forwarded-For。而将WAF的回源地址放到了 x-Forwarded-For了。
ipAddress = request.getHeader("X-Original-Forwarded-For");
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("X-Forwarded-For");
}
//获取nginx等代理的ip
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("x-forwarded-for");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
// 2.如果没有转发的ip,则取当前通信的请求端的ip(兼容k8s集群获取ip)
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
// 如果是127.0.0.1,则取本地真实ip
if (localIp.equals(ipAddress)) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
ipAddress = inet.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) {
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
log.error("解析请求IP失败", e);
ipAddress = "";
}
return "0:0:0:0:0:0:0:1".equals(ipAddress) ? localIp : ipAddress;
}
/**
* 获取访问设备
* @param request
* @return
*/
public static UserAgent getUserAgent(HttpServletRequest request) {
return UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
}
/**
* 根据ip获取 城市信息
* @param ipAddress
* @return
*/
public static String getCityInfo(String ipAddress) {
String cityInfo = null;
try {
return searcher.search(ipAddress);
} catch (Exception e) {
log.error("搜索:{} 失败: {}",ipAddress, e);
}
return null;
}
/**
* 根据ip2region解析ip地址
* @param ip ip地址
* @return 解析后的ip地址信息
*/
public static String getIp2region(String ip) {
if(searcher == null){
log.error("Error: DbSearcher is null");
return null;
}
try {
String ipInfo = searcher.search(ip);
if (!StringUtils.isEmpty(ipInfo)) {
ipInfo = ipInfo.replace("|0", "");
ipInfo = ipInfo.replace("0|", "");
}
return ipInfo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取IP地址
*
* @return 本地IP地址
*/
public static String getHostIp(){
try{
return InetAddress.getLocalHost().getHostAddress();
}catch (UnknownHostException e){
}
return localIp;
}
/**
* 获取主机名
*
* @return 本地主机名
*/
public static String getHostName(){
try{
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
}
return "未知";
}
public static void main(String[] args) {
String ip = "119.122.113.223";
String cityInfo = IpUtils.getIp2region(ip);
// System.out.println(getCityInfo(ip));
// System.out.println(cityInfo);
// System.out.println(getHostIp());
// System.out.println(getHostName());
String url = "https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+ ip +"&resource_id=5809";
String body = HttpUtil.get(url);
JSONObject jsonObject = JSON.parseObject(body);
JSONArray data1 = jsonObject.getJSONArray("data");
JSONObject o = (JSONObject)data1.get(0);
System.out.println(o.getString("location"));
}
}
4.其中,getIp方法为获取请求源的ip地址,getUserAgent为获取请求客户端的设备型号信息,getIp2region为获取ip的地理位置信息