java获取请求的ip,获取浏览器请求的设备信息

1.java获取 request 中的ip信息

/**
    * 获取ip地址
    * @author gaodongyang
    * @date 2020/8/11 14:06
    * @param request 请求的request
    * @return String ip地址
    **/
    private static String getIp(HttpServletRequest request){
        String ipAddress = request.getHeader("x-forwarded-for");
        String unknown = "unknown";
        if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            String benji = "127.0.0.1";
            String bj = "0:0:0:0:0:0:0:1";
            if (benji.equals(ipAddress) || bj.equals(ipAddress)) {
                ///根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                if(inet != null){
                    ipAddress = inet.getHostAddress();
                }
            }
        }
        ///对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
        int i = 15;
        String s = ",";
        if (ipAddress != null && ipAddress.length() > i) {
            if (ipAddress.indexOf(s) > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

2. spring中spring security获取ip信息

String ip = ((WebAuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails()).getRemoteAddress();

3. 使用UserAgentUtils浏览器解析工具获取设备信息

  1. 导入maven依赖
	
     <dependency>
         <groupId>eu.bitwalkergroupId>
         <artifactId>UserAgentUtilsartifactId>
         <version>1.21version>
     dependency>
  1. 使用
//request为请求的request
String device = getDevice(request.getHeader("User-Agent"));

 /**
     * 解析 用户代理(User-Agent)
     * @param userAgent 用户代理User-Agent ,UA
     * @return "设备类型:%s,操作系统:%s,浏览器:%s,浏览器版本:%s,浏览器引擎:%s,用户代理(User-Agent):[%s]"
     * @author GongLiHai
     * @date 2020/8/25 11:12
     */
    private String getDevice(String userAgent) {
     	//解析agent字符串
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        //获取浏览器对象
        Browser browser = ua.getBrowser();
        //获取操作系统对象
        OperatingSystem os = ua.getOperatingSystem();
        return String.format("设备类型:%s,操作系统:%s,浏览器:%s,浏览器版本:%s,浏览器引擎:%s,用户代理(User-Agent):[%s]",
                os.getDeviceType(),
                os.getName(),
                browser.getName(),
                browser.getVersion(userAgent),
                browser.getRenderingEngine(),
                userAgent
        );
    }

4. 自己写的java从 request 中获取信息

/**
    * 获取请求设备信息
    * @author gaodongyang
    * @date 2020/8/11 14:19
    * @param request 请求
    * @return String 设备信息 pc端还是手机端
    **/
    private static String getDeviceInfo(HttpServletRequest request) {
        ///定义正则
        String pattern = "^Mozilla/\\d\\.\\d\\s+\\(+.+?\\)";
        String pattern2 = "\\(+.+?\\)";
        ///将给定的正则表达式编译到模式中
        Pattern r = Pattern.compile(pattern);
        Pattern r2 = Pattern.compile(pattern2);

        String userAgent = request.getHeader("User-Agent");
        ///创建匹配给定输入与此模式的匹配器
        Matcher m = r.matcher(userAgent);
        String result = null;
        if (m.find()) {
            result = m.group(0);
        }
        if(result == null){
            return null;
        }
        Matcher m2 = r2.matcher(result);
        if (m2.find()) {
            result = m2.group(0);
        }
        result = result.replace("(", "");
        result = result.replace(")", "");

        if (StringUtils.isBlank(result)) {
            return null;
        }
        result = result.replace(" U;", "");
        result = result.replace(" zh-cn;", "");

        String android = "Android";
        String iPhone = "iPhone";
        String iPad = "iPad";
        if(result.contains(android) || result.contains(iPhone) || result.contains(iPad)){
            return "移动端";
        }else{
            return "PC端";
        }
    }

你可能感兴趣的:(java,java)