|
|
@ -1,5 +1,7 @@
|
|
|
|
package com.ruoyi.common.utils;
|
|
|
|
package com.ruoyi.common.utils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sun.net.util.IPAddressUtil;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -7,37 +9,65 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author ruoyi
|
|
|
|
* @author ruoyi
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class IpUtils
|
|
|
|
public class IpUtils {
|
|
|
|
{
|
|
|
|
public static String getIpAddr(HttpServletRequest request) {
|
|
|
|
public static String getIpAddr(HttpServletRequest request)
|
|
|
|
if (request == null) {
|
|
|
|
{
|
|
|
|
|
|
|
|
if (request == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return "unknown";
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String ip = request.getHeader("x-forwarded-for");
|
|
|
|
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");
|
|
|
|
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");
|
|
|
|
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");
|
|
|
|
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");
|
|
|
|
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();
|
|
|
|
ip = request.getRemoteAddr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|