Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.alibaba.druid.support.http.stat.WebSessionStat;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.DruidWebUtils;
import com.alibaba.druid.util.DruidWebUtilsJakarta;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
Expand Down Expand Up @@ -128,7 +128,7 @@ protected String getRemoteAddress(HttpServletRequest request) {
ip = request.getHeader(realIpHeader);
}
if (ip == null || ip.length() == 0) {
ip = DruidWebUtils.getRemoteAddr(request);
ip = DruidWebUtilsJakarta.getRemoteAddr(request);
}
return ip;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.alibaba.druid.support.profile.ProfileEntryKey;
import com.alibaba.druid.support.profile.ProfileEntryReqStat;
import com.alibaba.druid.support.profile.Profiler;
import com.alibaba.druid.util.DruidWebUtils;
import com.alibaba.druid.util.DruidWebUtilsJakarta;
import com.alibaba.druid.util.PatternMatcher;
import com.alibaba.druid.util.ServletPathMatcher;
import jakarta.servlet.*;
Expand Down Expand Up @@ -276,7 +276,7 @@ public void init(FilterConfig config) throws ServletException {

StatFilterContext.getInstance().addContextListener(statFilterContextListener);

this.contextPath = DruidWebUtils.getContextPath(config.getServletContext());
this.contextPath = DruidWebUtilsJakarta.getContextPath(config.getServletContext());
if (webAppStat == null) {
webAppStat = new WebAppStat(contextPath, this.sessionStatMaxCount);
}
Expand Down
57 changes: 1 addition & 56 deletions core/src/main/java/com/alibaba/druid/util/DruidWebUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,7 @@ public static String getRemoteAddr(HttpServletRequest request) {
return ip;
}

public static String getRemoteAddr(jakarta.servlet.http.HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip != null && ip.contains(",")) { //截取逗号前第一个ip视为源头ip
ip = ip.substring(0, ip.indexOf(",")).trim();
}
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) {
static boolean isValidAddress(String ip) {
if (ip == null) {
return false;
}
Expand Down Expand Up @@ -119,16 +86,6 @@ private static String getContextPath_2_5(ServletContext context) {
return contextPath;
}

private static String getContextPath_2_5(jakarta.servlet.ServletContext context) {
String contextPath = context.getContextPath();

if (contextPath == null || contextPath.length() == 0) {
contextPath = "/";
}

return contextPath;
}

public static String getContextPath(ServletContext context) {
if (context.getMajorVersion() == 2 && context.getMinorVersion() < 5) {
return null;
Expand All @@ -141,18 +98,6 @@ public static String getContextPath(ServletContext context) {
}
}

public static String getContextPath(jakarta.servlet.ServletContext context) {
if (context.getMajorVersion() == 2 && context.getMinorVersion() < 5) {
return null;
}

try {
return getContextPath_2_5(context);
} catch (NoSuchMethodError error) {
return null;
}
}

public static Boolean getBoolean(GenericServlet servlet, String key) {
String property = servlet.getInitParameter(key);
if ("true".equals(property)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.alibaba.druid.util;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;

public class DruidWebUtilsJakarta {
private DruidWebUtilsJakarta() {
}

public static String getRemoteAddr(final HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip != null && ip.contains(",")) { //截取逗号前第一个ip视为源头ip
ip = ip.substring(0, ip.indexOf(",")).trim();
}
if (ip != null && !DruidWebUtils.isValidAddress(ip)) {
ip = null;
}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
if (ip != null && !DruidWebUtils.isValidAddress(ip)) {
ip = null;
}
}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
if (ip != null && !DruidWebUtils.isValidAddress(ip)) {
ip = null;
}
}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (ip != null && !DruidWebUtils.isValidAddress(ip)) {
ip = null;
}
}

return ip;
}

private static String getContextPath_2_5(final ServletContext context) {
String contextPath = context.getContextPath();

if (contextPath == null || contextPath.length() == 0) {
contextPath = "/";
}

return contextPath;
}

public static String getContextPath(final ServletContext context) {
if (context.getMajorVersion() == 2 && context.getMinorVersion() < 5) {
return null;
}

try {
return getContextPath_2_5(context);
} catch (NoSuchMethodError error) {
return null;
}
}

}