1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package io.common.utils;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.servlet.http.HttpServletRequest;
- public class IPUtils {
- private static Logger logger = LoggerFactory.getLogger(IPUtils.class);
-
- public static String getIpAddr(HttpServletRequest request) {
- String ip = null;
- try {
- ip = request.getHeader("x-forwarded-for");
- if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_CLIENT_IP");
- }
- if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_X_FORWARDED_FOR");
- }
- if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getRemoteAddr();
- }
- } catch (Exception e) {
- logger.error("IPUtils ERROR ", e);
- }
-
-
- return ip;
- }
-
- }
|