import com.yundaex.common.validation.asserts.YDAssert; import com.yundaex.common.validation.asserts.YDIllegalArgumentException; public class IPUtils { /*** * 校验IP段是否一致 * IP段的前三部分必须一直, 比如222.72.45.0-222.72.45.255, 不能是222.72.45.0 - 110.72.45.255 * @param startSection * @param endSection * @return */ public static boolean verifyIPSectionMeet(String startSection, String endSection){ boolean startIpMeet = verifyIPMeet(startSection); if (!startIpMeet){ throw new YDIllegalArgumentException("开始段IP不合法。"); } boolean endIpMeet = verifyIPMeet(endSection); if (!endIpMeet){ throw new YDIllegalArgumentException("结束段IP不合法。"); } if (startIpMeet && endIpMeet){ String startIpPrefix = startSection.substring(0, startSection.lastIndexOf(".") + 1); String endIpPrefix = endSection.substring(0, startSection.lastIndexOf(".") + 1); if (startIpPrefix.equals(endIpPrefix)){ return true; } } return false; } /*** * 校验IP是否合法 * @param ip * @return */ public static boolean verifyIPMeet(String ip){ YDAssert.isNotNull(ip, "IP不能为空。"); // 定义正则表达式 String regex = "^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\." + "(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\." + "(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\." + "(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$"; // 判断ip地址是否与正则表达式匹配 if (ip.matches(regex)) { // 返回判断信息 return true; } return false; } }
private void checkIpMeet(String ipAddress){ if ("0.0.0.0".equalsIgnoreCase(ipAddress)){ return; } boolean ipIsVerifyflag = IPUtils.verifyIPMeet(ipAddress); if (!ipIsVerifyflag){ throw new YDIllegalArgumentException("IP配置出错,IP不合法:" + ipAddress); } }
package me.zhengjie.common.utils; import javax.servlet.http.HttpServletRequest; /** * 获取ip地址 * @author X-rapido * @date 2018-11-24 */ public class IpUtil { public static String getIP(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.getRemoteAddr(); } return "0:0:0:0:0:0:0:1".equals(ip)?"127.0.0.1":ip; } }
package org.linlinjava.litemall.core.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.servlet.http.HttpServletRequest; import java.net.InetAddress; import java.net.UnknownHostException; /** * IP地址相关工具类 */ public class IpUtil { private static final Log logger = LogFactory.getLog(IpUtil.class); public static String getIpAddr(HttpServletRequest request) { String ipAddress; 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) { logger.error(e.getMessage(), e); } ipAddress = inet.getHostAddress(); } } // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length() // = 15 if (ipAddress.indexOf(",") > 0) { ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); } } } catch (Exception e) { ipAddress = ""; } return ipAddress; } }