简介
UserAgentUtils是Java Web项目中一个非常常用的工具库,用于解析用户浏览器的User-Agent字符串,以获取所访问的设备类型、操作系统类型和版本、浏览器类型和版本等信息。
依赖
<dependency> <groupId>eu.bitwalker</groupId> <artifactId>UserAgentUtils</artifactId> <version>1.21</version> </dependency>
代码
import eu.bitwalker.useragentutils.Browser; import eu.bitwalker.useragentutils.OperatingSystem; import eu.bitwalker.useragentutils.UserAgent; import eu.bitwalker.useragentutils.Version; import javax.servlet.http.HttpServletRequest; import java.net.InetAddress; import java.util.Objects; /** * @ClassName UserAgentUtils * @Description 用户代理工具类 * @Author XinHai.Ma * @Date 2023/6/9 22:45 * @Version 1.0 */ public class UserAgentUtils { /** * 获取客户端IP地址 * * @param request * @return */ public static String getIpAddress(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknow".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(); if (ip.equals("127.0.0.1")) { //根据网卡取本机配置的IP InetAddress inet = null; try { inet = InetAddress.getLocalHost(); } catch (Exception e) { e.printStackTrace(); } ip = inet.getHostAddress(); } } // 多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if (ip != null && ip.length() > 15) { if (ip.indexOf(",") > 0) { ip = ip.substring(0, ip.indexOf(",")); } } return ip; } /** * 获取设备类型 * * @param request * @return */ public static String getDeviceType(HttpServletRequest request) { String agent = request.getHeader("User-Agent"); UserAgent userAgent = UserAgent.parseUserAgentString(agent); OperatingSystem operatingSystem = userAgent.getOperatingSystem(); // 操作系统信息 eu.bitwalker.useragentutils.DeviceType deviceType = operatingSystem.getDeviceType(); // 设备类型 switch (deviceType) { case COMPUTER: return "PC"; case TABLET: { if (agent.contains("Android")) return "Android Pad"; if (agent.contains("iOS")) return "iPad"; return "Unknown"; } case MOBILE: { if (agent.contains("Android")) return "Android"; if (agent.contains("iOS")) return "IOS"; return "Unknown"; } default: return "Unknown"; } } /** * 获取来访者的浏览器版本 * * @param request * @return */ public static String getBrowserInfo(HttpServletRequest request) { String browserVersion = null; String header = request.getHeader("User-Agent"); if (header == null || header.equals("")) { return ""; } if (header.indexOf("MSIE") > 0) { browserVersion = "IE"; } else if (header.indexOf("Firefox") > 0) { browserVersion = "Firefox"; } else if (header.indexOf("Chrome") > 0) { browserVersion = "Chrome"; } else if (header.indexOf("Safari") > 0) { browserVersion = "Safari"; } else if (header.indexOf("Camino") > 0) { browserVersion = "Camino"; } else if (header.indexOf("Konqueror") > 0) { browserVersion = "Konqueror"; } return browserVersion; } /** * 获取发起请求的浏览器名称 * * @param request * @return */ public static String getBrowserName(HttpServletRequest request) { String header = request.getHeader("User-Agent"); UserAgent userAgent = UserAgent.parseUserAgentString(header); Browser browser = userAgent.getBrowser(); return Objects.isNull(browser) ? "Unknown" : browser.getName(); } /** * 获取发起请求的浏览器版本号 * * @param request * @return */ public static String getBrowserVersion(HttpServletRequest request) { String header = request.getHeader("User-Agent"); UserAgent userAgent = UserAgent.parseUserAgentString(header); //获取浏览器信息 Browser browser = userAgent.getBrowser(); //获取浏览器版本号 Version version = browser.getVersion(header); return Objects.isNull(version) ? "Unknown" : version.getVersion(); } /** * 获取发起请求的操作系统名称 * * @param request * @return */ public static String getOsName(HttpServletRequest request) { String header = request.getHeader("User-Agent"); UserAgent userAgent = UserAgent.parseUserAgentString(header); OperatingSystem operatingSystem = userAgent.getOperatingSystem(); return Objects.isNull(operatingSystem) ? "Unknown" : operatingSystem.getName(); } }
实现效果
标签:return,String,ip,request,代理,用户,header,使用,UserAgent From: https://www.cnblogs.com/mxh-java/p/17470497.html