What is DelegatingFilterProxy in Spring Security?

This class was originally inspired by Spring Security’s   FilterToBeanProxyclass, written by Ben Alex.
Authors:
Juergen Hoeller, Sam Brannen, Chris Beams
Before we go ahead try to understand the role of DelegatingFilterProxy in Spring Security, we need to go through the basics involved in securing the web application.
When we talk about securing a web application fundamentally we are saying that response should go to requests which are authenticated, else they should not.
In a Java web application, a servlet filter plays the role of intercepting a request and performing some function or intercepting the response and acting on it before it goes back to client.
So to act on a request or response we need to define a Filter in the web.xml, the Servlet Container manages the lifecycle of this filter. But if we want to get the Spring Container  to create the instance of these Filter beans that do the actual filtering, and  use the DI to configure these beans we need the DelegatingFilterProxy.
DelegatingFilterProxy comes to the rescue in this situation,it finds the beans in the spring application context which implements the custom logic and invokes the doFilter() method of this bean as it implements the Filter interface. For example
<filter>
<filter-name>customFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>customFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You need only a single DelegatingFilterProxy declaration in web.xml but you can have more than one filtering beans chained together in your application context.
Now we can read the class’s documentation
web.xml will usually contain a  DelegatingFilterProxy definition, with the specified filter-name corresponding to a bean name in Spring’s root application context. All calls to the filter proxy will then be delegated to that bean in the Spring context, which is required to implement the standard Servlet Filter interface.


This approach is particularly useful for Filter implementation with complex setup needs, allowing to apply the full Spring bean definition machinery to Filter instances. Alternatively, consider standard Filter setup in combination with looking up service beans from the Spring root application c

Comments

Popular posts from this blog

Java Interview : Threads

Spring Framework Interview Notes : Part Two Wiring

Card Dealer In Java in Less than 5 minutes