Hi,
This is what i am doing in one of my applications to intercept requests. Check this out.
Servlet code
A user token is placed in the session after successful authentication in the servlet. It is removed when the session expires or when the user logs out.
The login page should call the servlet for authetication with an identifiable query. In this example, the link in login.jsp's form would probably look like "http://domain/servlet/auth?cLogin". (see below).
We check for the token first and if it is valid, we don't care about the query
string. This setup helps avoid circular calls (especially since we do a forward as opposed to a redirect).
Authentication is valid only when a user token exists in the session. But we let the request proceed if the user is logging in.
If authentication fails and we have a valid configuration, we simply forward the request to the login page again. Otherwise, we throw an exception (we should really never get here ...). The default error page for the application would likely pick that up.
Web.xml
Code snippet for Login Page
Code snippet for Servlet:
- Obviously would be called from a doPost() method that's already extracted the parameters from the request if one of the parameters is "cLogin".
That's it. After you deploy a web application with this filter, all requests will be intercepted and authentication checked.