AddressUtils.java
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.huaheng.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.framework.config.HuaHengConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
 * 获取地址类
 *
 * @author huaheng
 */
@Component
public class AddressUtils {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
    public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
    @Resource
    private Environment environment;
    public static String getRealAddressByIP(String ip) {
        String address = "";
        try {
            if (HuaHengConfig.isAddressEnabled()) {
                address = HttpUtils.sendPost(IP_URL, "ip=" + ip);
                JSONObject json = JSONObject.parseObject(address);
                JSONObject object = json.getObject("data", JSONObject.class);
                String region = object.getString("region");
                String city = object.getString("city");
                address = region + " " + city;
            }
        } catch (Exception e) {
            log.error("获取地理位置异常:", e);
        }
        return address;
    }
    public static boolean isAlllowIp() {
        try {
            InetAddress address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();
//            System.out.println("-------address---------"+ip);
//            if (ip.equals("172.16.2.10") || ip.equals("172.16.93.238")) {
            if (ip.equals("172.16.2.10")) {
                return true;
            }
        } catch (UnknownHostException e) {
            return false;
        }
        return false;
    }
    public static boolean isAlllowIp(String ipconfig) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();
//            System.out.println("-------address---------"+ip);
//            if (ip.equals("172.16.2.10") || ip.equals("172.16.93.238")) {
            if (ip.equals(ipconfig)) {
                return true;
            }
        } catch (UnknownHostException e) {
            return false;
        }
        return false;
    }
    public   Boolean isAlllowPort() {
        try {
            String port=environment.getProperty("server.port");
            if(StringUtils.isNotEmpty(port)&&port.equals("8016")){
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }
}