当前位置:   article > 正文

Spring Boot 快速实现 IP 地址解析!_springboot获取ip地址

springboot获取ip地址

springmeng 2023-08-09 10:20 发表于山东

 

大家好,我是小孟!平时我开发了大量的项目,并将优质的项目录制供小伙伴学习,一次上车永久学习。全网性价比最高。VIP大特惠(仅限100名额)!...

来源:juejin.cn/post/7130544273421762573

本篇带大家实践在spring boot 项目中获取请求的ip与详细地址,我们的很多网站app 中都已经新增了ip 地址显示,大家也可以用在自己的开发中,显得更高级。

引入

如果使用本地ip 解析的话,我们将会借助ip2region,该项目维护了一份较为详细的本地ip 地址对应表,如果为了离线环境的使用,需要导入该项目依赖,并指定版本,不同版本的方法可能存在差异。

  1. <!-- ip库-->
  2. <dependency>
  3.  <groupId>org.lionsoul</groupId>
  4.  <artifactId>ip2region</artifactId>
  5.  <version>2.6.3</version>
  6. </dependency>

官方gitee:

https://gitee.com/lionsoul/ip2region.git

开发

在使用时需要将 xdb 文件下载到工程文件目录下,使用ip2region即使是完全基于 xdb 文件的查询,单次查询响应时间在十微秒级别,可通过如下两种方式开启内存加速查询:

  • vIndex 索引缓存: 使用固定的 512KiB 的内存空间缓存 vector index 数据,减少一次 IO 磁盘操作,保持平均查询效率稳定在10-20微秒之间。

  • xdb 整个文件缓存: 将整个 xdb 文件全部加载到内存,内存占用等同于 xdb 文件大小,无磁盘 IO 操作,保持微秒级别的查询效率。

  1. /**
  2.  * ip查询
  3.  */
  4. @Slf4j
  5. public class IPUtil {
  6.     private static final String UNKNOWN = "unknown";
  7.     protected IPUtil(){ }
  8.     /**
  9.      * 获取 IP地址
  10.      * 使用 Nginx等反向代理软件, 则不能通过 request.getRemoteAddr()获取 IP地址
  11.      * 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,
  12.      * X-Forwarded-For中第一个非 unknown的有效IP字符串,则为真实IP地址
  13.      */
  14.     public static String getIpAddr(HttpServletRequest request) {
  15.         String ip = request.getHeader("x-forwarded-for");
  16.         if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
  17.             ip = request.getHeader("Proxy-Client-IP");
  18.         }
  19.         if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
  20.             ip = request.getHeader("WL-Proxy-Client-IP");
  21.         }
  22.         if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
  23.             ip = request.getRemoteAddr();
  24.         }
  25.         return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
  26.     }
  27.     public static  String getAddr(String ip){
  28.         String dbPath = "src/main/resources/ip2region/ip2region.xdb";
  29.         // 1、从 dbPath 加载整个 xdb 到内存。
  30.         byte[] cBuff;
  31.         try {
  32.             cBuff = Searcher.loadContentFromFile(dbPath);
  33.         } catch (Exception e) {
  34.             log.info("failed to load content from `%s`: %s\n", dbPath, e);
  35.             return null;
  36.         }
  37.         // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
  38.         Searcher searcher;
  39.         try {
  40.             searcher = Searcher.newWithBuffer(cBuff);
  41.         } catch (Exception e) {
  42.            log.info("failed to create content cached searcher: %s\n", e);
  43.             return null;
  44.         }
  45.         // 3、查询
  46.         try {
  47.             String region = searcher.searchByStr(ip);
  48.             return region;
  49.         } catch (Exception e) {
  50.             log.info("failed to search(%s): %s\n", ip, e);
  51.         }
  52.         return null;
  53.     }

这里我们将ip 解析封装成一个工具类,包含获取IP和ip 地址解析两个方法,ip 的解析可以在请求中获取。获取到ip后,需要根据ip ,在xdb 中查找对应的IP地址的解析,由于是本地数据库可能存在一定的缺失,部分ip 存在无法解析的情况。

在线解析

