ip工具类

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yunduan.request.LocationDto;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class IpUtils {
    /***
     * 获取客户端ip地址
     * @param request
     */
    public static String getIP(final HttpServletRequest request) throws Exception {
        if (request == null) {
            throw (new Exception("getIpAddr method HttpServletRequest Object is null"));
        }
        String ipStr = request.getHeader("x-forwarded-for");
        if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
            ipStr = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
            ipStr = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
            ipStr = request.getRemoteAddr();
        }

        // 多个路由时,取第一个非unknown的ip
        final String[] arr = ipStr.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ipStr = str;
                break;
            }
        }
        //目的是将localhost访问对应的ip 0:0:0:0:0:0:0:1 转成 127.0.0.1。
        return ipStr.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipStr;
    }



    public static String getAddressByIp(String ip) {
        try {
            URL url = new URL("http://opendata.baidu.com/api.php?query=" + ip+"&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=utf-8&format=json");;
            URLConnection conn = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            StringBuffer result = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();
            JSONObject jsStr = new JSONObject(result.toString());
//            JSONArray jsData = (JSONArray) jsStr.get("data");
            List<LocationDto> locationDtos = JSONArray.parseArray(jsStr.get("data").toString(), LocationDto.class);
//            JSONObject data= (JSONObject) jsData.get(0);//位置
            LocationDto locationDto = locationDtos.get(0);
            return locationDto.getLocation();
        } catch (IOException e) {
            return "读取失败";
        }
    }

}


获取ip进行判断


@ApiOperation(httpMethod = "GET", value = "获取ip")
    @RequestMapping(value = "/getIP", method = RequestMethod.GET)
    public Map<String,String> getIP(HttpServletRequest servletRequest) {
        try {
            String ip = IpUtils.getIP(servletRequest);
            String[] privinces={"北京市","本地局域网","天津市","河北省","山西省","内蒙古","辽宁省","吉林省","黑龙江省","上海市","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","广西","海南省","重庆市","四川省","贵州省","云南省","西藏","陕西省","甘肃省","青海省","宁夏","新疆","台湾省","香港","澳门"};
            String address=IpUtils.getAddressByIp(ip);
            boolean china=false;
            for(String privince:privinces){
                if(address.startsWith(privince)){
                    china=true;
                    break;
                }
            }
            Map<String, String> map = new HashMap<>();
            map.put("ip",ip);
            map.put("isChina",china?"1":"2");
            map.put("address",address);
            return  map;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

 }