web 端获取用户访问 ip 与访问浏览器

简介

Java 服务端提供接口,当客户端登录或访问接口的时候,服务端记录访问用户的 IP 与浏览器信息到数据库

想要获取用户访问信息,在接口需要接收 HttpServletRequest 对象

一、获取用户访问ip

通过 HttpServletRequest 请求,获取访问庄外网地址

 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("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.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        if (ip.split(",").length > 1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }

通过 IP 获取用户地址

  /**
     * 通过IP获取地址(需要联网,调用淘宝的IP库)
     *
     * @param ip
     * @return
     */
    public static String getIpInfo(String ip) {
        if ("127.0.0.1".equals(ip)) {
            ip = "127.0.0.1";
        }
        String info = "";
        try {
            URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
            HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
            htpcon.setRequestMethod("GET");
            htpcon.setDoOutput(true);
            htpcon.setDoInput(true);
            htpcon.setUseCaches(false);

            InputStream in = htpcon.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
            StringBuffer temp = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                temp.append(line).append("\r\n");
                line = bufferedReader.readLine();
            }
            bufferedReader.close();
            JSONObject obj = (JSONObject) JSON.parse(temp.toString());
            if (obj.getIntValue("code") == 0) {
                JSONObject data = obj.getJSONObject("data");
                info += data.getString("country") + " ";
                info += data.getString("region") + " ";
                info += data.getString("city") + " ";
                info += data.getString("isp");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }

二、获取用户访问的浏览器信息

获取用户访问的浏览器信息,需要通过 User-Agent ,代码如下

  String userAgent = request.getHeader("User-Agent");

UserAgentUtil 工具类获取具体的浏览器信息

public class UserAgentUtil {

    public static String getUserAgent(String userAgent) {
        if(userAgent==""||userAgent==null){
            userAgent="";
        }
        if (userAgent.contains("Windows")) {
            if (userAgent.contains("Windows NT 10.0")) {
                return judgeBrowser(userAgent, "Windows 10");
            } else if (userAgent.contains("Windows NT 6.2")) {
                return judgeBrowser(userAgent, "Windows 8");
            } else if (userAgent.contains("Windows NT 6.1")) {
                return judgeBrowser(userAgent, "Windows 7" );
            } else if (userAgent.contains("Windows NT 6.0")) {
                return judgeBrowser(userAgent, "Windows Vista");
            } else if (userAgent.contains("Windows NT 5.2")) {
                return judgeBrowser(userAgent, "Windows XP");
            } else if (userAgent.contains("Windows NT 5.1")) {
                return judgeBrowser(userAgent, "Windows XP");
            } else if (userAgent.contains("Windows NT 5.01")) {
                return judgeBrowser(userAgent, "Windows 2000");
            } else if (userAgent.contains("Windows NT 5.0")) {
                return judgeBrowser(userAgent, "Windows 2000");
            } else if (userAgent.contains("Windows NT 4.0")) {
                return judgeBrowser(userAgent, "Windows NT 4.0");
            } else if (userAgent.contains("Windows 98; Win 9x 4.90")) {
                return judgeBrowser(userAgent, "Windows ME");
            } else if (userAgent.contains("Windows 98")) {
                return judgeBrowser(userAgent, "Windows 98");
            } else if (userAgent.contains("Windows 95")) {
                return judgeBrowser(userAgent, "Windows 95");
            } else if (userAgent.contains("Windows CE")) {
                return judgeBrowser(userAgent, "Windows CE");
            }
        } else if (userAgent.contains("Mac OS X")) {
            if(userAgent.contains("iPhone")){
                return judgeBrowser(userAgent, "iPhone");
            }
            else if (userAgent.contains("iPad")) {
                return judgeBrowser(userAgent, "iPad");//判断系统
            }else{
                return judgeBrowser(userAgent, "Mac");//判断系统
            }
        }else if(userAgent.contains("Android")){
            return judgeBrowser(userAgent, "Android");//判断系统
        }else if(userAgent.contains("Linux")){
            return judgeBrowser(userAgent, "Linux");//判断系统
        }else if(userAgent.contains("FreeBSD")){
            return judgeBrowser(userAgent, "FreeBSD");//判断系统
        }else if(userAgent.contains("Solaris")){
            return judgeBrowser(userAgent, "Solaris");//判断系统
        }
        return judgeBrowser(userAgent, "其他");
    }

    private static String judgeBrowser(String userAgent, String platformType) {
        if (userAgent.contains("Edge")) {
            return "Microsoft Edge";
        }else if(userAgent.contains("QQBrowser")){
            return "腾讯浏览器";
        }else if (userAgent.contains("Chrome")&&userAgent.contains("Safari")) {
            return "Chrome";
        } else if (userAgent.contains("")) {
            return "Firefox";
        }else if (userAgent.contains("360")) {//Internet Explorer 6
            return "360浏览器";
        }else if (userAgent.contains("Opera")) {//Internet Explorer 6
            return "Opera";
        }else if (userAgent.contains("Safari")&&!userAgent.contains("Chrome")) {
            return "Safari";
        }else if (userAgent.contains("MetaSr1.0")) {//Internet Explorer 6
            return "搜狗浏览器";
        }else if (userAgent.contains("TencentTraveler")) {//Internet Explorer 6
            return "腾讯浏览器";
        }else if (userAgent.contains("UC")) {//Internet Explorer 6
            return "UC浏览器";
        }else if (userAgent.contains("MSIE")) {
            if (userAgent.contains("MSIE 10.0")) {//Internet Explorer 10
                return "IE 10";
            } else if (userAgent.contains("MSIE 9.0")) {//Internet Explorer 9
                return "IE 9";
            } else if (userAgent.contains("MSIE 8.0")) {//Internet Explorer 8
                return "IE 8";
            } else if (userAgent.contains("MSIE 7.0")) {//Internet Explorer 7
                return "IE 7";
            } else if (userAgent.contains("MSIE 6.0")) {//Internet Explorer 6
                return "IE 6";
            }
        } else {//暂时支持以上三个主流.其它浏览器,待续...
            return "其他";
        }
        return "其他";
    }
}
View Code

参考:http://www.kaluokeji.com/find/1.html

参考:https://blog.csdn.net/x_bessie/article/details/93629670

posted @ 2020-12-18 17:51  byebai95  阅读(1058)  评论(0编辑  收藏  举报