So here is how I added a really simple filter to my Spring application.
In my web.xml I added the following filter definition, alongside the other standard security and encoding ones:
<filter> <filter-name>expiredPasswordFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetBeanName</param-name> <param-value>expiredPasswordFilterBean</param-value> </init-param> </filter>
The org.springframework.web.filter.DelegatingFilterProxy is the relevant filter type that allows you to then declare a targetBeanName value... which corresponds to a declaration we add in our applicationContext.xml, as so:
<bean name="expiredPasswordFilterBean" class="com.companyname.webapp.filter.ExpiredPasswordFilter"/>
With that connection set up, we now head back to the web.xml to define the mapping for our filter... separately from the above filter definition. In my case, this was enough:
<filter-mapping> <filter-name>expiredPasswordFilter</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping>
the filter name connects to the previous web.xml definition, and the url-pattern in my case was for the filter to be run on every request for a HTML path.
With that all in place, all I needed to do was declare the actual filter control class. This is defined in the applicationContext above as being at com.companyname.webapp.filter.ExpiredPasswordFilter, and the class looks a little like this:
com.companyname.webapp.filter.ExpiredPasswordFilter; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.filter.OncePerRequestFilter; public class ExpiredPasswordFilter extends OncePerRequestFilter { protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException, IOException { /* * This is where I did my user flag checking */ chain.doFilter(req, res); } }
The actual logic of the filter can be almost anything you want! But just make sure you call chain.doFilter(req, res); to invoke the next filter in the chain. This can be at any point throughout your code, and control is passed straight back to your filter once the chain has been executed, which can be useful for ensuring other security/authentication filters have been run before you do anything additional yourself.
This comment has been removed by the author.
ReplyDeleteI just ate a mountain of frustration with Spring, I am tired and I am frazzled. Reading your short and well written post I found a good workaround to get what I needed off the ground. Thank you so much for posting.
ReplyDeleteSteve
Same for me!
DeleteI was unable to get the primefaces fileupload filter work propperly... In fact, it wasn't called at all!
Now I just created a bean that extends the org.primefaces.webapp.filter.FileUploadFilter and use that filter instead ^^
This post saved my day! Thanks a lot!
Thank you guys, it's always great to hear my ramblings are proving useful! Cheers for taking the time to leave your comments, and all the best with your projects in the future!!
DeleteSimon
No worries Steve, I'm glad my post could help.
ReplyDeleteThank you for posting.
It's really Short and sweet description.
ReplyDeleteThanks for this blog.
You're welcome. I'm glad you found it useful, and thank you for your comment.
DeleteThis is just wonderful. I needed to redirect a user when session timeouts and your code just helped me.
ReplyDeleteThanks!
Fantastic Rishab, glad it was of help.
DeleteThank you for your comment.
Do not forget to add the following listener
ReplyDeleteorg.springframework.web.context.ContextLoaderListener
If missed you get this exception :
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
Thanks For Your valuable posting, it was very informative.
ReplyDelete"all I needed to do was declare the actual filter control class. This is defined in the applicationContext above as being at com.companyname.webapp.filter.ExpiredPasswordFilter",
ReplyDeleteAre you defining the ExpiredPasswordFilter bean in the
Dispatcher Servlet Application Context ("Web Layer Context", used for Controllers, Views, Resolvers used by Spring MVC)
or
in the "Root" Application context?
Thank you for the very nice post.
I can't think in any cases I would want use the traditional filter instead of the spring interceptor.
ReplyDeletebut yeh, thanks for the tutorial it was useful anyway
ReplyDeleteGreat article.. It worked for me..
ReplyDeleteThanks for sharing such a useful tutorial.. :)
The information you wish to give through this article is very informative. I have been waiting to read such wonderful piece of articles but was never lucky, but today I finally did become lucky because your article is just fabulous.
ReplyDeletehttp://www.redsqware.com/
It's really a short and beautiful description
ReplyDeleteThank you so much for posting.
Incredible points. Sound arguments. Keep up the great work.
ReplyDeleteAmazing Article! Your informations like a charm. I have read 3 books on Android app development , nowhere I can find these instructions. Lots of thanks for this excellent tutorial. You saved my time hours. THANKS. Do you know about any Top mobile App Development companies in Dubai?
ReplyDeleteArka Softwares offers high quality custom healthcare app development services to help enterprises achieve regulatory compliance. Get complete guide on healthcare app development and learn how to make a health app people will use.
ReplyDeleteDownload the SAP C_TS460_1709 Q&A PDF file easily to prepare SAP Certified Application Associate – SAP S/4HANA Sales Upskilling exam. It is particularly designed for SAP C_TS460_1709 exam and our SAP specialists have created this C_TS460_1709 Question Dumps observing the original C_TS460_1709 exam.
ReplyDelete"Great informatiom explained inshort. Thanks for posting.
ReplyDeleteCustom Application Development Services"