【发布时间】:2017-01-04 12:56:21
【问题描述】:
我跟随 this guide 读取和创建 cookie,但我只能读取从同一个子域创建的 cookie。
例如,如果我在 http://localhost:8080/x/y/test/create 创建一个 cookie,我可以从 http://localhost:8080/x/y/test/read 读取它,但我不能从 http://localhost:8080/x/y/test2/read 读取它(注意区别在 test 和 test2)
问题出在哪里?我怎么能在我的域中到处读取 cookie?
代码如下:
1 级
@Path("test")
public class Test {
@GET
@Path("/create")
@Produces(MediaType.TEXT_PLAIN)
public Response login() {
NewCookie cookie = new NewCookie("name", "123");
return Response.ok("OK").cookie(cookie).build();
}
@GET
@Path("/read")
@Produces(MediaType.TEXT_PLAIN)
public Response foo(@CookieParam("name") String value) {
System.out.println(value);
if (value == null) {
return Response.serverError().entity("ERROR").build();
} else {
return Response.ok(value).build();
}
}
}
2 级
@Path("test2")
public class Test2 {
@GET
@Path("/read")
@Produces(MediaType.TEXT_PLAIN)
public Response foo(@CookieParam("name") String value) {
System.out.println(value);
if (value == null) {
return Response.serverError().entity("ERROR").build();
} else {
return Response.ok(value).build();
}
}
}
编辑
问题出在创建时。现在我以这种方式创建 cookie:
NewCookie cookie = new NewCookie("name", "123", "/", "", "comment", 100, false);
【问题讨论】: