spring injection in servlet filter [duplicate]
Asked Answered
K

2

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.

Konstance answered 8/2, 2013 at 5:1 Comment(5)
will it work?? have you tried?Bitters
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 filterFacia
@TechExchange. I have tried this but it is causing serious error to my application.Konstance
@gerrytan. The filter is part of the one of the jar file. I am using Jive tool. Its based spring MVC only.Konstance
@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.Middendorf
R
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>
Refrigeration answered 8/2, 2013 at 8:6 Comment(4)
Thank you for the answer. Can we register in-it parameter of the servlet filter as property to <bean> element or do we have to specify it in <filter> element in web.xml.Konstance
And also as I have already specified the url pattern in web.xml as <url-pattern>/mywebservices/*</url-pattern>. Do I need to add it again in as value to /myservice/** in bean with the id filterChainProxy. Please helpKonstance
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 thereRefrigeration
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 or this to get Spring context beans.Overnice
P
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.

Phaedra answered 8/2, 2013 at 5:39 Comment(2)
Spring Security uses many filters defined within the Spring application context. That is what DelegatingFilterProxy is for.Middendorf
This answer is simply not correctNomenclator

© 2022 - 2024 — McMap. All rights reserved.