title: Add a cookie during the Spring Security login
body: <p>I have a web project with Spring Security and I have tried to save a cookie in the method that process the authentication success. However, when I look to the browser's cookies only appears the JSESSIONID one, and the same happens when I look to request.getCookies() at the servlet that Spring redirects to.</p>
  
  <p>I have tried to save the cookie in one of the application's servlets and the cookie is saved correctly, so maybe Spring Security cleans the response. Do you have any idea?</p>
  
  <p>One workaround would be to save it in Session, and then get it and save the cookie on the servlet to which the login redirects. Another one would be saving the cookie with javascript like <a href="https://stackoverflow.com/questions/20351554/set-and-read-cookies-in-spring-mvc-security">this</a>. But I don't like these solutions. Thanks in advance</p>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>public class RoleBasedAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler implements
      AuthenticationSuccessHandler {
      ...
      // save a cookie with the selected language
      Map&lt;String, String[]&gt; parameterMap = request.getParameterMap();
      if (parameterMap.containsKey("language")) {
          saveCookie("language", parameterMap.get("language")[0], response);
      }
  }
  
  public static void saveCookie(String cookieName, String value, HttpServletResponse response) {
      Cookie cookie = new Cookie(cookieName, value);
      //maxAge is one month: 30*24*60*60 
      cookie.setMaxAge(2592000);
      cookie.setDomain("projectName");
      cookie.setPath("/");
      response.addCookie(cookie);
      }
  }
  
  &lt;security:http auto-config="false" ...&gt;
      &lt;security:form-login login-page="/login.do" authentication-success-handler-ref="redirectRoleStrategy" .../&gt;
      ...
  &lt;/security:http&gt;
  
  &lt;bean id="redirectRoleStrategy" class="com.companyName.security.RoleBasedAuthenticationSuccessHandler"&gt;
      &lt;beans:property name="roleUrlMap"&gt;
          &lt;beans:map&gt;
              &lt;beans:entry key="ROLE_ADMIN" value="/privat/application.do"/&gt;
              ...
          &lt;/beans:map&gt;
      &lt;/beans:property&gt;
  &lt;/bean&gt;
  </code></pre>