Shopping project combat learning record (5) - Visitors to determine the IP area

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u012525096/article/details/86576520

Process

(1) obtained from the visitor Request IP: real IP, Proxy IP (x-forwarded-for fields) and so on.
(2) to obtain area information using third-party libraries or third-party API providers (such as Taobao http://ip.taobao.com/service/getIpInfo.php?ip=).
(3) Area information queries from the local database.

Code

(1) to obtain an IP from the Request:

    private static String getRemoteIp(HttpServletRequest request) {
        //x-forwarded-for:代表客户端,也就是HTTP的请求端真实的IP,只有在通过了HTTP代理或者负载均衡服务器时才会添加该项
        String ip = request.getHeader("x-forwarded-for");
        //Proxy-Client-IP和WL-Proxy-Client-IP: 只有在Apache(Weblogic Plug-In Enable)+WebLogic搭配下出现,其中“WL”就是WebLogic的缩写
        //访问路径是:Client -> Apache WebServer + Weblogic http plugin -> Weblogic Instances
        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();
        }
//        ip = "61.51.253.90";
//        ip = "218.25.19.153";
        //0:0:0:0:0:0:0:1: IPV6的形式,win7下可能会出现
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }

(2) obtain the area from IP:

    private static String getIpInfo(String ip) {
        HttpGet httpGet = new HttpGet(baseUrl + ip);
        try {
            HttpResponse response = client.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
                String tmp;
                String content = "";
                while((tmp = reader.readLine()) != null) {
                    content += tmp+"\r\n";
                }
                return content;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

(3) For more information Area queries from the local database

    public static Area getArea(HttpServletRequest request) {
        String ip = getRemoteIp(request);
        if (Objects.equals("127.0.0.1", ip)) {
            Area area = new Area();
            area.setId(367l);
            area.setName("北京市");
            return area;
        }
        String ipResp = getIpInfo(ip);
        Response response = JSON.parseObject(ipResp, Response.class);
        if (response.getCode() != 0) {
            //FIXME 记录该用户/浏览器上一次访问时所在的城市,若没有则默认北京
            Area area = new Area();
            area.setId(367l);
            area.setName("北京市");
            return area;
        }
        IpInfo info = response.getData();
        //本网站的业务只在中国开放
        if (!Objects.equals("中国", info.getCountry())) {
            //FIXME 记录该用户/浏览器上一次访问时所在的城市,若没有则默认北京
            Area area = new Area();
            area.setId(367l);
            area.setName("北京市");
            return area;
        }

        Area area = areaService.getByName(info.getCity());
        if (null == area) {
            //FIXME 记录该用户/浏览器上一次访问时所在的城市,若没有则默认北京
            area = new Area();
            area.setId(367l);
            area.setName("北京市");
            return area;
        }
        return area;
    }

Note: (1) Responsethe entity class JSON API Taobao constructed to provide a return for parsing JSON.
(2) areaService.getByName(info.getCity());Details of the region for the query from the local database.

Guess you like

Origin blog.csdn.net/u012525096/article/details/86576520