日志中添加客户端IP的配置方法

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;
public class LogIPFilter extends OncePerRequestFilter {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        
        String ip = request.getHeader("X-Forwarded-For");;
        String path=request.getServletPath()+request.getPathInfo();
        log.info("IP:["+ip+"],url:["+path+"]");
        filterChain.doFilter(request, response);
    }
}

你可能感兴趣的:(日志中添加客户端IP的配置方法)