feat: 获取客服端 ip 支持 X-Forwarded-For header
This commit is contained in:
parent
0987ff32a8
commit
9ac02e638b
@ -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
|
||||
* <p>
|
||||
* 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) {
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user