目录
一、思路
诱导对方点击链接从而获取对应的ip从而解析(4G,5G解析可能稍微有些偏差,无线网解析准确率高达90%)。当然也有不需要自己写代码的方式,后续我会把方法分享出来。

二、准备工具
服务器:阿里云服务器 win10 (这是我的服务器操作系统,就有点业务,服务器能装java环境就OK了)
代码编写:IDEA
测试工具:Postman (当然,浏览器也可以直接访问,这个就看你controller怎么写了)
工具类:ip2region.xdb(用于解析对方ip的所在地)
三、代码编写
2.1依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>2.7.0</version>
</dependency>
2.2ip解析类
import org.apache.commons.lang3.StringUtils;
import org.lionsoul.ip2region.xdb.Searcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.FileCopyUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
public class IpUtils {
private static final Logger logger = LoggerFactory.getLogger(IpUtils.class);
private final static String localIp = "127.0.0.1";
private static Searcher searcher = null;
@Autowired
private static ResourceLoader resourceLoader;
public static Resource resource;
static {
try {
ClassLoader classLoader = IpUtils.class.getClassLoader();
System.out.println(classLoader);
URL url = classLoader.getResource("p2region.xdb");
if (url != null) {
System.out.println(url);
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
connection.connect();
} else {
System.err.println("Could not find directory in classpath");
}
ClassPathResource classPathResource = new ClassPathResource("/ip2region/ip2region.xdb");
try {
InputStream inputStream = classPathResource.getInputStream();
System.out.println(inputStream);
byte[] dbBinStr = FileCopyUtils.copyToByteArray(inputStream);
searcher = Searcher.newWithBuffer(dbBinStr);
logger.debug("缓存成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
InputStream inputStream2 = IpUtils.class.getClassLoader().getResourceAsStream("classpath:caradd.xml");
} catch (IOException e) {
logger.error("解析ip地址失败", e);
throw new RuntimeException(e);
}
}
public static String getCityInfo(String ipAddress) {
String cityInfo = null;
try {
return searcher.search(ipAddress);
} catch (Exception e) {
logger.error(e);
}
return null;
}
}
2.3ip获取类
package com.hhkj.yunxingkong;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
@Slf4j
public class IPUtil {
private static final String UNKNOWN = "unknown";
public IPUtil() {
}
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.4controller编写
@GetMapping("ip")
public String test3(HttpServletRequest request) {
//获取IP地址
IPUtil ipUtil = new IPUtil();
String ipAddress = ipUtil.getIpAddr(request);
ClassPathResource classPathResource = new ClassPathResource("/ip2region/ip2region.xdb");
try {
InputStream inputStream = classPathResource.getInputStream();
System.out.println(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(IpUtils.getCityInfo(ipAddress));
return IpUtils.getCityInfo(ipAddress);
}
三、运行效果
3.1本机运行效果
3.2服务器运行效果
四、总结
综上,也是成功的能获取点击链接者的ip进行解析。后端我也没系统学过,就简简单单做了这个,若有什么bug(勿喷),欢迎大佬指教。