fix: 在获取客服端真实 ip 的方法中添加获取根据 X-Forwarded-For header 来进行获取的实现

This commit is contained in:
fudali 2019-01-09 16:21:38 +08:00
parent 9ac02e638b
commit 0a229a7fcf
2 changed files with 16 additions and 1 deletions

View File

@ -49,8 +49,15 @@ public class RequestUtil {
return StringUtils.isBlank(nginxHeader) ? request.getRemoteAddr() : nginxHeader;
}
/**
* 获取 header 中的客服端应用名称
* <p>
*
* @param request {@link HttpServletRequest}
* @return 可能为 null
*/
public static String getAppName(HttpServletRequest request) {
return request.getHeader("Client-AppName");
return request.getHeader(CLIENT_APPNAME_HEADER);
}
}

View File

@ -35,4 +35,12 @@ public class RequestUtilTest {
Mockito.when(request.getHeader(eq(X_REAL_IP))).thenReturn("");
Assert.assertEquals(RequestUtil.getRemoteIp(request), "127.0.0.1");
}
@Test
public void getAppName() {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getHeader(eq(RequestUtil.CLIENT_APPNAME_HEADER))).thenReturn("test");
Assert.assertEquals(RequestUtil.getAppName(request), "test");
}
}