From 9ac02e638bc7f623ba3fc3022a2ff7535ae27bb8 Mon Sep 17 00:00:00 2001 From: fudali Date: Wed, 9 Jan 2019 16:13:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=8E=B7=E5=8F=96=E5=AE=A2=E6=9C=8D?= =?UTF-8?q?=E7=AB=AF=20ip=20=E6=94=AF=E6=8C=81=20X-Forwarded-For=20header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/server/utils/RequestUtil.java | 22 ++++++++++- .../config/server/utils/RequestUtilTest.java | 38 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 config/src/test/java/com/alibaba/nacos/config/server/utils/RequestUtilTest.java diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java index fb420c439..6255e5847 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java @@ -24,11 +24,29 @@ import javax.servlet.http.HttpServletRequest; */ public class RequestUtil { + private static final String X_REAL_IP = "X-Real-IP"; + private static final String X_FORWARDED_FOR = "X-Forwarded-For"; + private static final String X_FORWARDED_FOR_SPLIT_SYMBOL = ","; + public static final String CLIENT_APPNAME_HEADER = "Client-AppName"; + /** + * get real client ip + *

+ * first use X-Forwarded-For header https://zh.wikipedia.org/wiki/X-Forwarded-For + * next nginx X-Real-IP + * last {@link HttpServletRequest#getRemoteAddr()} + * + * @param request {@link HttpServletRequest} + * @return + */ public static String getRemoteIp(HttpServletRequest request) { - String nginxHeader = request.getHeader("X-Real-IP"); - return (nginxHeader == null) ? request.getRemoteAddr() : nginxHeader; + String xForwardedFor = request.getHeader(X_FORWARDED_FOR); + if (!StringUtils.isBlank(xForwardedFor)) { + return xForwardedFor.split(X_FORWARDED_FOR_SPLIT_SYMBOL)[0].trim(); + } + String nginxHeader = request.getHeader(X_REAL_IP); + return StringUtils.isBlank(nginxHeader) ? request.getRemoteAddr() : nginxHeader; } public static String getAppName(HttpServletRequest request) { diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/RequestUtilTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/RequestUtilTest.java new file mode 100644 index 000000000..f3e928fc3 --- /dev/null +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/RequestUtilTest.java @@ -0,0 +1,38 @@ +package com.alibaba.nacos.config.server.utils; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.servlet.http.HttpServletRequest; + +import static org.mockito.ArgumentMatchers.eq; + +public class RequestUtilTest { + + private static final String X_REAL_IP = "X-Real-IP"; + private static final String X_FORWARDED_FOR = "X-Forwarded-For"; + + @Test + public void getRemoteIp() { + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + + Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1"); + Assert.assertEquals(RequestUtil.getRemoteIp(request), "127.0.0.1"); + + Mockito.when(request.getHeader(eq(X_REAL_IP))).thenReturn("127.0.0.2"); + Assert.assertEquals(RequestUtil.getRemoteIp(request), "127.0.0.2"); + + Mockito.when(request.getHeader(eq(X_FORWARDED_FOR))).thenReturn("127.0.0.3"); + Assert.assertEquals(RequestUtil.getRemoteIp(request), "127.0.0.3"); + + Mockito.when(request.getHeader(eq(X_FORWARDED_FOR))).thenReturn("127.0.0.3, 127.0.0.4"); + Assert.assertEquals(RequestUtil.getRemoteIp(request), "127.0.0.3"); + + Mockito.when(request.getHeader(eq(X_FORWARDED_FOR))).thenReturn(""); + Assert.assertEquals(RequestUtil.getRemoteIp(request), "127.0.0.2"); + + Mockito.when(request.getHeader(eq(X_REAL_IP))).thenReturn(""); + Assert.assertEquals(RequestUtil.getRemoteIp(request), "127.0.0.1"); + } +}