package com.public.util;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class IpUtil {
private IpUtil(){
}
public static String getIpAddr() {
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
return getIpAddr(request);
}
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
log.error(e.getMessage());
}
if(inet != null) {
ipAddress = inet.getHostAddress();
}else{
log.error("get inet error");
}
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) {
if (ipAddress.indexOf(',') > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(','));
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
ipAddress = "";
}
return ipAddress;
}
/**
* 将数字转成ip地址
*
* @param IpNum 数字
* @return 转换后的ip地址
*/
public static String getNumConvertIp(long ipLong) {
long[] mask = { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 };
long num = 0;
StringBuilder ipInfo = new StringBuilder();
for (int i = 0; i < 4; i++) {
num = (ipLong & mask[i]) >> (i * 8);
if (i > 0)
ipInfo.insert(0, ".");
ipInfo.insert(0, Long.toString(num, 10));
}
return ipInfo.toString();
}
/**
* 将ip地址转换成数字
*
* @param ipAddress 传入的ip地址
* @return 转换成数字类型的ip地址
*/
public static long getIpConvertNum(String ipAddress) {
String[] ip = ipAddress.split("\\.");
long a = Integer.parseInt(ip[0]);
long b = Integer.parseInt(ip[1]);
long c = Integer.parseInt(ip[2]);
long d = Integer.parseInt(ip[3]);
return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
}
public static long getBeginIpLong(String ip, String maskBit){
return getIpConvertNum(ip) & getIpConvertNum(getMask(Integer.parseInt(maskBit)));
}
public static String getBeginIpStr(String ip, String maskBit){
return getNumConvertIp(getBeginIpLong(ip, maskBit));
}
public static long getEndIpLong(String ip, String maskBit){
return getBeginIpLong(ip, maskBit) + getIpCount(maskBit) - 1L;
}
public static String getEndIpStr(String ip, String maskBit){
return getNumConvertIp(getEndIpLong(ip, maskBit));
}
public static int getIpCount(String mask){
return BigDecimal.valueOf(Math.pow(2, (32 - Integer.parseInt(mask)))).setScale(0, BigDecimal.ROUND_DOWN).intValue();
}
public static String getMask(int maskLen){
int binaryMask = 0xFFFFFFFF << (32 - maskLen);
StringBuilder sb = new StringBuilder();
for(int shift = 24; shift > 0 ; shift -=8){
sb.append(((binaryMask >>> shift) & 0xFF));
sb.append(".");
}
sb.append((binaryMask & 0xFF));
return sb.toString();
}
public static int getPrefixMask(String netmask){
String[] data = netmask.split("\\.");
int len = 0;
for (String n : data) {
len += (8 - Math.log((double)256-Integer.parseInt(n)) / Math.log(2));
}
return len;
}
}
Java IP地址转换工具类
于 2023-12-27 15:45:55 首次发布