存cookie

HttpServletResponse res =  ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
String userId="123456";
Cookie userCookie=new Cookie("userId",userId); 
userCookie.setMaxAge(1*24*60*60);   //存活期为一天 30*24*60*60
userCookie.setPath("/");
res.addCookie(userCookie);

取cookie

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
 Cookie[] cookies= request.getCookies();
     for (Cookie cookie : cookies) {
           if(cookie != null){
            switch(cookie.getName()){
                   case "userId":
                      userId= URLDecoder.decode(cookie.getValue(),"utf-8");
                      break;
                   default:
                      break;
              }
           }
           
      }