支持通过X-Forwarded-For请求头获取IP

仍然是CF-Connecting-IP的优先级最高,X-Forwarded-For作为备选
This commit is contained in:
2025-08-12 17:55:30 +08:00
parent fb426e0da7
commit 7c5269ecf9
@@ -29,13 +29,16 @@ class IP {
log.info("请求携带了CF-Connecting-IP头部,IP为:{}", IP); log.info("请求携带了CF-Connecting-IP头部,IP为:{}", IP);
return new ResponseEntity<>(IP, return new ResponseEntity<>(IP,
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")), MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")),
HttpStatus.OK); HttpStatus.OK); }
}
else { else {
log.info("请求未携带CF-Connecting-IP头部,HttpServletRequest获取到的IP为:{}", Request.getRemoteAddr()); if (HttpHeader.get("X-Forwarded-For".toLowerCase()) instanceof String IP) {
log.info("请求携带了X-Forwarded-For头部,IP为:{}", IP);
return new ResponseEntity<>(IP,
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")),
HttpStatus.OK); }
log.info("请求未携带CF-Connecting-IP和X-Forwarded-For头部,HttpServletRequest获取到的IP为:{}", Request.getRemoteAddr());
return new ResponseEntity<>(Request.getRemoteAddr(), return new ResponseEntity<>(Request.getRemoteAddr(),
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")), MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")),
HttpStatus.OK); HttpStatus.OK); }
}
} }
} }