Commit 1fe24756 by 张乐 Committed by GitHub

Merge pull request #387 from nobodyiam/forwarded-for-list

adapt for list result of X-FORWARDED-FOR
parents 91065c0d 714df8dd
package com.ctrip.framework.apollo.configservice.controller; package com.ctrip.framework.apollo.configservice.controller;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
...@@ -40,6 +41,8 @@ import javax.servlet.http.HttpServletResponse; ...@@ -40,6 +41,8 @@ import javax.servlet.http.HttpServletResponse;
@RestController @RestController
@RequestMapping("/configs") @RequestMapping("/configs")
public class ConfigController { public class ConfigController {
private static final Splitter X_FORWARDED_FOR_SPLITTER = Splitter.on(",").omitEmptyStrings()
.trimResults();
@Autowired @Autowired
private ReleaseService releaseService; private ReleaseService releaseService;
@Autowired @Autowired
...@@ -220,11 +223,11 @@ public class ConfigController { ...@@ -220,11 +223,11 @@ public class ConfigController {
} }
private String tryToGetClientIp(HttpServletRequest request) { private String tryToGetClientIp(HttpServletRequest request) {
String ipAddress = request.getHeader("X-FORWARDED-FOR"); String forwardedFor = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) { if (!Strings.isNullOrEmpty(forwardedFor)) {
ipAddress = request.getRemoteAddr(); return X_FORWARDED_FOR_SPLITTER.splitToList(forwardedFor).get(0);
} }
return ipAddress; return request.getRemoteAddr();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment