0

I'm making a Java webapp which uses the Spring 4 framework.

When a user makes any request, if the user is not logged in, I use an interceptor to store this request object inside the user's session. Afterwards I redirect the user to a login screen.

When the user succesfully logs in, I want the login controller to not return a model and view (for example, the homepage) but instead fetch the stored previous request from the session and "execute" it as if the user just launched that request.

I'm having some problems with filling in some variables:

public ModelAndView login(RedirectAttributes redirectAttributes)
{
    ... //Do login stuff.

    HttpServletRequest previousRequest = (HttpServletRequest) httpServletRequest.getSession().getAttribute("previousRequest"); 
    httpServletRequest.getRequestDispatcher(previousRequest.getServletPath()).forward(previousRequest, ????????);

    //end of method, I must return a ModelAndView but I don't have any view, I just want the request to be forwarded??
    return null; //???????????
}
  1. When getting the request dispatcher, you need to give it an url. I used httpServletRequest.getServletPath() but I'm not sure if this is correct.
  2. When forwarding, you need to give a request and response object. But I don't have the response object at this time. I could perhaps also store the response object just like I did with the request. Or is there any other way? What would happen if I supply the CURRENT reponse object instead of the PREVIOUS response object?
  3. My method expects me to return a modelandview, but I don't have any. How can I tell Spring to stop doing its normal "modelandview" scheme and instead start working on the forwarding of the request?
user1884155
  • 3,616
  • 4
  • 55
  • 108
  • Surely Spring already does all this for you? – user207421 Aug 07 '16 at 01:26
  • Spring security might do this, but that's not the point. I don't use spring security and I'm making a login logic myself. I do this because I want to learn how it works (by doing it myself), not just use a library. – user1884155 Aug 08 '16 at 10:04
  • Try redirecting as described here http://stackoverflow.com/questions/9034149/httpservletresponse-sendredirect-permanent. You may return whatever you like, I guess it will be ignored if you take care of flushing response buffer (response.flushBuffer();) – Vladimiro Corsi Aug 11 '16 at 13:15
  • @VladimiroCorsi I obviously cannot do a redirect, because then a new request would enter my application and I don't want that - I want the previous request to be executed. – user1884155 Aug 11 '16 at 16:48

1 Answers1

0

The setup described above is impossible in Jboss. You cannot forward and pass an arbitrary request object, because the servlet container will display an error. The forward method is meant to forward the CURRENT request object that is begin processed by the container.

A possible strategy, although still not ideal, is to wrap the stored request together with a freshly incoming request, and override the important methods like getParameters()´ andgetAttribute()´ to use the stored request instead of the actual request. This is the way Spring security solves this.

It seems to be impossible to tell a servletcontainer to stop handling the current request and redirect or forward to an arbitrary request object you just so happen to have available in your servlet.

user1884155
  • 3,616
  • 4
  • 55
  • 108