How to manage exceptions thrown in filters(common filters not only spring-security)?

18 Views Asked by At

I have a GlobalExceptionHandler decorated with @ControllerAdvice annotation but as we know filters are executed before request reaching to DispatchServlet controller advice wont work. But I am trying to find is there any way we can handle exception in filters using same GlobalExceptionHandler only

I am already aware about this article How to manage exceptions thrown in filters in Spring? but this is specific to using spring-security and I am trying to find a solution without spring-security

@Slf4j public class FilterChainExceptionHandlerFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    try {
        filterChain.doFilter(request, response);
    } catch (Exception ex) {
        log.error(ErrorConstants.FILTER_CHAIN_EXCEPTION, ex);
        request.getRequestDispatcher("/errorControllerPath").forward(request,response);
        
    }
}

}

but its only redirect to /error path not on my designated path and also if I use response.sendRedirect() it get stuck in redirection loop

0

There are 0 best solutions below