java 如何让利用ip 实现登录日志功能

1.导入如下pom

org.nutz.http.Http;

org.nutz.http.Response;

iorg.nutz.json.Json;

 
        
            org.nutz
            nutz
            1.r.55
        

2.新建实体类

package com.example.springboot.shiro.common.utils.ip;

/**
 * Created by joe强 on 2018/8/31 2:06
 */
public class TaobaoIPResult {
    private int code;
    private String country;
    private String area;
    private String region;
    private String city;
    private String county;
    private String isp;
    private String ip;

    public TaobaoIPResult() {
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getArea() {
        return this.area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getRegion() {
        return this.region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCounty() {
        return this.county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public String getIsp() {
        return this.isp;
    }

    public void setIsp(String isp) {
        this.isp = isp;
    }

    public String getIp() {
        return this.ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }
}


3.调用第三方接口,网上有很多这里是 淘宝的接口

package com.example.springboot.shiro.common.utils.ip;


import org.nutz.http.Http;
import org.nutz.http.Response;
import org.nutz.json.Json;

import java.util.Map;

/**
 * Created by joe强 on 2018/8/31 2:07
 */
public class TaobaoIP {
    public TaobaoIP() {
    }

    public static TaobaoIPResult getResult(String ip) {
        Response response = Http.get("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
       TaobaoIPResult result = new TaobaoIPResult();
        if (ip != null && response.getStatus() == 200) {
            try {
                String content = response.getContent();
                Map contentMap = (Map) Json.fromJson(content);
                if (((Integer)((Integer)contentMap.get("code"))).intValue() == 0) {
                    Map dataMap = (Map)contentMap.get("data");
                    result.setCountry((String)dataMap.get("country"));
                    result.setRegion((String)dataMap.get("region"));
                    result.setCity((String)dataMap.get("city"));
                    result.setCounty((String)dataMap.get("county"));
                    result.setIsp((String)dataMap.get("isp"));
                    result.setArea((String)dataMap.get("area"));
                    result.setIp((String)dataMap.get("ip"));
                    result.setCode(0);
                    return result;
                }
            } catch (Exception var6) {
                ;
            }
        }

        result.setCode(-1);
        result.setCountry("XX");
        result.setRegion("XX");
        result.setCity("XX");
        result.setCounty("XX");
        result.setIsp("XX");
        result.setArea("XX");
        result.setIp(ip);
        return result;
    }
}

4.网上有很多方法,原理都差不多,但是有的会请求超时,这个方法暂且没有这种情况,如有请检查URL是否写的正确!

你可能感兴趣的:(Springboot,Utils)