9

I am trying to do spring injection to servlet filter.The filter is apart of the referenced jar files. so. I cannot change it as interceptor. In web.xml of my plugin project

<filter>
    <filter-name>CustomFilter</filter-name>    
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    
    <init-param>    
        <param-name>someinitparam</param-name>    
        <param-value>value to it</param-value>    
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CustomFilter</filter-name>
    <url-pattern>/mywebservices/*</url-pattern>
</filter-mapping>

In spring.xml I will use like this

<bean id="CustomFilter" class="com.abc.CustomFilter"></bean>

There are some filters are already configured in spring.xml as

<bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
    <property name="filterInvocationDefinitionSource">
        <value>
            CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
            PATTERN_TYPE_APACHE_ANT
            /mywebservices/*=some existing filters
        </value>              
    </property>
</bean>

As I have already specified my url pattern in web.xml do I need to add again in filterChainProxy as

/mywebservices/**=CustomFilter, some existing filters

Will it work.

Please suggest.

Tuna
  • 2,937
  • 4
  • 37
  • 61
Patan
  • 17,073
  • 36
  • 124
  • 198
  • will it work?? have you tried? – TheWhiteRabbit Feb 08 '13 at 05:06
  • Hmm.. sounds tricky. Spring dependency injection is normally done over beans created on spring context -- whereas filters are created by java ee server itself. Maybe what you can do is -- if you can be sure spring context is ready by the time your Filter class is created -- try obtain a reference to it somehow.. By the way can you tell us more about your app -- do you use Spring MVC? If so maybe you don't need to use the servlet filter – gerrytan Feb 08 '13 at 05:15
  • @TechExchange. I have tried this but it is causing serious error to my application. – Patan Feb 08 '13 at 05:17
  • @gerrytan. The filter is part of the one of the jar file. I am using Jive tool. Its based spring MVC only. – Patan Feb 08 '13 at 05:31
  • 1
    @User222 if it's causing a serious error you should say what it is. Also you shouldn't be using Acegi Security as it is no longer developed and is insecure. – Shaun the Sheep Feb 08 '13 at 13:48

2 Answers2

14

You can configure the filter like you did in your web.xml

<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>/mywebservices/*</url-pattern>
</filter-mapping>

and then inject properties in the spring.xml

<bean id="CustomFilter" class="com.abc.CustomFilter">
   <property name="someParameter">
      <value>some value</value>
   </property>
</bean>
ckoidl
  • 362
  • 5
  • 15
  • Thank you for the answer. Can we register in-it parameter of the servlet filter as property to element or do we have to specify it in element in web.xml. – Patan Feb 08 '13 at 10:02
  • And also as I have already specified the url pattern in web.xml as /mywebservices/*. Do I need to add it again in as value to /myservice/** in bean with the id filterChainProxy. Please help – Patan Feb 08 '13 at 10:15
  • FilterChainProxy is optional and is used to have fine grained control over which filters are executed. Usually you do not need to define it explicitly. If it is there for some other reason, you are doing no bad if you also add your filter there – ckoidl Feb 08 '13 at 14:55
  • No, this won't work. Other answer is right, basically you let your web context create a filter instance and then you tell Spring to create another instance, which won't be used as a filter. You could use some alternative like [this](http://stackoverflow.com/a/3847488/1199132) or [this](http://stackoverflow.com/a/7882192/1199132) to get Spring context beans. – Aritz Feb 19 '15 at 14:29
0

I think you can not inject the beans outside spring context and your servlet filter is outside the spring context. If you want a filter inside the context then I recommend go for spring web interceptors. These interceptors are within spring context and you can leverage spring container capabilities with these interceptors.