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;
    }
  • 相关阅读:
    HDU 1301 Jungle Roads (最小生成树)
    POJ 1733 Parity game (并查集)
    HDU 3038 How Many Answers Are Wrong (并查集)
    CentOS用yum安装搭建LAMP
    Linux下php安装Redis扩展
    PHPExcel用法
    利用phpmailer类邮件发送
    Vim编辑器配置
    vhost文件设置
    ThinkPHP验证码类
  • 原文地址:https://www.cnblogs.com/wscw/p/14304133.html
Copyright © 2011-2022 走看看