当前位置: 首页 > news >正文

SpringBoot结合ip2region实现博客评论显示IP属地

你好呀,我是小邹。

在现代的Web应用中,特别是博客和论坛类网站,为用户提供地理定位服务(如显示用户所在地理位置)可以极大地增强用户体验。本文将详细探讨如何使用Java和相关技术栈来实现在博客评论中显示用户的地址信息,特别关注如何利用ip2region库解析IP地址获取地理位置。

效果图:

在这里插入图片描述

技术栈:
  • 后端框架:Spring Boot
  • 数据库:MySQL
  • 前端技术:HTML, CSS, JavaScript
  • 地理编码库ip2region
  • 辅助库Apache Commons IO, Lombok
代码分析:IpParseUtil

这个类位于com.zou.blog.util包下,提供了几个关键方法用于解析IP地址并获取用户的地理位置信息。

ip2region文件:https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.xdb

package com.zou.blog.util;import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;/*** ip解析工具** @author zxf* @version v1.0.0* @date 2024/7/14 20:26*/
@Slf4j
public class IpParseUtil {/*** 解析ip地址** @param ipStr 字符串类型ip 例:192.168.0.1* @return 返回结果形式(国家 | 区域 | 省份 | 城市 | ISP) 例 [中国, 0, 河北省, 衡水市, 电信]*/public static List<String> parse(String ipStr) {return parse(ipStr, null);}/*** 自定义解析ip地址** @param ipStr ip 字符串类型ip 例:1970753539(经过转换后的)* @param index 想要获取的区间 例如:只想获取 省,市 index = [2,3]* @return 返回结果例 [北京,北京市]*/public static List<String> parse(String ipStr, int[] index) {try {//获得文件流时,因为读取的文件是在打好jar文件里面,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流//ResourcePatternResolver的实现方法,可以匹配到各种部署时的各种文件类型例如war,jar,zip等等findPathMatchingResourcesResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();Resource[] resources = resolver.getResources("ip2region.xdb");Resource resource = resources[0];InputStream is = resource.getInputStream();File target = new File("ip2region.xdb");FileUtils.copyInputStreamToFile(is, target);is.close();Searcher searcher = new Searcher(String.valueOf(target), null, null);long ip = Searcher.checkIP(ipStr);return parse(ip, index, searcher);} catch (Exception e) {log.error("ip解析为long错误,ipStr:[{}],错误信息:[{}]", ipStr, e.getMessage(), e);throw new RuntimeException("系统异常!");}}/*** 自定义解析ip地址** @param ip    ip Long类型ip* @param index 想要获取的区间 例如:只想获取 省,市 index = [2,3]* @return 返回结果例 [湖南省, 衡阳市]*/public static List<String> parse(Long ip, int[] index, Searcher searcher) {List<String> regionList = new ArrayList<>();try {String region = searcher.search(ip);log.info("获取到的城市信息:" + region);String[] split = region.split("\\|");if (index == null) {regionList = Arrays.asList(split);} else {for (int i : index) {regionList.add(split[i]);}}//关闭资源searcher.close();} catch (Exception e) {log.error("根据ip解析地址失败,ip:[{}],index[{}],报错信息:[{}]", ip, index, e.getMessage(), e);throw new RuntimeException("系统异常!");}return regionList;}/*** 获取IP方法** @author zxf*/public static String getIpAddr(HttpServletRequest request) {if (request == null) {return "unknown";}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("X-Forwarded-For");}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.getHeader("X-Real-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}return ip;}public static void main(String[] args) {String ip = "218.20.32.122";List<String> parse = parse(ip);System.out.println(parse);}}

关键方法解析:

  1. parse(String ipStr) 方法

    • 接收一个字符串类型的IP地址,如"192.168.0.1"。
    • 返回一个列表,其中包含解析得到的地理位置信息,格式为:[国家, 区域, 省份, 城市, ISP]。
  2. parse(String ipStr, int[] index) 方法

    • 提供了更灵活的参数index,允许开发者选择返回特定范围的信息,例如仅省份和城市。
    • 返回一个列表,只包含由index参数指定的地理位置信息部分。
  3. getIpAddr(HttpServletRequest request) 方法

    • 从HTTP请求中提取客户端的IP地址,考虑到可能存在的代理服务器情况。
集成到博客评论系统:

要在博客评论中显示用户的位置信息,你可以在处理评论提交的控制器中集成IpParseUtil类的方法。

  1. 获取IP地址
    在评论提交的控制器中,使用getIpAddr方法从HTTP请求中获取用户IP地址。

  2. 解析IP地址
    调用parse方法解析IP地址,获取用户的地理位置信息。

  3. 保存位置信息
    将解析得到的位置信息保存至评论模型中,如Comment实体的location字段。

// 在评论控制器中
@PostMapping("/comments")
public ResponseEntity<Comment> createComment(@RequestBody Comment comment, HttpServletRequest request) {String ip = IpParseUtil.getIpAddr(request);List<String> location = IpParseUtil.parse(ip);comment.setLocation(location);// ...保存评论到数据库...
}
显示位置信息:

在前端展示评论时,从location字段读取并显示信息。

<!-- 前端HTML示例 -->
<div class="comment"><p>By: {{ comment.user.name }}</p><p>Location: {{ comment.location.join(", ") }}</p><p>{{ comment.content }}</p>
</div>

通过上述步骤,我们成功实现了在博客评论中显示用户地址信息的功能,这不仅增加了博客的互动性,还为用户提供了一种直观的地理位置参考。使用Java和ip2region库,我们可以高效准确地解析IP地址并获取地理位置信息,从而提升用户体验和网站的吸引力。

http://www.lryc.cn/news/401726.html

相关文章:

  • 设计模式使用场景实现示例及优缺点(行为型模式——策略模式)
  • ReactRouter v6升级的步骤
  • 【JVM实战篇】内存调优:内存问题诊断+案例实战
  • 专业条码二维码扫描设备和手机二维码扫描软件的区别?
  • 基于嵌入式Linux的高性能车载娱乐系统设计与实现 —— 融合Qt、FFmpeg和CAN总线技术
  • 探索IP形象设计:快速掌握设计要点
  • 泛微Ecology8明细表对主表赋值
  • opencv—常用函数学习_“干货“_5
  • JAVA零基础学习1(CMD、JDK、环境变量、变量和键盘键入、IDEA)
  • Redis的安装配置及IDEA中使用
  • ubuntu 物理内存爆炸而不使用虚拟内存的问题
  • Python实现音频均衡和降噪
  • 【JavaScript 算法】贪心算法:局部最优解的构建
  • Azcopy Sync同步Azure文件共享
  • 单例模式 饿汉式和懒汉式的区别
  • Python中的模块和包的定义以及如何在Python中导入和使用它们
  • 设计模式使用场景实现示例及优缺点(结构型模式——组合模式)
  • 《系统架构设计师教程(第2版)》第11章-未来信息综合技术-06-云计算(Cloud Computing) 技术概述
  • 网络安全工作者如何解决网络拥堵
  • 电脑显示mfc140u.dll丢失的修复方法,总结7种有效的方法
  • ospf的MGRE实验
  • 开发指南047-前端模块版本
  • c#中的字符串方法
  • 成像光谱遥感技术中的AI革命:ChatGPT
  • 学习分布式事务遇到的小bug
  • ElasticSearch学习之路
  • (C++二叉树02) 翻转二叉树 对称二叉树 二叉树的深度
  • 高阶面试-mongodb
  • MySQL数据库慢查询日志、SQL分析、数据库诊断
  • [短笔记] Ubuntu配置环境变量的最佳实践