PDA

View Full Version : @Resource from servlet filter



bozzo@celi.it
Sep 30th, 2009, 05:07 AM
I've got a servlet filter that allow accessing a spring controller only for a couple ip. I defined it in my web.xml:

<filter>
<filter-name>IpAddressFilter</filter-name>
<filter-class>it.celi.security.IpAddressFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>IpAddressFilter</filter-name>
<url-pattern>/systemInfo.do</url-pattern>
</filter-mapping>

Inside of it there is a List<String> ipWhiteList.
Now, in my spring's application-context.xml i define:

<util:list id="ipWhiteList">
<value>192.168.1.237</value>
<value>127.0.0.1</value>
</util:list>
How can I autowire it?
I tought something similar to @Resource(name ="ipWhiteList") :)
(obviously it isn't so easy!) :(

Note that it isn't a real exigence, I only want to know for future use: autowiring a bean in the context to a servlet out of it. Possible? ;)

Marten Deinum
Sep 30th, 2009, 07:59 AM
This isn't possible, spring can only (auto)wire/configure beans it knows about. Obviously your filter is not part of the spring configuration or even under the control of spring it is under the control of the servlet container.

You can use a DelegatingFilterProxy and configure your actual filter inside your application context. However if it is for limiting access to a controller it might be better to use a HandlerInterceptor instead.

bozzo@celi.it
Sep 30th, 2009, 10:11 AM
Could you please tell me with more details how can I add a handlerInterceptor to my configuration? Can I leave the filter as it is? :o

bozzo@celi.it
Sep 30th, 2009, 10:55 AM
Better...
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>it.celi.security.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>ipAddressFilter</param-value>
</init-param>
</filter>
It works.
But I don't want ALL urls are filtered, only a controller.

Marten Deinum
Sep 30th, 2009, 01:03 PM
That is why I suggested a HandlerInterceptor.... I suggest the reference guide...

bozzo@celi.it
Oct 1st, 2009, 03:29 AM
I read the reference guide at http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html#mvc-handlermapping but I don't understand one thing, it isn't explained how to put the interceptor BEFORE the controller, I mean


<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="/systemCheck.do">systemCheckController</prop>
<prop key="/systemInfo.do">systemInfoController</prop>

</props>
</property>
</bean>

This is what I have, then I implement an IpInterceptor, how can I put it BEFORE the systemInfo.do and NOT BEFORE systemCheck.do? The example in the reference guide does not explain that :rolleyes:

Marten Deinum
Oct 1st, 2009, 04:12 AM
define multiple HandlerMappings. One with and one without the interceptor.

bozzo@celi.it
Oct 1st, 2009, 05:36 AM
Ok, finally... it works! :) Thanks for your help!
Now I'm using two SimpleUrlHandlerMapping with 'order' property 1 for the interceptor-based and 2 for other mappings.
Thank you again!