views:

286

answers:

1

I have been asked to set up some authentication for some content on our website using JSP. What I would like to do seems simple to me but I can't quite figure out how to do it in JSP.

What I would like to do is this: When a user requests a page that you must be logged in to see, I have a tag that checks their cookies for an authentication token. If it is not there, they are redirected to a login page. After they log in, I want to redirect them back to the page they first requested along with any parameters they were sending.

Now, I have the tag that is checking their authentication and redirecting them to the login page. That part is working just fine. But I'm not sure how to maintain the first requested url and parameters so they can be redirected after they login. How might I accomplish this?

+2  A: 

Pass it as a request parameter or maybe store it in the session (and remove at end).

I am not sure how you implemented the custom tag to check the logged-in user (this seems pretty overcomplicated, just a single Filter listening on an url-pattern covering the secured pages which checks the presence of the logged-in user in the HttpSession has been sufficient), but basically you need to grab the the desired information from the HttpServletRequest which should be available to you in any way.

The HttpServletRequest#getRequestURI() returns the (relative) request URI and HttpServletRequest#getRequestURL() returns the (full) request URL to which you would like to redirect back afterwards and the HttpServletRequest#getQueryString() returns the query string (the GET request parameters, if any) for the case you'd like to include that in the redirect URL as well.

BalusC
Right, I can get the url. But when I redirect to the login page, that url is then changed to the url of the login page, not the previously requested page. How can I pass that url to the login page so I can redirect after the login? HttpServletResponse doesn't have any methods to set parameters or anything. I supposed I could append it to the end of the login url, but that sounds more like a hack than a good solution to me. Any ideas how I can store the previous url so that I can keep it after the redirect?Oh, and thanks very much for your prompt response!
Brent Parker
BalusC, I just looked into your Filter idea and I agree, that is a much better way to go. I think I'm going to change to implement that. Thank you very much! However, I'm still going to need to somehow store the requested page somehow for the redirect after logging in.
Brent Parker
Either pass it as request parameter or put it temporarily in session.
BalusC