package com.example.webapp2.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Created by yeqiang on 3/26/19.
*/
public class InetUtils {
private static final Logger logger = LoggerFactory.getLogger(InetUtils.class);
private static final String[] HEADERS_TO_TRY = {
"X-Forwarded-For",
"Proxy-Client-IP",
"WL-Proxy-Client-IP",
"HTTP_X_FORWARDED_FOR",
"HTTP_X_FORWARDED",
"HTTP_X_CLUSTER_CLIENT_IP",
"HTTP_CLIENT_IP",
"HTTP_FORWARDED_FOR",
"HTTP_FORWARDED",
"HTTP_VIA",
"REMOTE_ADDR"};
/**
* 从请求头中读取真实代理服务器前客户端ip地址
*
* @param request
* @return
*/
public static String getProxyRealIp(HttpServletRequest request) {
String ip = null;
for (String key : HEADERS_TO_TRY) {
ip = request.getHeader(key);
if (ip != null) {
return ip;
}
}
return ip;
}
/**
* ipv4地址转长整形
*
* @param ip
* @return
*/
public static Long ip2long(String ip) {
try {
InetAddress address = InetAddress.getByName(ip);
if (address instanceof Inet4Address) {
long l = 0l;
for (int i = 0; i < 4; i++) {
byte b = address.getAddress()[i];
long l1 = (long) b & 0xFF;
l += l1 << ((3 - i) * 8);
}
return l;
}
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
return null;
}
/**
* 将一个长整数转换成ipv4字符串
*
* @param lng
* @return
*/
public static String long2IP(Long lng) {
byte[] array = new byte[4];
for (int i = 0; i < 4; i++) {
array[i] = (byte) ((lng >> ((3 - i) * 8)) & 0xFF);
}
try {
String ipv4 = InetAddress.getByAddress(array).toString().substring(1);
return ipv4;
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
return null;
}
/**
* 将ipv6地址字符串转为大整数
*
* @param ip
* @return
*/
public static BigInteger ipv6ToBitInteger(String ip) {
try {
InetAddress address = InetAddress.getByName(ip);
if (address instanceof Inet6Address) {
return new BigInteger(1, address.getAddress());
}
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
return null;
}
/**
* 将大整数转换成ipv6字符串
*
* @param bigInteger
* @return
*/
public static String bigIntegerToIPv6(BigInteger bigInteger) {
try {
String ipv6 = InetAddress.getByAddress(bigInteger.toByteArray()).toString().substring(1);
return ipv6;
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
return null;
}
}
单元测试
package com.example.webapp2.utils;
import org.junit.Test;
import java.math.BigInteger;
import java.net.UnknownHostException;
/**
* Created by yeqiang on 5/22/19.
*/
public class InetUtilsTest {
@Test
public void ip2long() throws Exception {
String ip = "192.168.0.1";
Long lng = InetUtils.ip2long(ip);
System.out.println(lng);
ip = InetUtils.long2IP(lng);
System.out.println(ip);
}
@Test
public void ipv6ToBigInteger() throws UnknownHostException {
String ipv6Address = "2001:0DB8:0000:0023:0008:0800:200C:417A";
BigInteger bigInteger = InetUtils.ipv6ToBitInteger(ipv6Address);
System.out.println(bigInteger);
System.out.println(InetUtils.bigIntegerToIPv6(bigInteger));
}
}
单元测试执行结果
42540766411282593502542288127932514682
2001:db8:0:23:8:800:200c:417a
3232235521
192.168.0.1