zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    查找父/子控件(元素、节点)
    【转载】单点系统架构的可用性与性能优化
    【转载】互联网架构,如何进行容量设计?
    【转载】细聊分布式ID生成方法
    【转载】秒杀系统架构优化思路
    【转载】程序员这口饭-职业规划解决方案
    【转载】一位软件工程师的6年总结
    【转载】VS工具使用——代码生成函数关系图
    【转载】VS工具使用——代码图
    【转载】一些VS2013的使用技巧
  • 原文地址:https://www.cnblogs.com/wscw/p/14304133.html
Copyright © 2011-2022 走看看