What's the point of Spring MVC's DelegatingFilterProxy?

后端 未结 7 1948
长发绾君心
长发绾君心 2020-12-07 06:56

I see this in my Spring MVC app\'s web.xml:


    springSecurityFilterCh         


        
相关标签:
7条回答
  • 2020-12-07 07:35

    You are right about 'glue' stuff. As written in JavaDocs of FilterChainProxy:

    The FilterChainProxy is linked into the servlet container filter chain by adding a standard Spring DelegatingFilterProxy declaration in the application web.xml file.

    Please see FIlterChainProxy section of blog Behind the Spring Security Namespace for an excellent explanation.

    0 讨论(0)
  • 2020-12-07 07:39

    Its been a long time but I had the same question and I found this: https://www.javacodegeeks.com/2013/11/spring-security-behind-the-scenes.html

    I tried to run my spring security project by removing the filter in question and also by adding it. What I found is if we add the filter, only then the call will redirect to required login page as defined in the spring-security configuration.

    Hence, agreeing to @Ryan's answer.

    0 讨论(0)
  • 2020-12-07 07:42

    I have been perplexed by "springSecurityFilterChain" in web.xml and found this answer in springframework security document:

    The <http> element encapsulates the security configuration for the web layer of your application. >It creates a FilterChainProxy bean named "springSecurityFilterChain" which maintains the stack of >security filters which make up the web security configuration [19]. Some core filters are always >created and others will be added to the stack depending on the attributes child elements which are >present. The positions of the standard filters are fixed (see the filter order table in the >namespace introduction), removing a common source of errors with previous versions of the framework >when users had to configure the filter chain explicitly in theFilterChainProxy bean. You can, of >course, still do this if you need full control of the configuration.

    Here is the link http://docs.spring.io/spring-security/site/docs/3.0.x/reference/appendix-namespace.html

    0 讨论(0)
  • 2020-12-07 07:46

    The thing is, servlet filters are managed by the servlet container, and not by spring. And you may need to inject some spring components into your filters.

    So, if you need something like:

    public class FooFilter {
    
        @Inject
        private FooService service;
    
        public void doFilter(....) { .. }
    
    }
    

    then you need the delegating filter proxy.

    0 讨论(0)
  • 2020-12-07 07:47

    What are Servlet Filters?

    Servlet filters are, in general, a Java WebApp concept. You can have servlet filters in any webapp, whether or not you use Spring framework in your application.

    These filters can intercept requests before they reach the target servlet. You can implement common functionality, like authorization, in servlet filters. Once implemented, you can configure the filter in your web.xml to be applied to a specific servlet, specific request url patterns or all url patterns.

    Where servlet filters are used?

    Modern web-apps can have dozens of such filters. Things like authorization, caching, ORM session management, and dependency injection are often implemented with the aid of servlet filter. All of these filters need to be registered in web.xml.

    Instantiating Servlet Filters - without Spring Framework

    Your servlet container creates instances of Filters declared in web.xml and calls them at appropriate times (i.e., when servicing servlet requests). Now if you are like most of the Dependency Injection (DI) fans, you would likely say that creation of instances is what my DI framework (Spring) does better. Can't I get my servlet filters created with Spring so they are amenable to all DI goodness?

    DelegatingFilterProxy, so that Spring creates your filter instances

    This is where DelegatingFilterProxy steps in. DelegatingFilterProxy is an impelmentation of the javax.servlet.Filter interface provided by Spring Framework. Once you configure DelegatingFilterProxy in web.xml, you can declare the actual beans that do the filtering in your spring configuration. This way, Spring creates the instances of beans that do the actual filtering, and you can use DI to configure these beans.

    Note that you need only a single DelegatingFilterProxy declaration in web.xml but you can have several filtering beans chained together in your application context.

    0 讨论(0)
  • 2020-12-07 07:48

    Do you know what a Servlet Filter is and how it works? It's a very useful piece of the Servlet Spec, allowing us to apply AOP-like concepts to servicing of HTTP requests. Many frameworks use Filter implementations for various things, and it's not uncommon to find custom implementations of them because they're very simple to write and useful. In a Spring app, most of the stuff that your app can do is in your Spring beans. A Filter instance, though, is controlled by the Servlet container. The container instantiates, initializes, and destroys it. The Servlet Spec doesn't require any kind of Spring integration, though, so you're left with a really useful concept (Filters) with no convenient way of tying it to your Spring app and the beans that do the work.

    Enter the DelegatingFilterProxy. You write a Filter implementation and make it a Spring bean, but instead of adding your own Filter class to the web.xml, you use the DelegatingFilterProxy, and give it the bean name of your filter in the Spring context. (If you don't explicitly provide a name, it uses the "filter-name".) Then at runtime, the DelegatingFilterProxy handles the complexity of finding the real implementation - the one you wrote and configured in Spring - and routing requests to it. So at runtime, it's as if you had listed your filter in the web.xml, but you get the benefit of being able to wire it like any other Spring bean.

    If you take that filter mapping out of your web.xml, everything will continue working, but none of your URLs will be secured. (That's assuming the name "springSecurityFilterChain" accurately describes what it does.) That's because this mapping is filtering every incoming request and handing it off to a security filter that's defined in your Spring context.

    0 讨论(0)
提交回复
热议问题