I am currently implementing "auto login" mechanism in JSF. A filter is implemented to intercept each request, and check if a user is logged in by reading cookies.
When a user first logs in, in the managed bean, the cookie is saved in this way:
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
Cookie cookie = new Cookie("myCookieRef", value);
cookie.setPath("/");
cookie.setMaxAge(3600);
response.addCookie(cookie);
Later if the user performs a redirection, in the filter, I use the following code to retrieve the cookie:
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
for (Cookie cookie : cookies)
{
if (name.equals("myCookieRef"))
{
return cookie;
}
}
}
request is the HttpServletRequest object.
The problem is the returned cookie always has a -1 maxAge and null value.
I don't know if I miss anything when adding the cookie, or should I specify some additional attributes for the cookie?
Many thanks.