3

Lets say I have a Spring MVC form like this:

<form:form action="${pageContext.servletContext.contextPath}/secure/main.htm" commandName="secure/main">

    <form:input path="operatorId" cssClass="textField"/>

    <form:input path="clientId" cssClass="textField"/>

</form:form>

What I am trying to do is to store those fields values in the cookies that they will be saved other time user logins into the system. It is similar to Remember Me checkbox, but just without the checkbox. My controller looks like this:

@RequestMapping(method = RequestMethod.POST)
public String processAuthenticate(@Valid AuthenticationForm authenticationForm,
                                  Map<String, Object> model,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {

    authenticationForm = (AuthenticationForm) model.get("authenticationForm");

    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        System.out.println(cookie.getValue());
        if (cookie.getName().equals("clientId")) {
            authenticationForm.setClientId(cookie.getValue());
        } else if (cookie.getName().equals("operatorId")) {
            authenticationForm.setOperatorId(cookie.getValue());
        }
    }

    String clientId = authenticationForm.getClientId();
    String operatorId = authenticationForm.getOperatorId();

    Cookie cookieClientId= new Cookie("clientId", clientId);
    cookieClientId.setMaxAge(COOKIE_EXPIRY);
    response.addCookie(cookieClientId);

    Cookie cookieOperatorId = new Cookie("operatorId", operatorId);
    cookieOperatorId.setMaxAge(COOKIE_EXPIRY);
    response.addCookie(cookieOperatorId);

    return MAIN_FORM_MAPPING;
}

But when I click the button and this method is invoked, my values are not saved. It is the first time I am trying to use Cookies so maybe I am missing something? I was following this SO question. But in my case this does not work. Anybody could advice me with the solution to this problem?

Community
  • 1
  • 1
Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
  • There are security implications to the way you are storing that data in cookies - is it secure, can someone edit their cookie and see someone else's data, etc? Another way to do this is to store a (hashed ?) key in the cookie and then use that key to retrieve that data from your database. – nickdos Nov 01 '12 at 22:41
  • Good advice, thanks, I'll do that. – Paulius Matulionis Nov 01 '12 at 22:50

2 Answers2

2

Sorry for the false alarm. I was doing everything right, but I had a method which within each request returned new AuthenticationForm object as a model attribute, like this:

@ModelAttribute("secure/" + MAIN_FORM_MAPPING)
public AuthenticationForm getAuthenticationForm() {
    return new AuthenticationForm();
}

And I had a method which is used to show the form in the view:

@RequestMapping(method = {RequestMethod.GET})
public String showForm(Map<String, Object> model, HttpServletRequest request) {
    AuthenticationForm authenticationForm = (AuthenticationForm) model.get("secure/main");
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        System.out.println(cookie.getValue());
        if (cookie.getName().equals("clientId")) {
           authenticationForm.setClientId(cookie.getValue());   //Just added this code to this method
        } else if (cookie.getName().equals("operatorId")) {
           authenticationForm.setOperatorId(cookie.getValue());
        }
    }
    return MAIN_FORM_MAPPING;
}

Then I realized that this form object is always new, so the values I am setting from cookies always on a new form. I had to set the values from cookies in the showForm method and everything is working.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
0

Please check whether this is useful. In spring mvc 3, how to write a cookie while returning a ModelAndView?

@RequestMapping("/example")
private ModelAndView exampleHandler(HttpServletResponse response) {

        response.addCookie(new Cookie("COOKIENAME", "The cookie's value"));

        return new ModelAndView("viewname");
}
Community
  • 1
  • 1
Sajith
  • 2,038
  • 7
  • 27
  • 42
  • I am adding cookie directly to response, but I am returning the view mapping instead of `ModelAndView`. Does this makes any difference? I don't think so... – Paulius Matulionis Nov 01 '12 at 09:00