2 changed files with 42 additions and 1 deletions
@ -0,0 +1,40 @@ |
|||
package com.dongjian.dashboard.back.util; |
|||
|
|||
import jakarta.servlet.http.HttpServletRequest; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
|
|||
@Slf4j |
|||
public class IPUtils { |
|||
|
|||
private IPUtils() {} |
|||
|
|||
public static String getClientIp(HttpServletRequest request) { |
|||
if (request == null) { |
|||
return ""; |
|||
} |
|||
|
|||
String ip = request.getHeader("X-Forwarded-For"); |
|||
if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { |
|||
return ip.split(",")[0].trim(); |
|||
} |
|||
|
|||
ip = request.getHeader("X-Real-IP"); |
|||
if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { |
|||
return ip; |
|||
} |
|||
|
|||
ip = request.getHeader("Proxy-Client-IP"); |
|||
if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { |
|||
return ip; |
|||
} |
|||
|
|||
ip = request.getHeader("WL-Proxy-Client-IP"); |
|||
if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { |
|||
return ip; |
|||
} |
|||
|
|||
return request.getRemoteAddr(); |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue