Spring MVC의 기능을 확장시켜 직접 JWT 검증 방식을 구현하게 된 이유와 결과에 대해 기술합니다.


<aside> 💡 목차

</aside>

<Why?> 확장의 이유


프로젝트를 개선하고자 구조를 변경하던 중, 팀원에게 서드파티 라이브러리 사용으로 인해 코드 이해 비용이 크다는 피드백을 받았습니다.

그럼 어떻게 이해비용이 큰다 말했는지 이유를 살펴보겠습니다.

1. 회원 전용 API 인지 알기 어려워요


    @PostMapping("/{memberId}")
    public ResponseEntity<CommonResult> blocking(
            @PathVariable Long memberId,
            @RequestBody Map<String, String> roomId
            ) throws Exception {
        blockBusiness.block(memberId, Long.valueOf(roomId.get("roomId")));
        return ResponseEntity.ok(
                CommonResult.of(
                        "차단 완료"
                )
        );
    }
    /**
     * 현재 로그인 회원 엔티티 조회
     */
    public MemberEntity getCurrentMemberEntity() throws Exception {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String MemberEmail = userDetails.getUsername();
        return findByEmail(MemberEmail);
    }

위 코드를 보시면 해당 API가 (토큰 인증이 필요한)회원 전용인지 이해하는데 어렵습니다.

더불어 Authentication 이라는 객체가 메모리에 적재되어 사용하게되는데, 이를 위해 다시 DB에 접근하게되며, Service 객체의 책임이 아닌지라 Contorller 객체에게 Authentication 사용에 대한 책임이 부여됩니다.

2. Spring Security ? 깊숙이 이해하고 써야 해요!