Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is OncePerRequestFilter?

Tags:

spring

Documentation says org.springframework.web.filter.OncePerRequestFilter "guarantees to be just executed once per request". Under what circumstances a Filter may possibly be executed more than once per request?

like image 610
Somu Avatar asked Oct 31 '12 07:10

Somu


People also ask

What is OncePerRequestFilter in Java?

public abstract class OncePerRequestFilter extends GenericFilterBean. Filter base class that guarantees to be just executed once per request, on any servlet container. It provides a doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain) method with HttpServletRequest and HttpServletResponse arguments.

What is isAsyncDispatch?

protected boolean isAsyncDispatch(HttpServletRequest request) The dispatcher type javax. servlet. DispatcherType. ASYNC introduced in Servlet 3.0 means a filter can be invoked in more than one thread over the course of a single request.

What is a FilterChain?

A FilterChain is an object provided by the servlet container to the developer giving a view into the invocation chain of a filtered request for a resource.


1 Answers

Under what circumstances a Filter may possibly be executed more than once per request?

You could have the filter on the filter chain more than once.

The request could be dispatched to a different (or the same) servlet using the request dispatcher.


A common use-case is in Spring Security, where authentication and access control functionality is typically implemented as filters that sit in front of the main application servlets. When a request is dispatched using a request dispatcher, it has to go through the filter chain again (or possibly a different one) before it gets to the servlet that is going to deal with it. The problem is that some of the security filter actions should only be performed once for a request. Hence the need for this filter.

like image 82
Stephen C Avatar answered Sep 23 '22 16:09

Stephen C