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
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 singleSignOn(@RequestBody String bodySso, HttpServletResponse response) {
response.addCookie(new Cookie("heroku-nav-data", navData));
return new ResponseEntity(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);