Java获取浏览器信息、IP地址、操作系统信息

     业务需求需要记录用户登录时的信息:浏览器信息、IP地址、操作系统信息等,从网上查找了很多帖子,实现方式有两种,一种纯编码判断,一种是使用UserAgentUtils工具类。

注:经测试第一种方式获取浏览器名称时获取不到IE的信息,第二种是可以的。

IE11请求的User-Agent为:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

Chrome请求的User-Agent为:

Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36

一、第一种方式

使用工具类获取信息,添加maven依赖:



    eu.bitwalker
    UserAgentUtils
    1.21

1、获取浏览器名称及版本

public static String browserName(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        Browser browser = ua.getBrowser();
        return browser.getName() + "-" + browser.getVersion(userAgent);
    }

 2、获取操作系统名称

 public static String osName(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        OperatingSystem os = ua.getOperatingSystem();
        return os.getName();
    }

3、获取IP地址

public static String getIpAddress(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.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_FORWARDED");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_VIA");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("REMOTE_ADDR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.contains(",")) {
            return ip.split(",")[0];
        } else {
            return ip;
        }
    }

获得信息如下:Java获取浏览器信息、IP地址、操作系统信息_第1张图片

 

二、第二种方式

1、获取浏览器名称(获取不到IE的信息

public static String getBrowserName(HttpServletRequest request) {
        String browserName = "";
        String userAgent = request.getHeader("User-Agent").toUpperCase();
        if (userAgent == null || userAgent.equals("")) {
            return "";
        }
        if (userAgent.indexOf("MSIE") > 0) {
            browserName = "IE";
        } else if (userAgent.indexOf("FIREFOX") > 0) {
            browserName = "Firefox";
        } else if (userAgent.indexOf("CHROME") > 0) {
            browserName = "Chrome";
        } else if (userAgent.indexOf("SAFARI") > 0) {
            browserName = "Safari";
        } else if (userAgent.indexOf("CAMINO") > 0) {
            browserName = "Camino";
        } else if (userAgent.indexOf("KONQUEROR") > 0) {
            browserName = "Konqueror";
        } else if (userAgent.indexOf("EDGE") > 0) {
            browserName = "Microsoft Edge";
        }
        return browserName;
    }

  2、获取操作系统名称

public static String getOSname(HttpServletRequest request) {
        String osName = "";
        String userAgent = request.getHeader("User-Agent").toUpperCase();
        if (userAgent.contains("WINDOWS")) {
            if (userAgent.contains("WINDOWS NT 10.0")) {
                osName = "Windows 10";
            } else if (userAgent.contains("WINDOWS NT 6.2")) {
                osName = "Windows 8";
            } else if (userAgent.contains("WINDOWS NT 6.1")) {
                osName = "Windows 7";
            } else if (userAgent.contains("WINDOWS NT 6.0")) {
                osName = "Windows Vista";
            } else if (userAgent.contains("WINDOWS NT 5.2")) {
                osName = "Windows XP";
            } else if (userAgent.contains("WINDOWS NT 5.1")) {
                osName = "Windows XP";
            } else if (userAgent.contains("WINDOWS NT 5.01")) {
                osName = "Windows 2000";
            } else if (userAgent.contains("WINDOWS NT 5.0")) {
                osName = "Windows 2000";
            } else if (userAgent.contains("WINDOWS NT 4.0")) {
                osName = "Windows NT 4.0";
            } else if (userAgent.contains("WINDOWS 98; WIN 9X 4.90")) {
                osName = "Windows ME";
            } else if (userAgent.contains("WINDOWS 98")) {
                osName = "Windows 98";
            } else if (userAgent.contains("WINDOWS 95")) {
                osName = "Windows 95";
            } else if (userAgent.contains("WINDOWS CE")) {
                osName = "Windows CE";
            }
        } else if (userAgent.contains("MAC")){
            osName = "Mac";
        } else if (userAgent.contains("UNIX")){
            osName = "UNIX";
        } else if (userAgent.contains("LINUX")){
            osName = "Linux";
        } else if (userAgent.contains("SUNOS")){
            osName = "SunOS";
        }
        return osName;
    }

获取IP地址和第一种方式是一样的,获取后的效果是这样的:

第二种显然无法满足需求,推荐使用第一种工具类的方式获取。

 

欢迎关注

Java获取浏览器信息、IP地址、操作系统信息_第2张图片

 

你可能感兴趣的:(Java)