blue123 blue123 - 14 days ago 8
Java Question

how can the user be redirected to the target page after he/she login

Say we have a link to a target page (not login page) in the email sent to the user. How can the user be redirected to the target page after he/she login? Spring is in the backend.

Answer

We are using homegrown authentication

At the moment when you're checking the before continuing the request (which, in case of homegrown authentication, is usually performed in a servlet filter), you can just use HttpServletRequest#getRequestURI() to get the current request URI.

String requestedURI = request.getRequestURI();

You just have to remember this in either the session scope or as a hidden input field of the login form. Once the login succeeds, then you just have to redirect to it.

response.sendRedirect(requestedURI);