1

I am working on a Java/J2EE & Spring based web application. We are using spring security configurations for login. I have a requirement and not getting how to implement it. Requirement: Whenever anonymous users clicks on 'View Address Book' link on home page they should be redirect to login/signup page. and the header of login/signup page should be set to "Please login/signup to view your address book".

Now, here I am not getting based on what parameter I should change login/signup page heading.

Please suggest. Reference to specific files/steps will be appreciated.

Regards, Arun

Arun
  • 141
  • 2
  • 4
  • 11

1 Answers1

5

You can use SavedRequest interface. Spring security use SavedRequest type to store the redirect url and parameters.

  1. Create a custom login controller to serve your login.jsp view.
  2. Get SavedRequest object from HttpSessionRequestCache
  3. Get the requested url and parameters savedRequest.
  4. Pass it to login.jsp page as model attribute.

    @Controller
    public class LoginController {
        @RequestMapping("/login")
        public ModelAndView showLoginPage(HttpServletRequest request, HttpServletResponse response) {
    
            SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
    
            String requestedUrl  = savedRequest.getRedirectUrl();
            ModelAndView model =  new ModelAndView("login");
            model.addObject("requestedUrl",requestedUrl);
            return model;
        }
    }
    

    You can also get request parameters by

    savedRequest.getParameterValues("parameterName");
    
mirmdasif
  • 6,014
  • 2
  • 22
  • 28
  • Hi sadasidha, httpSessionRequestCache was already there in my controller. I was not aware of its usage. Thanks, for your input. It resolved my problem up to some extent. Actually, now, once I click on 'View Address Book' link it showing right message on 'login/signup' page. But then if I click on 'Login' link then also saved request returning saved parameters and jsp displaying the address book message again. Do you have a good way to handle this scenario? – Arun May 13 '15 at 19:26
  • @Arun - If the request came for login page you can make the decession to show and /or remove the parameters from savedRequest. Also consider accepting my answer if it helps. – mirmdasif May 14 '15 at 07:07
  • I am able to fix it after providing a check for 'referer'. Thanks! Not seeing any option to accept. Already voted. – Arun May 14 '15 at 12:53