Spring :Inserting cookies in a REST call response

后端 未结 5 1506
萌比男神i
萌比男神i 2020-12-16 17:45

I am implementing REST API endpoints using spring mvc. I am trying to send back a HTTP response with a cookie value. This is the equivalent of what I need

相关标签:
5条回答
  • 2020-12-16 18:25

    You can use Spring API for Cookie: org.springframework.http.HttpCookie:

    HttpCookie cookie = ResponseCookie.from("heroku-nav-data", nav_data)
            .path("/")
            .build();
    return ResponseEntity.ok()
            .header(HttpHeaders.SET_COOKIE, cookie.toString())
            .body(id);
    
    0 讨论(0)
  • 2020-12-16 18:28

    I finally found the solution :

    HttpHeaders headers = new HttpHeaders();
    headers.add("Set-Cookie","key="+"value");
    ResponseEntity.status(HttpStatus.OK).headers(headers).build();
    
    0 讨论(0)
  • 2020-12-16 18:39

    Hey Here is the Example of how to add cookie to response object and reading the cookie from response object using @CookieParam

    package com.ft.resources;
    import javax.ws.rs.CookieParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.core.NewCookie;
    import javax.ws.rs.core.Response;
    @Path("/cookie")
    public class CookieResource {
    
    @GET
    @Path("/write")
    public Response write() {
        //create cookie
        NewCookie c1=new NewCookie("uname","gaurav");
        NewCookie c2=new NewCookie("password","gaurav@123");
        //adding cookie to response object
        return Response.ok().cookie(c1,c2).build();
    }
    
    @GET
    @Path("/read")
    public Response read(@CookieParam("uname") String uname,@CookieParam("password") 
    String password) {
        System.out.println(uname);
        System.out.println(password);
    
        String msg="Username:"+uname;
        msg=msg.concat("</br>");
        msg=msg.concat("Password:"+password);
        return Response.ok(msg).build();
    
    }
    }
    
    0 讨论(0)
  • 2020-12-16 18:45

    While it is possible to set a cookie using a raw Set-Cookie header, it will be easier to use the Servlet API :

    Add the HttpServletResponse parameter to your controller method, Spring will pass the relevant instance; then use the addCookie method :

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<String> singleSignOn(@RequestBody String bodySso, HttpServletResponse response) {
    
        response.addCookie(new Cookie("heroku-nav-data", navData));
        return new ResponseEntity<String>(id,headers,HttpStatus.OK);    
    
    }
    

    You can also add more parameters to the cookie object if needed:

    final Cookie cookie = new Cookie(this.cookieName, principal.getSignedJWT());
    cookie.setDomain(this.cookieDomain);
    cookie.setSecure(this.sendSecureCookie);
    cookie.setHttpOnly(true);
    cookie.setMaxAge(maxAge);
    response.addCookie(cookie);
    
    0 讨论(0)
  • 2020-12-16 18:46

    Alternatively you can use

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.COOKIE, cookie-name + "=" + cookie value);
    ResponseEntity.status(HttpStatus.OK).headers(headers).build();
    
    0 讨论(0)
提交回复
热议问题