3

I cannot delete some cookies (2) from the request and I don't know why...

I tried this ways:

Set expiration to 0, value to blank, secure flag to false 

@GET
@Path("clear-cookies")
public Response clear-cookies(@QueryParam(NEXT) String next) throws Exception {

    Viewable page = getPage();

    // Returns form and remove cookies, setting expiration time to zero.
    return javax.ws.rs.core.Response
        .ok(page)
        .cookie(new NewCookie(NEXT, next))
        .cookie(new NewCookie(FIRST, "", null, null, null, 0, false))
        .cookie(new NewCookie(SECOND, "", null, null, null, 0, false))
        .build();
}

Set expiration to 0, value to null, secure flag to true

@GET
@Path("clear-cookies")
public Response clear-cookies(@QueryParam(NEXT) String next) throws Exception {

    Viewable page = getPage();

    // Returns form and remove cookies, setting expiration time to zero.
    return javax.ws.rs.core.Response
        .ok(page)
        .cookie(new NewCookie(NEXT, next))
        .cookie(new NewCookie(FIRST, null, null, null, null, 0, true))
        .cookie(new NewCookie(SECOND, null, null, null, null, 0, true))
        .build();
}
Community
  • 1
  • 1
Davide
  • 103
  • 3
  • 9

2 Answers2

1

I have resolved this issue by common method is describing all common parameters. At least three of parameters have to be equal: name(="name"), , path(="/") and domain(=null) :

public static NewCookie createDomainCookie(String value, int maxAgeInMinutes) {
    ZonedDateTime time = ZonedDateTime.now().plusMinutes(maxAgeInMinutes);
    Date expiry = time.toInstant().toEpochMilli();
    NewCookie newCookie = new NewCookie("name", value, "/", null, Cookie.DEFAULT_VERSION,null, maxAgeInMinutes*60, expiry, false, false);
    return newCookie;
}

And use it the common way for set:

NewCookie domainNewCookie = RsCookieHelper.createDomainCookie(token, 60);
Response res = Response.status(Response.Status.OK).cookie(domainNewCookie).build();

and for delete:

NewCookie domainNewCookie = RsCookieHelper.createDomainCookie("", 0);
Response res = Response.status(Response.Status.OK).cookie(domainNewCookie).build();
RoutesMaps.com
  • 1,628
  • 1
  • 14
  • 19
-1

The following worked for me but you would need to know the names of your cookies and delete them one by one:

@GET
@Path("clear-cookie")
@Produces(MediaType.TEXT_PLAIN)
public Response clear-cookie(@CookieParam("COOKIE_NAME") javax.ws.rs.core.Cookie cookie) {
    if (cookie != null) {
    NewCookie newCookie = new NewCookie(cookie, null, 0, false);
    return Response.ok("OK").cookie(newCookie).build();
    }
    return Response.ok("OK - No session").build();
}
kzwo
  • 1
  • 3
  • It does not delete existing cookie. Look here: https://stackoverflow.com/questions/8080239/how-to-delete-a-cookie-on-server-with-jax-rs-newcookie – RoutesMaps.com Jun 28 '18 at 11:18