bug fixed for getRemoteAddress.

This commit is contained in:
wenshao
2018-09-12 20:32:40 +08:00
parent e92e385609
commit bf30592712
2 changed files with 35 additions and 1 deletions
@@ -262,7 +262,6 @@ public class WebSessionStat {
}
if (remoteAddresses.length() > 256) {
LOG.error("session ip change too many");
return;
}
@@ -23,19 +23,54 @@ public class DruidWebUtils {
public static String getRemoteAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip != null && !isValidAddress(ip)) {
ip = null;
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
if (ip != null && !isValidAddress(ip)) {
ip = null;
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
if (ip != null && !isValidAddress(ip)) {
ip = null;
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (ip != null && !isValidAddress(ip)) {
ip = null;
}
}
return ip;
}
private static boolean isValidAddress(String ip) {
if (ip == null) {
return false;
}
for (int i = 0; i < ip.length(); ++i) {
char ch = ip.charAt(i);
if (ch >= '0' && ch <= '9') {
} else if (ch >= 'A' && ch <= 'F') {
} else if (ch >= 'a' && ch <= 'f') {
} else if (ch == '.' || ch == ':') {
//
} else {
return false;
}
}
return true;
}
private static String getContextPath_2_5(ServletContext context) {
String contextPath = context.getContextPath();