728x90
이번주 한일
시큐리티에서 Exception Handling 해주기
@RequiredArgsConstructor
@Slf4j
@Component
public class ExceptionHandlingFilter extends OncePerRequestFilter {
private final ObjectMapper objectMapper;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
try {
filterChain.doFilter(request, response);
} catch (CustomException e) {
log.error(e.getMessage());
response.setStatus(e.getStatusCode().value());
response.setContentType(APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
response.getWriter().print(
objectMapper.writeValueAsString(ApiResponse.of(e.getStatusCode(),e.getMessage())));
}
}
}
@Configuration
@EnableMethodSecurity(securedEnabled = true)
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final ExceptionHandlingFilter exceptionHandlingFilter;
private final JwtAuthenticationFilter authenticationFilter;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
...
.addFilterBefore(this.authenticationFilter, UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(this.exceptionHandlingFilter, JwtAuthenticationFilter.class);
return http.build();
}
...}
}
filter는 다음 필터로 넘어갈 때 doFilter로 다음 필터를 타고타고 가기 때문에 앞쪽에 필터를 두고 try catch문으로 exception을 처리해주도록 했다.
다만 이름은 filter인데 filter에서 handling을 한다는 점이 뭔가 어색했다.
양방향 설정없는 1:다 연관관계에서 다 객체 들고오기
강의 서비스를 프로젝트에서 강의의 댓글 기능을 구현했다.
실무에서는 양방향 연관관계를 거의 사용하지 않는다하여 최대한 양방향 관계 없이 진행하려 했다.
강의를 조회할 때 댓글을 가져오려다 보니 대댓글도 있는 상황에서 어떻게 댓글을 들고올지 고민하였다.
결국 구현하고자 한 내용은
1. 게시글에 루트 댓글을 페이징 처리해서 들고온다.
2. 각 루트 댓글의 대댓글 게수를 함께 조회하도록 하자.
관련글
728x90