如何在春季拦截请求被拒绝的异常?

我在Tomcat日志中看到大量条目(示例粘贴在下面)。几个月前,在次要版本升级(Spring Security 4.2.4,IIRC)之后,这些功能开始出现在我的日志文件中,因此这显然是Spring中默认启用的新安全功能。这里报告了类似的问题,但我的问题具体涉及如何在控制器中拦截这些异常。有一个Spring Security bug记录了这个问题(提供一种处理RequestedException的方法)。但是,直到Spring 5.1,他们才打算解决此问题。RequestRejectedException

我理解为什么会引发这些异常,并且我不想禁用此安全功能

我想对此功能进行一些控制,以便:

  1. 我知道我没有阻止合法用户访问我的网站。
  2. 我可以看到哪些请求触发了此事件(它们是SQL注入攻击吗?
  3. 我可以调整服务器响应。Spring Security防火墙将完整的堆栈跟踪转储到Web客户端(信息泄露),以及一个(这是非常不正确的,这应该是一个)。500 Internal Server Error400 Bad Request

我想找到一种方法来记录请求的URL,但也要专门针对这些异常抑制堆栈跟踪,因为它们会污染我的日志文件,而不会给我任何有用的信息。理想情况下,我想拦截这些异常并在我的应用程序层中处理它们,而不是在Tomcat日志中报告它们。

例如,这是每天出现在我的:catalina.out

Aug 10, 2018 2:01:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception
org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentially malicious String ";"
        at org.springframework.security.web.firewall.StrictHttpFirewall.rejectedBlacklistedUrls(StrictHttpFirewall.java:265)
        at org.springframework.security.web.firewall.StrictHttpFirewall.getFirewalledRequest(StrictHttpFirewall.java:245)
        at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:193)
        at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
        at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347)
        at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.ajp.AjpProcessor.service(AjpProcessor.java:486)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

我在两天内看到了超过3,200个这样的问题,它很快成为我的日志文件的最大贡献者,以至于它阻止了我看到其他合法的问题。从本质上讲,这个新的Spring Security功能是一种内置的拒绝服务形式,自四月份以来,它已经浪费了我几个小时的时间。我并不是说它不是一个重要的功能,只是默认的实现是完全拙劣的,我想找到一种方法来获得对它的一些控制,无论是作为开发人员还是作为系统管理员。catalina.out

我使用自定义错误控制器在Spring中拦截许多其他异常类型(包括)。但是,由于某种原因,似乎正在失败。IOExceptionRequestRejectedException

这是我的相关部分,以给出我想要完成的事情的想法:ErrorController.java

@ControllerAdvice
public final class ErrorController
{
    /**
     * Logger.
     */
    private static final Logger LOGGER = Logger.getLogger(ErrorController.class.getName());

    /**
     * Generates an Error page by intercepting exceptions generated from HttpFirewall.
     *
     * @param ex A RequestRejectedException exception.
     * @return The tile definition name for the page.
     */
    @ExceptionHandler(RequestRejectedException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String handleRequestRejectedException(final HttpServletRequest request, final RequestRejectedException ex)
    {
        if (LOGGER.isLoggable(Level.INFO))
        {
            LOGGER.log(Level.INFO, "Request Rejected", ex);
        }

        LOGGER.log(Level.WARNING, "Rejected request for [" + request.getRequestURL().toString() + "]. Reason: " + ex.getMessage());
        return "errorPage";
    }

    /**
     * Generates a Server Error page.
     *
     * @param ex An exception.
     * @return The tile definition name for the page.
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleException(final Exception ex)
    {
        if (LOGGER.isLoggable(Level.SEVERE))
        {
            LOGGER.log(Level.SEVERE, "Server Error", ex);
        }

        return "errorPage";
    }
}

此错误控制器适用于许多异常。例如,它成功地拦截了这个:IllegalStateException

Aug 05, 2018 7:50:30 AM com.mycompany.spring.controller.ErrorController handleException
SEVERE: Server Error
java.lang.IllegalStateException: Cannot create a session after the response has been committed
        at org.apache.catalina.connector.Request.doGetSession(Request.java:2999)
...

但是,这不是拦截(如上面第一个日志示例中缺少“服务器错误”所示)。RequestRejectedException

如何在错误控制器中进行拦截?RequestRejectedException


答案 1

它也可以通过一个简单的过滤器来处理,这将导致404错误响应

@Component
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
public class LogAndSuppressRequestRejectedExceptionFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(req, res);
        } catch (RequestRejectedException e) {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;

            log
                .warn(
                        "request_rejected: remote={}, user_agent={}, request_url={}",
                        request.getRemoteHost(),  
                        request.getHeader(HttpHeaders.USER_AGENT),
                        request.getRequestURL(), 
                        e
                );

            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}

答案 2

对于Spring安全版本及更高版本,您只需创建一个将要注入Spring安全过滤器链中的类型的Bean。5.4RequestRejectedHandler

import org.springframework.security.web.firewall.RequestRejectedHandler;
import org.springframework.security.web.firewall.HttpStatusRequestRejectedHandler;

@Bean
RequestRejectedHandler requestRejectedHandler() {
   // sends an error response with a configurable status code (default is 400 BAD_REQUEST)
   // we can pass a different value in the constructor
   return new HttpStatusRequestRejectedHandler();
}

推荐