2021与蓝度共同重构项目,服务端
liuhaonan
2022-05-16 5c4101f44ee5261fc5a8cc65966d9af19b1a12e9
日志
已修改2个文件
已添加1个文件
148 ■■■■■ 文件已修改
ximon-admin/pom.xml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/src/main/java/com/sandu/ximon/admin/controller/LightController.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/src/main/java/com/sandu/ximon/admin/utils/IPUtils.java 141 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/pom.xml
@@ -75,6 +75,10 @@
            <scope>system</scope>
            <systemPath>${project.basedir}/minio-7.1.0-all.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>net.minidev</groupId>
            <artifactId>json-smart</artifactId>
        </dependency>
    </dependencies>
    <!-- Maven控制Spring Profile -->
ximon-admin/src/main/java/com/sandu/ximon/admin/controller/LightController.java
@@ -108,7 +108,8 @@
        if (length != 24) {
            return ResponseUtil.fail("设备编号长度不正确");
        }
        lightReportDataService.exportList(request,response, deviceCode);
        lightReportDataService.exportList(request, response, deviceCode);
        return ResponseUtil.success("导出成功");
    }
ximon-admin/src/main/java/com/sandu/ximon/admin/utils/IPUtils.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,141 @@
package com.sandu.ximon.admin.utils;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Enumeration;
@Slf4j
public class IPUtils {
    /**
     * èŽ·å–IP地址
     * <p>
     * ä½¿ç”¨Nginx等反向代理软件, åˆ™ä¸èƒ½é€šè¿‡request.getRemoteAddr()获取IP地址
     * å¦‚果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isBlank(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            log.error("IPUtils ERROR ", e);
        }
        //使用代理,则获取第一个IP地址
        if (StringUtils.isBlank(ip) && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }
    /**
     * @description èŽ·å–ç³»ç»ŸçŽ¯å¢ƒip
     */
    public static String getCurrentSystemIp() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface ni = networkInterfaces.nextElement();
                Enumeration<InetAddress> nias = ni.getInetAddresses();
                while (nias.hasMoreElements()) {
                    InetAddress ia = nias.nextElement();
                    if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                        return ia.toString().substring(1);
                    }
                }
            }
        } catch (SocketException e) {
            log.error("Failed to get current system Ip ", e);
        }
        return null;
    }
    public static String getIpAddrs(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.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                    ip = inet.getHostAddress();
                } catch (UnknownHostException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if (ip != null && ip.length() > 0) {
            //取第一个非unknown的有效IP字符串
            String[] split = ip.split(",");
            for (String s : split) {
                if (!"unknown".equalsIgnoreCase(s)) {
                    ip = s;
                    break;
                }
            }
        }
        return ip;
    }
    public static String getPublicIp() {
        try {
            String ip = "http://pv.sohu.com/cityjson?ie=utf-8";
            URL url = new URL(ip);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            BufferedReader in = new BufferedReader(new
                    InputStreamReader(urlConnection.getInputStream()));
            StringBuilder stringBuilder = new StringBuilder();
            String read;
            while ((read = in.readLine()) != null) {
                stringBuilder.append(read);
            }
            //可打印stringBuilder再解析得到的信息
            String string = StringUtils.split(stringBuilder.toString(), "=")[1];
            JSONObject jsonObject = JSONUtil.parseObj(string);
            return jsonObject.get("cip").toString();
        } catch (Exception e) {
            return "未知";
        }
    }
}