3

我无法从请求中删除一些 cookie (2),我不知道为什么...

我试过这种方法:

将过期时间设置为 0,将值设置为空白,将安全标志设置为 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();
}

将过期时间设置为 0,将值设置为 null,将安全标志设置为 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();
}
4

2 回答 2

1

我已经通过常用方法解决了这个问题,即描述所有常用参数。至少三个参数必须相等: name(="name"), , path(="/") 和 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;
}

并将其用于设置的常用方式:

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

并删除:

NewCookie domainNewCookie = RsCookieHelper.createDomainCookie("", 0);
Response res = Response.status(Response.Status.OK).cookie(domainNewCookie).build();
于 2018-06-28T20:36:15.863 回答
-1

以下内容对我有用,但您需要知道 cookie 的名称并一一删除:

@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();
}
于 2016-10-01T13:30:22.197 回答