2015-01-17 17 views
16

Sono un noobie con API RESTful e sto cercando di creare un servizio di accesso in cui fornisco un messaggio di posta elettronica e una password e, se la convalida riesce, per memorizzare un cookie. Inoltre, come posso controllare il cookie (se memorizzato)?Come impostare e controllare i cookie con JAX-RS?

Come può essere ottenuto?

@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(); 
} 

E come posso controllare il cookie (se impostato)?

risposta

27

È possibile effettuare le seguenti operazioni:

  • Per memorizzare un nuovo cookie:

    @GET 
    @Path("/login") 
    @Produces(MediaType.TEXT_PLAIN) 
    public Response login() { 
        NewCookie cookie = new NewCookie("name", "123"); 
        return Response.ok("OK").cookie(cookie).build(); 
    } 
    
  • Per recuperare il 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(); 
        } 
    } 
    

    Tuttavia, si può desidera solo il valore:

    @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(); 
        } 
    } 
    

A proposito, si consiglia di provare il seguente codice:

@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(); 
} 

Questo rimuove il cookie nel browser. Il comportamento dipende dall'implementazione di JAX-RS. Con RESTEasy (JBoss AS 7.0) e Google Chrome funziona bene.

+0

Grazie per la risposta. Quindi prima di ogni servizio che eseguo, dovrei usare: if (cookie == null) { return Response.serverError(). Entity ("ERROR"). Build(); } else { // Execute service } Come si invia il cookieparam dal client? – Gil404

+0

Dipende dal software client. Quale cliente stai usando? per esempio. JQuery, 'HttpURLConnection', ecc. –

+0

Attualmente sto testando entrambi. – Gil404

Problemi correlati