• springboot获取请求ip、系统、浏览器等信息


    1、添加依赖

    <!-- https://mvnrepository.com/artifact/eu.bitwalker/UserAgentUtils -->
            <dependency>
                <groupId>eu.bitwalker</groupId>
                <artifactId>UserAgentUtils</artifactId>
                <version>1.21</version>
            </dependency>

    2、使用方法

    UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("user-agent"));
            String clientType = userAgent.getOperatingSystem().getDeviceType().toString();
            LOGGER.info("clientType = " + clientType);   //客户端类型  手机、电脑、平板
            String os = userAgent.getOperatingSystem().getName();
            LOGGER.info("os = " + os);    //操作系统类型
            String ip = IpUtil.getIpAddr(request);
            LOGGER.info("ip = " + ip);    //请求ip
            String browser = userAgent.getBrowser().toString();
            LOGGER.info("browser = " + browser);    浏览器类型

      //其中ip的获取方式
        public static String getIpAddr(HttpServletRequest request) {

    String ip = request.getHeader("x-forwarded-for");

    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("X-Real-IP");
    //LOGGER.error("X-Real-IP:"+ip);
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("http_client_ip");
          // LOGGER.error("http_client_ip:"+ip);
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getRemoteAddr();
          // LOGGER.error("getRemoteAddr:"+ip);
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("Proxy-Client-IP");
          // LOGGER.error("Proxy-Client-IP:"+ip);
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("WL-Proxy-Client-IP");
          // LOGGER.error("WL-Proxy-Client-IP:"+ip);
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("HTTP_X_FORWARDED_FOR");
          // LOGGER.error("HTTP_X_FORWARDED_FOR:"+ip);
    }
      // 如果是多级代理,那么取第一个ip为客户ip
    if (ip != null && ip.indexOf(",") != -1) {
    ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
          // LOGGER.error("ip:"+ip);
    }
    return ip;
    }
  • 相关阅读:
    首次搭建微信小程序记录各种细节
    vue3.x 首次搭建
    通过nodejs 下载阿里云省、市、区的json文件,使用在echart上
    vscode
    使用 ts + and design + react-redux + react-router-dom + ahook 开发 管理后台
    Python 字典(dict) 操作基础
    高斯混合模型视频背景建模的EM算法与Matlab 实现
    Tensorflow 10分钟快速上手
    Ubuntu16.04LTS 搜狗输入法无法输入中文
    Machine Learning Class 1:Introduction,supervised & unsupervised learning
  • 原文地址:https://www.cnblogs.com/wscw/p/14304133.html
Copyright © 2020-2023  润新知