Java 如何使用 JAX-RS 设置和检查 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28004298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:26:32  来源:igfitidea点击:

How to set and check cookies wih JAX-RS?

javarestcookiesjax-rs

提问by Gil404

I am a noobie with RESTful API and I am trying to build a Login service in which I provide an email and password and if the validation is successful - to store a cookie. In addition, how do I check the cookie(if stored)?

我是 RESTful API 的菜鸟,我正在尝试构建一个登录服务,我在其中提供电子邮件和密码,如果验证成功 - 存储 cookie。另外,如何检查cookie(如果已存储)?

How can this be achieved?

如何做到这一点?

@Path("/login")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
public Response Login(final String i_LoginDetails) throws JSONException {
    final JSONObject obj = new JSONObject(i_LoginDetails);
    try {
        if (isValidUser(obj.getString("email"), obj.getString("password"))) {
            // Set a cookie
        } else {
            // return error invalid-credentials message
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Response.ok("TEST").build();
}

And how do I check the cookie(if set)?

我如何检查 cookie(如果设置)?

回答by Paul Vargas

You can do the following:

您可以执行以下操作:

  • To store a new cookie:

    @GET
    @Path("/login")
    @Produces(MediaType.TEXT_PLAIN)
    public Response login() {
        NewCookie cookie = new NewCookie("name", "123");
        return Response.ok("OK").cookie(cookie).build();
    }
    
  • To retrieve the cookie (javax.ws.rs.core.Cookie):

    @GET
    @Path("/foo")
    @Produces(MediaType.TEXT_PLAIN)
    public Response foo(@CookieParam("name") Cookie cookie) {
        if (cookie == null) {
            return Response.serverError().entity("ERROR").build();
        } else {
            return Response.ok(cookie.getValue()).build();
        }
    }
    

    However, you may only want the value:

    @GET
    @Path("/foo")
    @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:

    @GET
    @Path("/login")
    @Produces(MediaType.TEXT_PLAIN)
    public Response login() {
        NewCookie cookie = new NewCookie("name", "123");
        return Response.ok("OK").cookie(cookie).build();
    }
    
  • 要检索 cookie ( javax.ws.rs.core.Cookie):

    @GET
    @Path("/foo")
    @Produces(MediaType.TEXT_PLAIN)
    public Response foo(@CookieParam("name") Cookie cookie) {
        if (cookie == null) {
            return Response.serverError().entity("ERROR").build();
        } else {
            return Response.ok(cookie.getValue()).build();
        }
    }
    

    但是,您可能只需要以下值:

    @GET
    @Path("/foo")
    @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();
        }
    }
    


By the way, you may want to try the following code:

顺便说一句,您可能想尝试以下代码:

@GET
@Path("/logout")
@Produces(MediaType.TEXT_PLAIN)
public Response logout(@CookieParam("name") 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();
}

This removes the cookie in the browser. The behavior depends on the implementation of JAX-RS. With RESTEasy (JBoss AS 7.0) and Google Chrome works fine.

这将删除浏览器中的 cookie。该行为取决于 JAX-RS 的实现。使用 RESTEasy (JBoss AS 7.0) 和 Google Chrome 可以正常工作。