Monday, June 08, 2009

Fetching Servlet Filter init parameters from a properties file with Spring

A while ago I wrote some code and thought it a neat trick but totally forgot about it until recently. I am writing this blog entry, firstly to share the idea and secondly to see if somebody can give an alternative using only Spring classes (which would deepen my understanding). Using Spring's DelegatingFilterProxy it is possible to hand off creation of servlet filters to a bean defined in Spring's application context. This is very useful if you want to inject Spring managed beans into a your servlet filters.

I am using the DelegatingFilterProxy mechanism to configure my servlet filter init parameters and in conjunction using PreferencesPlaceholderConfigurer so these come from a properties file. This means I can easily switch between prod and dev environments without having to modify web.xml (I could even change the authenticationFilter implementation to something else but that is a different story!). My aim here is to minimize the number of files I need to edit when moving between prod and dev environments. I hope somebody else finds this approach useful.

As an example I am using the Yale CAS Filter, which is considered a little long in the tooth now (but I'm still using it). It makes for a good example because there are numerous filter properties that you might want to change. Even the most up to date version of the JA-SIG CAS Filter could potentially benefit from using my approach.

web.xml


...
<filter>
<filter-name>authenticationFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>authenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...

applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location" value="/WEB-INF/dev.properties" />
</bean>

<bean id="authenticationFilter" class="edu.bristol.web.ParameterPassingFilterProxy">
<property name="parameters">
<map>
<entry key="edu.yale.its.tp.cas.client.filter.loginUrl" value="${cas.loginUrl}" />
<entry key="edu.yale.its.tp.cas.client.filter.validateUrl" value="${cas.validateUrl}" />
<entry key="edu.yale.its.tp.cas.client.filter.wrapRequest" value="${cas.wrapRequest}" />
<entry key="edu.yale.its.tp.cas.client.filter.serverName" value="${cas.serverName}" />
<entry key="edu.yale.its.tp.cas.client.filter.renew" value="${cas.renew}" />
</map>
</property>
<property name="targetBeanName" value="casFilter" />
</bean>

<bean id="casFilter" class="edu.yale.its.tp.cas.client.filter.CASFilter"/>

</beans>

The extra class that I added to make this work is available from ParameterPassingFilterProxy.java you are free to use it as you wish.

19 comments:

ismjml said...

Thanks for sharing this. I have the very same problem and might have eventually found the solution on my own but this lays out what I need clearly.
Note: Comment imported. Original by Ken at 2010-03-01 21:17

Unknown said...

Wow I've been searching for this functionality for days. This should be integrated into spring itself!

Unknown said...

Argh! The .java file link is dead, would you be willing to re-share the code?

Unknown said...

Please share the Java file. this link is broken. Please send the Java file on hitender.ar@gmail.com

Unknown said...

Jacket Leather Indonesia
Civet Coffee Indonesia
http://bantalsilikon01.blogspot.com/

cd
anne jelita brenda anne caca tasya ruth
bantal web jual bantal bantal bantal silikon pembicara hellow bumbu bumbu bumbu bumbu bumbu distro distro

marinirseo
distro
sabunherbal
keset
bantal
seo
distro
jual
marinir
bumbu

sheela rajesh said...

I have to appreciate you for your great work which you had done in your blog.i want you to add more like this.
JAVA Training in Chennai
JAVA Course in Chennai
Digital Marketing Course in Chennai
Python Training in Chennai
Big data training in chennai
Selenium Training in Chennai
JAVA Training in Chennai
JAVA Course in Chennai

Anbarasan14 said...

Awesome post, this post has really got some valid useful content with it. Great job; Do share more post regularly.

Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai

ravi said...

Thank you so much for sharing this. Very useful for me.

chennaizigzagcars

for ict 99 said...

The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Final Year Project Centers in Chennai.

Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

madin said...

High quality from a trust worthy replica raybans sunglasses here can offer you the best in high quality, including fake raybans clubmaster , etc.

Tuhin said...

keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore

madin said...

Paris Fashion Week would Replica handbags uk have been incomplete without replica Yves Saint Laurent bags Anthony Vaccarello’s homage to maison founder Yves Saint Laurent in the Saint Laurent spring/summer 2019 collection.

madin said...

La performance d'Alexander McQueen est connue pour sa puissance émotionnelle, pas cher alexander mcqueen son pas cher enfant alexander mcqueenénergie primitive et sa nature contemporaine romantique mais déterminée. Un élément essentiel de la culture de McQueen est la juxtaposition d'éléments contrastés: pas cher enfant alexander mcqueen vulnérabilité et force, tradition et modernité, fluidité et sérieux. En respectant la tradition de l'artisanat, une vision ouverte et même passionnée s'est réalisée. La collection McQueen combine une compréhension approfondie de la couture personnalisée britannique, un superbe savoir-faire et un savoir-faire impeccable fabriqué en Italie.

madin said...

Buy a discounted IWC watch from this Time and get free shipping. Large selection in stock. replica watches UKIWC watches, is an iconic brand. The IWC Portuguese, replica IWC watches sale a collection that includes the IWC Portuguese automatic and IWC Yacht Club chronograph is legendary. IWC Schaffhausen is also known for the IWC Aquatimer, Top Gun, Big Pilot watch, Mark XVII, Pilot's Watch, Spitfire chronograph and their Big Pilot Watch, the St-Exupery.

madin said...

OMEGA has been a world leader in advanced watch design since 1848. Over the years, cheap replica watchesthe brand has been widely celebrated for their durability and precision. replica omega watches has served as the official timekeepers of the Olympic Games for nearly a century. They also created the official watch of the space program and first watch to land on the moon. Buying an OMEGA watch is an excellent way to invest in a piece of history.

madin said...

Montblanc MeisterstückClassique is not surprisingly regarded as one replica mont blanc ballpoint pen of Montblanc's top ten fountain pens, replica great characters edition and perhaps the most iconic of the Montblanc writing series. This pen is coated with precious black resin and has a bright white star badge. It is a must-have for collectors and has a well-known name, with German as a "masterpiece". Each pen has a separate serial number, which means that no two pens are the same. Similarly, each nib of Montblanc is individually stamped after manual testing by Montblanc's master craftsmen.

madin said...

Created in 1993, Christian Louboutin’s signature red-bottom heels remain the replica louboutin shoes fashion world’s most stylish stilettos. Level up your shoe collection for less with unbeatable Christian Louboutin sales, replica women louboutin evening where you’ll find alluring Louboutin heels and unique Louboutin shoes in your perfect size.

madin said...

replica cartier watches is the renowned French jeweller and watchmaker with a history of pioneering design and a drive for excellence. The first references to Cartier wristwatches date back to 1888.discounted replica watches Late in the 19th century it was pocket and chatelaine watches that were most popular, but Louis Cartier saw a future in timepieces that could be worn on the wrist.

madin said...

Today it may be one of the biggest and most profitable luxury brands on the planet, Replica handbags a byword for indulgence from Beijing to Bond Street, replica Louis Vuiton bags but Louis Vuitton's origins are more humble, dating back to a young man who left home to make his living packing luggage for the great and the good in 19th-century Paris.