How to intercept a RequestRejectedException in Spring?

后端 未结 7 1080
傲寒
傲寒 2020-12-14 01:06

I am seeing a ton of RequestRejectedException entries in my Tomcat log (sample pasted below). These started appearing in my log file after a minor vers

7条回答
  •  抹茶落季
    2020-12-14 01:51

    Another way to handle it is by using Spring AOP. We can create an advice around the FilterChainProxy.doFilter() method that catches any RequestRejectedException(s) thrown by the HttpFirewall and translates it into a 400 BAD_REQUEST

    @Aspect
    @Component
    public class FilterChainProxyAdvice {
    
        @Around("execution(public void org.springframework.security.web.FilterChainProxy.doFilter(..))")
        public void handleRequestRejectedException (ProceedingJoinPoint pjp) throws Throwable {
            try {
                pjp.proceed();
            } catch (RequestRejectedException exception) {
                HttpServletResponse response = (HttpServletResponse) pjp.getArgs()[1]);
                response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            }
        }
    }
    

提交回复
热议问题