mirror of
https://github.com/TheWhiteDog9487/WebAPI.git
synced 2026-08-11 23:01:30 +08:00
抽离记录日志的逻辑到单独一个过滤器内
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package xyz.thewhitedog9487.WebAPI.Controller.Filter;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class LogClientInfo extends OncePerRequestFilter {
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
log.info("接收到对于{} {} 的请求", request.getMethod(), request.getRequestURL() + ( ( request.getQueryString() == null ) ? "" : "?" + request.getQueryString() ) );
|
||||
log.info("请求ID:{}", request.getRequestId());
|
||||
if (request.getHeader("CF-Connecting-IP".toLowerCase()) instanceof String IP) {
|
||||
log.info("请求携带了CF-Connecting-IP头部,IP为:{}", IP); }
|
||||
else if(request.getHeader("X-Forwarded-For".toLowerCase()) instanceof String IP) {
|
||||
IP = IP.split(",")[0].trim();
|
||||
log.info("请求携带了X-Forwarded-For头部,IP为:{}", IP); }
|
||||
else {
|
||||
log.info("请求未携带CF-Connecting-IP和X-Forwarded-For头部,HttpServletRequest获取到的IP为:{}", request.getRemoteAddr()); }
|
||||
filterChain.doFilter(request, response); } }
|
||||
@@ -20,24 +20,21 @@ class IP {
|
||||
ResponseEntity<String> GetIP(@RequestHeader Map<String, String> HttpHeader, HttpServletRequest Request) {
|
||||
if (HttpHeader.get("CF-Connecting-IP".toLowerCase()) instanceof String IP) {
|
||||
/*
|
||||
↑ instanceof的模式变量一定非空
|
||||
↑ instanceof的如果成功完成模式匹配,那么模式变量一定非空
|
||||
相当于是:
|
||||
if (HttpHeader.get("CF-Connecting-IP" != null) {
|
||||
String IP = HttpHeader.get("CF-Connecting-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); }
|
||||
else if (HttpHeader.get("X-Forwarded-For".toLowerCase()) instanceof String IP) {
|
||||
IP = IP.split(",")[0].trim();
|
||||
return new ResponseEntity<>(IP,
|
||||
MultiValueMap.fromSingleValue(Map.of("Content-Type", "text/plain;charset=UTF-8")),
|
||||
HttpStatus.OK); }
|
||||
else {
|
||||
if (HttpHeader.get("X-Forwarded-For".toLowerCase()) instanceof String IP) {
|
||||
IP = IP.split(",")[0].trim();
|
||||
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); }
|
||||
|
||||
@@ -10,7 +10,6 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MissingRequestHeaderException;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -61,10 +60,6 @@ class Message {
|
||||
@ExceptionHandler(MissingRequestHeaderException.class)
|
||||
ResponseEntity<ResponseData> HandleMissingHeader(MissingRequestHeaderException Exception, HttpServletRequest Request) {
|
||||
log.warn("请求缺少必要的头部信息:{}", Exception.getHeaderName());
|
||||
if (Request.getHeader("CF-Connecting-IP") instanceof String IP) {
|
||||
log.warn("请求携带了CF-Connecting-IP头部,IP:{}", IP);
|
||||
} else {
|
||||
log.warn("请求未携带CF-Connecting-IP头部,IP:{}", Request.getRemoteAddr()); }
|
||||
return new ResponseEntity<>(new ResponseData(
|
||||
HttpStatus.BAD_REQUEST.value(),
|
||||
"请求缺少必要的头部信息",
|
||||
|
||||
Reference in New Issue
Block a user