Skip to content

Commit

Permalink
♻️ refactor(工具类): 使用List替换StringUtils简化IP获取逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Jun 4, 2024
1 parent ff8477d commit a43e324
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -68,10 +68,9 @@ public static byte[] objectToBytes(Object object) {
public static String getClientIpAddress(ServerHttpRequest httpRequest) {
HttpHeaders headers = httpRequest.getHeaders();
for (String header : IP_HEADER_CANDIDATES) {
String ipList = headers.getFirst(header);
if (ipList != null && !ipList.isEmpty() && !"unknown".equalsIgnoreCase(ipList)) {
String[] ipArray = StringUtils.commaDelimitedListToStringArray(ipList);
return ipArray[0];
List<String> ipList = headers.get(header);
if (ipList != null && !ipList.isEmpty()) {
return ipList.getFirst();
}
}
return Objects.requireNonNull(httpRequest.getRemoteAddress()).getAddress().getHostAddress();
Expand Down

0 comments on commit a43e324

Please sign in to comment.