jwt 필터 두 번 실행되는 문제

Hyuk·2023년 9월 3일
0

HappyScrolls 개발기

목록 보기
3/24
post-thumbnail

(쿼리 최적화중 찾은 또다른 문제)

Hibernate: select member0_.id as id1_5_, member0_.email as email2_5_, member0_.nickname as nickname3_5_, member0_.point as point4_5_, member0_.thumbnail as thumbnai5_5_ from member member0_ where member0_.email=?
Hibernate: select member0_.id as id1_5_, member0_.email as email2_5_, member0_.nickname as nickname3_5_, member0_.point as point4_5_, member0_.thumbnail as thumbnai5_5_ from member member0_ where member0_.email=?
Hibernate: select article0_.id as id1_0_, article0_.body as body2_0_, article0_.create_date as create_d3_0_, article0_.member_id as member_i6_0_, article0_.title as title4_0_, article0_.view_count as view_cou5_0_ from article article0_ limit ?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select count(article0_.id) as col_0_0_ from article article0_

전체 쿼리이다. 메소드 하나에서 출력되는 쿼리인데, 너무 많은 쿼리가 날라가는것 같아서 개선해보고자 한다.

먼저 맨 위의 두 줄을 해결해보려 했다.

Hibernate: select member0_.id as id1_5_, member0_.email as email2_5_, member0_.nickname as nickname3_5_, member0_.point as point4_5_, member0_.thumbnail as thumbnai5_5_ from member member0_ where member0_.email=?
Hibernate: select member0_.id as id1_5_, member0_.email as email2_5_, member0_.nickname as nickname3_5_, member0_.point as point4_5_, member0_.thumbnail as thumbnai5_5_ from member member0_ where member0_.email=?

맨 위의 두 줄은 인증시에 날아가는 쿼리이고, 인증시에 이렇게 쿼리가 두번씩 나가는 이슈가 있었다.

왜 두 번 실행 되는지 궁금했다.

일단 인증 단계에서 member를 불러오는 부분은

위와 같다. 이 부분이 두 번 호출 된거 아닐까 싶다.

근데 생각해보니 매번 쿼리를 날리는게 맞긴 한건가? 고민해봐야할 것 같다.

그리고 member는 주로 email로 조회하니깐 email은 인덱스로 등록해야할 것 같다.

여기서 의심이 생기는 부분을 찾았다.

상속받은 GenericFilterBean 이라는 이름에서 뭔가.. 빈으로 등록되는 필터같은데.. 라는 생각과 동시에 빈으로 등록이 되어서 자동으로 이미 필터가 등록된거 아닐까?? 하는 생각이 들었다.

그런데 이미 나는 config 파일에서 필터를 등록해주었으니, 두 번 호출 된게 아닌가 싶었다.

검색해보니 역시나 Spring Boot는 Bean들 중에 Filter가 있으면 자동으로 Filter Chain에 등록하도록 동작한다고 한다. 그것이 문제였던 것 같다.

그렇다면 둘 중 하나를 지워줘야 하는데, 인증과 관련된 필터는 순서를 지정해줘야하므로 config파일에서 등록된 걸 놔두고, GenericFilterBean 상속 부분을 변경해야 했다.

따라서 GenericFilterBean을 OncePerRequestFilter로 바꾸어서 상속했다.

변경 전 코드는

대략 이러하고 ( 내부 내용은 바꾸지 않고 상속받은 클래스와 오버라이드 한 메소드만 바뀌었다.)

변경 후 코드는

대략 이러하다.

다시 같은 메소드를 실행해서

Hibernate: select member0_.id as id1_5_, member0_.email as email2_5_, member0_.nickname as nickname3_5_, member0_.point as point4_5_, member0_.thumbnail as thumbnai5_5_ from member member0_ where member0_.email=?
Hibernate: select article0_.id as id1_0_, article0_.body as body2_0_, article0_.create_date as create_d3_0_, article0_.member_id as member_i6_0_, article0_.title as title4_0_, article0_.view_count as view_cou5_0_ from article article0_ limit ?, ?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select member0_.id as id1_5_0_, member0_.email as email2_5_0_, member0_.nickname as nickname3_5_0_, member0_.point as point4_5_0_, member0_.thumbnail as thumbnai5_5_0_ from member member0_ where member0_.id=?
Hibernate: select count(article0_.id) as col_0_0_ from article article0_

아까와 비교해서 쿼리가 하나 줄었다 ㅋ.ㅋ

나머지는 다음 글에서 해결해볼 예정이다.

느낀 점

역시 코드는 알고 작성해야한다는 점.
또, 문제가 생겼을 때 내가 모르는 문제더라도 추론해보자!

profile
🙂 🙃 🙂 🙃

0개의 댓글