@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"); }
public static String getRemoteIp(HttpServletRequest request) { 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; }
RequestUtil { public static String getRemoteIp(HttpServletRequest request) { 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; } }
RequestUtil { public static String getRemoteIp(HttpServletRequest request) { 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; }  }
RequestUtil { public static String getRemoteIp(HttpServletRequest request) { 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; }  static String getRemoteIp(HttpServletRequest request); static String getAppName(HttpServletRequest request); static User getUser(HttpServletRequest request); static String getSrcUserName(HttpServletRequest request); }
RequestUtil { public static String getRemoteIp(HttpServletRequest request) { 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; }  static String getRemoteIp(HttpServletRequest request); static String getAppName(HttpServletRequest request); static User getUser(HttpServletRequest request); static String getSrcUserName(HttpServletRequest request); static final String CLIENT_APPNAME_HEADER; static final String NACOS_USER_KEY; }