From 4516a6b356fe280defdf56a1b3aa150628137baf Mon Sep 17 00:00:00 2001 From: Skqing <569141948@qq.com> Date: Tue, 16 Oct 2018 12:47:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=A6=82=E6=9E=9C=E6=98=AF?= =?UTF-8?q?=E5=86=85=E7=BD=91IP=E5=88=99=E4=B8=8D=E5=8E=BB=E8=8E=B7?= =?UTF-8?q?=E5=8F=96IP=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/AddressUtils.java | 2 +- .../java/com/ruoyi/common/utils/IpUtils.java | 64 ++++++++++++++----- .../manager/factory/AsyncFactory.java | 16 +++-- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/AddressUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/AddressUtils.java index 1cbdb143..67d72bd0 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/AddressUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/AddressUtils.java @@ -20,7 +20,7 @@ public class AddressUtils public static String getRealAddressByIP(String ip) { - String address = "XX XX"; + String address = "address disabled"; if (Global.isAddressEnabled()) { String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip); diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/IpUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/IpUtils.java index 9b5af273..80010314 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/IpUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/IpUtils.java @@ -1,5 +1,7 @@ package com.ruoyi.common.utils; +import sun.net.util.IPAddressUtil; + import javax.servlet.http.HttpServletRequest; /** @@ -7,37 +9,65 @@ import javax.servlet.http.HttpServletRequest; * * @author ruoyi */ -public class IpUtils -{ - public static String getIpAddr(HttpServletRequest request) - { - if (request == null) - { +public class IpUtils { + public static String getIpAddr(HttpServletRequest request) { + if (request == null) { return "unknown"; } String ip = request.getHeader("x-forwarded-for"); - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) - { + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) - { + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Forwarded-For"); } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(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)) - { + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) - { + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip; } -} + + + public static boolean internalIp(String ip) { + byte[] addr = IPAddressUtil.textToNumericFormatV4(ip); + return internalIp(addr); + } + + private static boolean internalIp(byte[] addr) { + final byte b0 = addr[0]; + final byte b1 = addr[1]; + //10.x.x.x/8 + final byte SECTION_1 = 0x0A; + //172.16.x.x/12 + final byte SECTION_2 = (byte) 0xAC; + final byte SECTION_3 = (byte) 0x10; + final byte SECTION_4 = (byte) 0x1F; + //192.168.x.x/16 + final byte SECTION_5 = (byte) 0xC0; + final byte SECTION_6 = (byte) 0xA8; + switch (b0) { + case SECTION_1: + return true; + case SECTION_2: + if (b1 >= SECTION_3 && b1 <= SECTION_4) { + return true; + } + case SECTION_5: + switch (b1) { + case SECTION_6: + return true; + } + default: + return false; + } + + } +} \ No newline at end of file diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/manager/factory/AsyncFactory.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/manager/factory/AsyncFactory.java index 4542e84f..ac2e4dc0 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/manager/factory/AsyncFactory.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/manager/factory/AsyncFactory.java @@ -1,10 +1,8 @@ package com.ruoyi.framework.manager.factory; -import java.util.TimerTask; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.utils.AddressUtils; +import com.ruoyi.common.utils.IpUtils; import com.ruoyi.framework.shiro.session.OnlineSession; import com.ruoyi.framework.util.LogUtils; import com.ruoyi.framework.util.ServletUtils; @@ -17,6 +15,10 @@ import com.ruoyi.system.service.ISysOperLogService; import com.ruoyi.system.service.impl.SysLogininforServiceImpl; import com.ruoyi.system.service.impl.SysUserOnlineServiceImpl; import eu.bitwalker.useragentutils.UserAgent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.TimerTask; /** * 异步工厂(产生任务用) @@ -72,8 +74,12 @@ public class AsyncFactory @Override public void run() { - // 远程查询操作地点 - operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp())); + if (IpUtils.internalIp(operLog.getOperIp())) { + operLog.setOperLocation("内网IP"); + } else { + // 远程查询操作地点 + operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp())); + } SpringUtils.getBean(ISysOperLogService.class).insertOperlog(operLog); } };