首先需要写一个IP获取的工具类,因为每一次用户的Request请 求,都会携带上请求的IP地 址放到请求头中。 public class IpUtil {public static String getIpAddr(ServerHttpRequest request) {HttpHeaders headers = request.getHeaders();String ipAddress = headers.getFirst("X-Forwarded-For");if (ipAddress == ...
method = searcher.getClass().getMethod("binarySearch", String.class); break; case DbSearcher.MEMORY_ALGORITYM: method = searcher.getClass().getMethod("memorySearch", String.class); break; default: } DataBlock dataBlock = null; if (Util.isIpAddress(ip) == false) { System.out.println("...
首先需要写一个IP获取的工具类,因为每一次用户的Request请 求,都会携带上请求的IP地 址放到请求头中。 publicclassIpUtil{publicstaticStringgetIpAddr(ServerHttpRequest request){ HttpHeaders headers = request.getHeaders(); String ipAddress = headers.getFirst("X-Forwarded-For");if(ipAddress ==null|| ip...
(ipAddress==null||ipAddress.isEmpty()||"unknown".equalsIgnoreCase(ipAddress)){ipAddress=request.getRemoteAddr();}returnipAddress;}publicstaticvoidcloseSearcher(){try{Searcher searcher=searcherThreadLocal.get();if(Objects.nonNull(searcher)){searcher.close();searcherThreadLocal.remove();}}catch(...
privateSearchersearcher;@OverridepublicStringgetIpAddress(Stringip){if("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {return"局域网 ip"; }if(searcher ==null) {try{Filefile =ResourceUtils.getFile("classpath:ipdb/ip2region.xdb");StringdbPath = file.getPath(); ...
importorg.lionsoul.ip2region.xdb.Searcher;importjava.io.*;importjava.util.concurrent.TimeUnit;publicclassSearcherTest{publicstaticvoidmain(String[]args){// 1、创建 searcher 对象String dbPath="ip2region.xdb file path";Searcher searcher=null;try{searcher=Searcher.newWithFileOnly(dbPath);}catch(IOExcep...
// 如果查找不到 IP,可以返回 127.0.0.1,可以做一定的处理,但是这里不考虑// if (ip == null) {// return "127.0.0.1";// }returnip;}/** * 获取mac地址 */publicstaticStringgetMacAddress()throwsException{// 取mac地址byte[]macAddressBytes=NetworkInterface.getByInetAddress(InetAddress.getLocalHost(...
String address = ""; try { address = searcher.search("66.249.79.193"); } catch (Exception e) { throw new RuntimeException(e); } System.out.println("=== 访问者的地址为:"+address+" === "); 1. 2. 3. 4. 5. 6. 7.
public class IpUtil { private static Searcher searcher; /** * 判断是否为合法 IP * @return */ public static boolean checkIp(String ipAddress) { String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\...
InetAddress inet = InetAddress.getLocalHost(); ipAddress = inet.getHostAddress(); } catch (UnknownHostException e) { log.error("根据网卡获取本机配置的IP异常", e); } } } // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 ...