From 7c5269ecf94282a88fbeb722ddd2b563c8436594 Mon Sep 17 00:00:00 2001 From: TheWhiteDog9487 Date: Tue, 12 Aug 2025 17:55:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87X-Forwarded?= =?UTF-8?q?-For=E8=AF=B7=E6=B1=82=E5=A4=B4=E8=8E=B7=E5=8F=96IP=20=E4=BB=8D?= =?UTF-8?q?=E7=84=B6=E6=98=AFCF-Connecting-IP=E7=9A=84=E4=BC=98=E5=85=88?= =?UTF-8?q?=E7=BA=A7=E6=9C=80=E9=AB=98=EF=BC=8CX-Forwarded-For=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=A4=87=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/thewhitedog9487/WebAPI/Controller/IP.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/xyz/thewhitedog9487/WebAPI/Controller/IP.java b/src/main/java/xyz/thewhitedog9487/WebAPI/Controller/IP.java index 903197f..7811e34 100644 --- a/src/main/java/xyz/thewhitedog9487/WebAPI/Controller/IP.java +++ b/src/main/java/xyz/thewhitedog9487/WebAPI/Controller/IP.java @@ -29,13 +29,16 @@ class IP { log.info("请求携带了CF-Connecting-IP头部,IP为:{}", IP); return new ResponseEntity<>(IP, MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")), - HttpStatus.OK); - } + HttpStatus.OK); } 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(), MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")), - HttpStatus.OK); - } + HttpStatus.OK); } } }