package com.das.common.util; import org.apache.commons.lang3.StringUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; /** * @Author lime * @Date 2018/1/31 */ public class IpUtil { public static String getIpAddress(HttpServletRequest request) { String xIp = request.getHeader("X-Real-IP"); String xFor = request.getHeader("X-Forwarded-For"); if(StringUtils.isNotEmpty(xFor) && !"unKnown".equalsIgnoreCase(xFor)){ //多次反向代理后会有多个ip值,第一个ip才是真实ip int index = xFor.indexOf(","); if(index != -1){ return xFor.substring(0,index); }else{ return xFor; } } xFor = xIp; if(StringUtils.isNotEmpty(xFor) && !"unKnown".equalsIgnoreCase(xFor)){ return xFor; } if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) { xFor = request.getHeader("Proxy-Client-IP"); } if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) { xFor = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) { xFor = request.getHeader("HTTP_CLIENT_IP"); } if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) { xFor = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) { xFor = request.getRemoteAddr(); } return xFor; } public static String getIpAddress() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); return getIpAddress(request); } }
啦啦啦
原文地址:https://www.cnblogs.com/ClassNotFoundException/p/9007498.html
时间: 2024-10-27 16:53:43