🐳
Engineering Wiki
  • 🖐️Welcome
  • 📚백엔드 로드맵
    • 메인페이지
  • Spring
    • spring boot
      • security
        • security 기본
        • filter
        • JWT
      • 스프링 핵심 원리
        • 객체지향 설계와 스프링
        • 스프링IoC컨테이너와 bean
      • IntelliJ
        • Spring boot 생성 및 git clone
        • Spring boot 프로젝트 생성
      • vscode
        • Spring boot 프로젝트 생성
      • scheduling
        • 스케쥴링 설정시 에러 상황
      • paging
      • 에러 핸들링
        • ErrorCode생성 및 ExceptionHandler로 에러처리
        • Security & JWT 에러처리
        • spring cloud sleuth
      • 로그 핸들링
        • logback
        • HttpRequestServlet 래핑
      • gradle
        • hidetake.ssh 키파일 설정
      • maven
        • maven tomcat
      • lib
        • lombok
        • tiles
      • API 부하테스트 툴 K6
      • JPA
        • Mybatis / JPA 차이
      • Mybatis
    • spring batch
      • batch
        • Spring Batch 기본개념
  • FRONT
    • vue
      • Spring boot & Vue.js 설치 및 연동
      • Spring boot & Vue.js 웹개발 세팅
      • vue의 기본구조 실행순서
      • SPA 이해
  • JAVA
    • 환경설정
    • 자바의 정석
      • generics
  • DATABASE
    • mongoDB
      • 정규표현식을 사용해 대소문자 구분없이 검색
      • mongoDB export import
      • MAC 설치 및 실행
    • MYSQL
      • dbeaver 데이터 내보내기 불러오기
      • [에러] 스프링 mysql 8 연결 에러
      • MAC M1 mysql 설치
      • GROUP BY 정리
      • 테이블 명세서 빠르게 생성
  • AWS
    • IAM
    • 설치&명령어
      • eb 설치 & 명령어
      • CLI 설치 & 명령어
    • sam
      • SAM 개념
      • SAM Lambda S3이벤트 트리거, MongoDB 접근코드
      • SAM intellij 배포
    • peering
      • mongodb atlas AWS vpc peering
      • MongoDB & Lambda VPC peering ,endpoint설정
    • 쉘스크립트
      • 도커 컨테이너 중단시 슬랙 리포팅 및 재실행
  • DOCKER
    • 설치&명령어
      • Docker 기초
      • Docker Container 유용한 명령어
    • MAC관련 문제
      • 이미지 빌드 관련 문제상황
      • MAC M1 도커 실행 원리
      • [에러] docker: Error response from daemon: Mounts denied:
  • ELK
    • 세팅
      • 로드벨런서에 logstash 세팅
      • Elastic Beanstalk + Elastic Cloud + docker 설정
      • ElasticCloud + filebeat + logstash + docker 설정 (버전8.5.0)
      • ELK 적용 사례, 로그수집(filebeat/logstash) 설명
    • logstash
      • Logstash는 로그를 왜 message라는 field로 저장할까?
      • logstash health check
    • filebeat
      • filebeat 아키텍쳐
  • unity
    • 유니티 기본
      • 캐릭터 이동
      • 카메라
  • WORDPRESS
    • 워드프레스 기본
  • git
    • GIT 개념
      • 라이프사이클
    • 명령어
      • defult 브랜치 main 으로 변경
      • 첫번째 커밋 삭제(브런치삭제) 후 원격저장소에 강제 push
      • git 원격저장소에 remote 방법(vscode로 진행)
      • git gh
      • git reset
      • git rebase
  • MAC
    • 개발 환경세팅
      • 맥 초기 개발세팅
    • 유용한내용
      • app store 다운로드 없이 웹에서 Xcode 다운
      • ubuntu iso 설치 usb 만들기
      • 응용프로그램 에러
      • 잠김 파일
  • CS
    • data structure & algorism
      • 자료구조의 정의 및 종류
  • 방통대
    • 대학수학의 이해
      • 1강. 수학의 기초(1)
    • 딥러닝
      • 1강.신경망의 개요
  • NODE
    • 개발기록
      • 인스타그램 API 활용하여 게시물 슬랙에 리포팅
Powered by GitBook
On this page
  1. Spring
  2. spring boot
  3. 로그 핸들링

HttpRequestServlet 래핑

PreviouslogbackNextgradle

Last updated 2 years ago

생성일: 2022년 12월 7일 오전 11:30

  • 간단한 flow

request (요청)

-> 디스패처 서블릿

-> 컨트롤러의 객체로 값을 바인딩 하는 과정에서 바디 데이터 소비

-> 컨트롤러의 request body 비워져 있음

  • request.getReader()에서 InputStream을 생성하는데, 이걸 tomcat에서 한번만 사용할 수 있도록 막아두어서, 한번 read한 body값은 다시 읽을 수 없게 되어 있었다.

  1. HttpServletRequestWrapper 상속받아 클래스 생성 재정의

import org.apache.commons.io.IOUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

public class RereadableRequestWrapper extends HttpServletRequestWrapper {
    private ByteArrayOutputStream cachedBytes;

    public RereadableRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (cachedBytes == null)
            cacheInputStream();

        return new CachedServletInputStream(cachedBytes.toByteArray());
    }

    @Override
    public BufferedReader getReader() throws IOException{
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    private void cacheInputStream() throws IOException {
        /* Cache the inputstream in order to read it multiple times. For
         * convenience, I use apache.commons IOUtils
         */
        cachedBytes = new ByteArrayOutputStream();
        IOUtils.copy(super.getInputStream(), cachedBytes);
    }

    /* An input stream which reads the cached request body */
    private static class CachedServletInputStream extends ServletInputStream {

        private final ByteArrayInputStream buffer;

        public CachedServletInputStream(byte[] contents) {
            this.buffer = new ByteArrayInputStream(contents);
        }

        @Override
        public int read() {
            return buffer.read();
        }

        @Override
        public boolean isFinished() {
            return buffer.available() == 0;
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setReadListener(ReadListener listener) {
            throw new RuntimeException("Not implemented");
        }
    }
}
  1. OncePerRequestFilter 를 상속받은 클래스의 doFilterInternal 메서드에서 래핑

@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        RereadableRequestWrapper rereadableRequestWrapper = new RereadableRequestWrapper((HttpServletRequest)request);
				filterChain.doFilter(rereadableRequestWrapper , response);
    }
  1. AOP 에서 확인해봄

  • 래핑해주지 않으면 body 가 비어있지만, 래핑해주면 잘 뜸

@Around("com.dot3company.olobo.api.aop.LoggingAspect.onRequest()")
public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {

HttpServletRequest request = // 5
                ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        ServletInputStream inputStream = request.getInputStream();
        String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

        System.out.println("Http Body = " + messageBody);
        System.out.println("Http Body Length = " + request.getContentLength());
}
Controller에서 HttpRequest Body 값은 왜 비워져 있을까?