如果想要获取更加全面的ip 地址信息,可使用在线数据库,这里提供的是 whois.pconline.com 的IP解析,该IP解析在我的使用过程中表现非常流畅,而且只有少数的ip 存在无法解析的情况。

  1. @Slf4j
  2. public class AddressUtils {
  3.     // IP地址查询
  4.     public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
  5.     // 未知地址
  6.     public static final String UNKNOWN = "XX XX";
  7.     public static String getRealAddressByIP(String ip) {
  8.         String address = UNKNOWN;
  9.         // 内网不查询
  10.         if (IpUtils.internalIp(ip)) {
  11.             return "内网IP";
  12.         }
  13.         if (true) {
  14.             try {
  15.                 String rspStr = sendGet(IP_URL, "ip=" + ip + "&json=true" ,"GBK");
  16.                 if (StrUtil.isEmpty(rspStr)) {
  17.                     log.error("获取地理位置异常 {}" , ip);
  18.                     return UNKNOWN;
  19.                 }
  20.                 JSONObject obj = JSONObject.parseObject(rspStr);
  21.                 String region = obj.getString("pro");
  22.                 String city = obj.getString("city");
  23.                 return String.format("%s %s" , region, city);
  24.             } catch (Exception e) {
  25.                 log.error("获取地理位置异常 {}" , ip);
  26.             }
  27.         }
  28.         return address;
  29.     }
  30.     public static String sendGet(String url, String param, String contentType) {
  31.         StringBuilder result = new StringBuilder();
  32.         BufferedReader in = null;
  33.         try {
  34.             String urlNameString = url + "?" + param;
  35.             log.info("sendGet - {}" , urlNameString);
  36.             URL realUrl = new URL(urlNameString);
  37.             URLConnection connection = realUrl.openConnection();
  38.             connection.setRequestProperty("accept" , "*/*");
  39.             connection.setRequestProperty("connection" , "Keep-Alive");
  40.             connection.setRequestProperty("user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  41.             connection.connect();
  42.             in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
  43.             String line;
  44.             while ((line = in.readLine()) != null) {
  45.                 result.append(line);
  46.             }
  47.             log.info("recv - {}" , result);
  48.         } catch (ConnectException e) {
  49.             log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
  50.         } catch (SocketTimeoutException e) {
  51.             log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
  52.         } catch (IOException e) {
  53.             log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
  54.         } catch (Exception e) {
  55.             log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
  56.         } finally {
  57.             try {
  58.                 if (in != null) {
  59.                     in.close();
  60.                 }
  61.             } catch (Exception ex) {
  62.                 log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
  63.             }
  64.         }
  65.         return result.toString();
  66.     }
  67. }

场景

那么在开发的什么流程获取ip 地址是比较合适的,这里就要用到我们的拦截器了。拦截进入服务的每个请求,进行前置操作,在进入时就完成请求头的解析,ip 获取以及ip 地址解析,这样在后续流程的全环节,都可以复用ip 地址等信息

  1. /**
  2.  * 对ip 进行限制,防止IP大量请求
  3.  */
  4. @Slf4j
  5. @Configuration
  6. public class IpUrlLimitInterceptor implements HandlerInterceptor{
  7.     @Override
  8.     public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) {
  9.         //更新全局变量
  10.         Constant.IP = IPUtil.getIpAddr(httpServletRequest);
  11.         Constant.IP_ADDR = AddressUtils.getRealAddressByIP(Constant.IP);
  12.         Constant.URL = httpServletRequest.getRequestURI();
  13.         return true;
  14.     }
  15.     @Override
  16.     public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) {
  17.         //通过本地获取
  18. //        获得ip
  19. //        String ip = IPUtil.getIpAddr(httpServletRequest);
  20.         //解析具体地址
  21. //        String addr = IPUtil.getAddr(ip);
  22.         //通过在线库获取
  23. //        String ip = IpUtils.getIpAddr(httpServletRequest);
  24. //        String ipaddr = AddressUtils.getRealAddressByIP(ipAddr);
  25. //        log.info("IP >> {},Address >> {}",ip,ipaddr);
  26.     }
  27.     @Override
  28.     public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
  29.     }
  30. }

如果想要执行我们的ip 解析拦截器,需要在spring boot的视图层进行拦截才会触发我们的拦截器。

  1. @Configuration
  2. public class WebConfig implements WebMvcConfigurer {
  3.     @Autowired
  4.     IpUrlLimitInterceptor ipUrlLimitInterceptor;
  5.  
  6.      //执行ip拦截器
  7.     @Override
  8.     public void addInterceptors(InterceptorRegistry registry){
  9.         registry.addInterceptor(ipUrlLimitInterceptor)
  10.                 // 拦截所有请求
  11.                 .addPathPatterns("/**");
  12.     }
  13. }

通过这样的一套流程下来,我们就能实现对每一个请求进行ip 获取、ip解析,为每个请求带上具体ip地址的小尾巴。

优质文章阅读:

1,准备三个月,终拿快手offer!薪资很香!

2,圆梦!终于进阿里了!

图片

·················END·················

你好,我是小孟,10年开发老司机,小作坊boss、做过码农、主管、产品经理。喜欢自由,讨厌职场的勾心斗角。我的偶像是乔布斯,10年前选择计算机这个行业,就是因为热爱。现在已而立之年,虽然没有当初追寻技术的单纯,但依然热爱。技术改变世界,知识改变命运是我不变的信念。学习、思考、因时代变化而变化是我的武器。如果我觉得一件事,值得干,至少我会坚持5年。在我毕业一年后,我就搞定了房贷、车贷等一切贷款,所以我可以自由。我不喜欢循规蹈矩和被安排的生活,大学时候就喜欢折腾,现在也是。我大多数时候很孤独,也喜欢孤独,不怎么合群,在创业中苟延残喘的活下来。未来怎么样,我不知道,但是我依然会追寻有激情的生活和事情,我相信坚持的力量,如果能把一件事坚持下去,真的可以打败99%的人。关注我,一起聊技术、职场、人生和好玩的事。

暂时开放微信大号,好友位不多,需要的小伙伴们可以加,朋友圈和交流群里会发一些学习资料、个人见解、课程等等。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/405733
推荐阅读
相关标签
  

闽ICP备14008679号