I have to remove Cookies from the Response and redirect back to the same requesting URL. I recently upgraded to tomcat 9 and started using LegacyCookieProcessor to avoid invalid domain error. But for some reason i am unable to remove the cookie and redirect to same URL with subsequent call having no cookie in the request.
below is the code i am using to remove the cookie:
public static void removeCookie(String name, HttpServletRequest req, HttpServletResponse res) {
boolean isSecure = req.isSecure();
String domain = getDomain(req);
String path = "/";
String cookieName = getTicketCookiePrefix() + name;
addCookie(req, res, cookieName, "", 0, path, domain, isSecure);
Cookie[] cookies = req.getCookies();
if (cookies != null) {
Cookie[] var8 = cookies;
int var9 = cookies.length;
for(int var10 = 0; var10 < var9; ++var10) {
Cookie cookie = var8[var10];
if (cookie.getName().equals(cookieName)) {
cookie.setValue("");
}
}
}
}
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, int cookielife, String path, String domain, boolean secure) {
Cookie ck = createCookie(name, encodeCookieValue(value));
if (path != null) {
ck.setPath(path);
}
if (domain != null) {
ck.setDomain(domain);
}
ck.setMaxAge(cookielife);
ck.setSecure(secure);
response.addCookie(ck);
}
Please Let me know we need to do anything differently with legacy cookie processor to remove cookie.
Problem Statement: User-agent (IE) is unable to process (remove) cookie using 'Set-Cookie' header
Relevant diff between Tomcat 8 & 9:
Relevant diff between cookie processors: The legacy cookie parsing algorithm supported only limited global configuration via several system properties. Those system properties are still supported, but are going to be deprecated in favor of this new configuration element. ref: tomcat-8.0, tomcat-8.5
LegacyCookieProcessor
Rfc6265CookieProcessor
Combination used: Tomcat9 + LegacyCookieProcessor
Solution: Replace VM param FWD_SLASH_IS_SEPARATOR with LegacyCookieProcessor.forwardSlashIsSeparator attribute under context.xml/CookieProcessor
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" forwardSlashIsSeparator="false"/>
Ref: RFC2109 - https://www.ietf.org/rfc/rfc2109.txt RFC6265 - https://www.ietf.org/rfc/rfc6265.txt