将来的にsetMaxAge()を使用してCookieを設定した場合、後続のリクエストでCookieをメモリに読み戻すと、getMaxAge()によって-1が返されます。Chromeの設定とインスペクターで実際のCookieを確認しましたが、実際に有効期限が60日後に設定されていることを確認できます。
static public void setHttpCookie(HttpServletResponse response, String payload) {
Cookie c = new Cookie(COOKIE_NAME, payload);
c.setMaxAge(60*86400); // expire sixty days in the future
c.setPath("/"); // this cookie is good everywhere on the site
response.addCookie(c);
}
static public String checkForCookie(HttpServletRequest req) {
Cookie[] cookies = req.getCookies();
if ( cookies != null ) {
for ( Cookie c : cookies ) {
if ( COOKIE_NAME.equals(c.getName()) ) {
int maxAge = c.getMaxAge();
logger.debug("Read back cookie and it had maxAge of {}.", maxAge);
String payload = c.getValue();
return payload;
}
}
}
return null;
}
c.getMaxAge()が常に-1を返すのはなぜですか?