티스토리 뷰

공부

[Spring] filter logger by MDC (get IP & UserAgent)

seunggabi 승가비 2020. 5. 30. 04:35
@Component
public class LoggerFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;

        String ip = req.getHeader(HttpHeaders.X_FORWARDED_FOR);
        ip = ip == null ? req.getRemoteAddr() : ip;
        MDC.put("ClientIP", ip);

        String userAgent = req.getHeader(HttpHeaders.USER_AGENT);
        MDC.put("UserAgent", userAgent);

        chain.doFilter(req, response);
    }
}

https://www.lesstif.com/java/spring-client-ip-18220218.html

 

Spring 에서 client ip 가져오는 법.

 

www.lesstif.com

 

'공부' 카테고리의 다른 글

[Sh] sudo -s  (0) 2020.05.30
[Python] slack chat.postMessage line break  (0) 2020.05.30
[Spring] filter logger by MDC (get IP & UserAgent)  (0) 2020.05.30
[SpringBoot] multiple filter  (0) 2020.05.30
[Git] rm *.xxx files  (0) 2020.05.30
[Sh] kill $(pgrep xxx)  (0) 2020.05.30
댓글
댓글쓰기 폼