
commons.box.util.Servlets Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-box-app Show documentation
Show all versions of commons-box-app Show documentation
Common utils for BOX projects.
The newest version!
package commons.box.util;
import commons.box.app.AppLog;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
* 创建作者:xingxiuyi
* 版权所属:xingxiuyi
*/
public class Servlets {
private static final AppLog LOG = Logs.get(Servlets.class);
public static final String[] REMOTE_ADDR_HEADER_NAMES = {
"X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"
};
/**
* 获取真实IP地址
*
* @param request
* @return
*/
public static final String remoteAddr(HttpServletRequest request) {
String ip = Strs.trim(request.getHeader("X-Forwarded-For"));
try {
if (Strs.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
Enumeration rhs = request.getHeaderNames();
while ((Strs.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) && rhs.hasMoreElements()) {
String hn = rhs.nextElement();
for (String dn : REMOTE_ADDR_HEADER_NAMES) {
if (Strs.equalsIgnoreCase(hn, dn)) {
ip = Strs.trim(request.getHeader(hn));
break;
}
}
}
}
if (Strs.contains(ip, ",")) {
String[] ips = ip.split(",");
for (String ip1 : ips) {
String tip1 = Strs.trim(ip1);
if (!("unknown".equalsIgnoreCase(tip1))) {
ip = tip1;
break;
}
}
}
} catch (Exception ignored) {
}
if (Strs.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) ip = request.getRemoteAddr();
return ip;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy