修正不能正确读取请求头导致无法返回正确的IP和ISO3166的问题

之前的版本所有的请求头都是强制小写,现在变大小写混合,就挺摸不着头脑的
This commit is contained in:
TheWhiteDog9487
2025-12-12 10:43:46 +08:00
parent 12db682c41
commit 94d15e04ac
@@ -9,6 +9,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -38,25 +39,21 @@ class IP {
examples = @ExampleObject(value = "78.141.226.247")), examples = @ExampleObject(value = "78.141.226.247")),
description = "成功获取到IP地址,内容为纯文本格式的IP地址") description = "成功获取到IP地址,内容为纯文本格式的IP地址")
@GetMapping("/ip") @GetMapping("/ip")
ResponseEntity<String> GetIP(@RequestHeader Map<String, String> HttpHeader, HttpServletRequest Request) { ResponseEntity<String> GetIP(@RequestHeader("CF-COnnecting-IP") String Header_CFConnectingIP,
if ( HttpHeader.get("CF-Connecting-IP".toLowerCase() ) instanceof String IP) { @RequestHeader("X-Forwarded-For") String Header_XForwardedFor,
/* HttpServletRequest Request) {
↑ 如果成功完成instanceof模式匹配,那么模式变量一定非空 if (Header_CFConnectingIP != null) {
相当于是: return new ResponseEntity<>(Header_CFConnectingIP,
if ( HttpHeader.get("CF-Connecting-IP".toLowerCase() ) != null ) { new HttpHeaders(MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8"))),
String IP = HttpHeader.get( "CF-Connecting-IP".toLowerCase() ); }
*/
return new ResponseEntity<>(IP,
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")),
HttpStatus.OK); } HttpStatus.OK); }
else if (HttpHeader.get("X-Forwarded-For".toLowerCase()) instanceof String IP) { else if (Header_XForwardedFor != null) {
IP = IP.split(",")[0].trim(); Header_XForwardedFor = Header_XForwardedFor.split(",")[0].trim();
return new ResponseEntity<>(IP, return new ResponseEntity<>(Header_XForwardedFor,
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")), new HttpHeaders(MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8"))),
HttpStatus.OK); } HttpStatus.OK); }
else { else {
return new ResponseEntity<>(Request.getRemoteAddr(), return new ResponseEntity<>(Request.getRemoteAddr(),
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")), new HttpHeaders(MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8"))),
HttpStatus.OK); } } HttpStatus.OK); } }
@Operation(summary = "获取请求客户端的ISO 3166-1 alpha-2国家代码", description = """ @Operation(summary = "获取请求客户端的ISO 3166-1 alpha-2国家代码", description = """
@@ -78,13 +75,13 @@ class IP {
examples = { @ExampleObject(value = "未找到CF-IPCountry头部") } ), examples = { @ExampleObject(value = "未找到CF-IPCountry头部") } ),
description = "未找到CF-IPCountry头部,内容为纯文本格式的错误信息") } ) description = "未找到CF-IPCountry头部,内容为纯文本格式的错误信息") } )
@GetMapping("iso3166") @GetMapping("iso3166")
ResponseEntity<String> GetISO3166(@RequestHeader Map<String, String> HttpHeader) { ResponseEntity<String> GetISO3166(@RequestHeader("CF-IPCountry") String Header_CFIPCountry) {
if (HttpHeader.get("CF-IPCountry".toLowerCase()) instanceof String CountryCode) { if (Header_CFIPCountry != null) {
return new ResponseEntity<>(CountryCode, return new ResponseEntity<>(Header_CFIPCountry,
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")), new HttpHeaders(MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8"))),
HttpStatus.OK); } HttpStatus.OK); }
else { else {
return new ResponseEntity<>("未找到CF-IPCountry头部", return new ResponseEntity<>("未找到CF-IPCountry头部",
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")), new HttpHeaders(MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8"))),
HttpStatus.NOT_FOUND); } } HttpStatus.NOT_FOUND); } }
} }