问题:
spring HandlerInterceptor postHandle 中 setCookie 没作用
MVC @Controller @ResponseBody
SetDeviceIdInterceptor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package demo.spuuid; import java.time.LocalDate; import java.util.Arrays; import java.util.Date; import java.util.Objects; import java.util.Optional; import java.util.UUID; import java.util.logging.Logger; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.ui.ModelMap; import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequestInterceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * * @author ddcweb */ public class SetDeviceIdInterceptor implements HandlerInterceptor { private static final Logger LOG = Logger.getLogger(SetDeviceIdInterceptor.class.getName()); //deviceId 有效期 public final static int SECONDS_10_YEARS = 3600 * 24 * 365 * 10; @Override public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception { String deviceId = cookieValue(req.getCookies(), "deviceId"); if (deviceId == null) { UUID uuid = UUID.randomUUID(); Cookie c = new Cookie("deviceId", uuid.toString()); c.setMaxAge(SECONDS_10_YEARS); resp.addCookie(c); LOG.info("add deviceId cookie"); } return true; } @Override public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object o, ModelAndView mav) throws Exception { //this set cookie not work } @Override public void afterCompletion(HttpServletRequest hsr, HttpServletResponse hsr1, Object o, Exception excptn) throws Exception { } public static Optional<Cookie> cookie(Cookie[] cookies, String cookieName) { Optional<Cookie> ret; if (cookies == null) { ret = Optional.empty(); } else { ret = Arrays.stream(cookies).filter(c -> Objects.equals(c.getName(), cookieName)).findFirst(); } return ret; } public static String cookieValue(Cookie[] cookies, String cookieName) { Optional<Cookie> cop = cookie(cookies, cookieName); String ret = cop.isPresent() ? cop.get().getValue() : null; return ret; } } |
TestController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package demo.spuuid; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * * @author ddcweb */ @Controller public class TestController { private static final Logger LOG = Logger.getLogger(TestController.class.getName()); @RequestMapping("test") @ResponseBody public String test(String name, @CookieValue(name = "deviceId", required = false) String deviceId, HttpServletResponse resp ) { LOG.log(Level.INFO, "deviceId:{0}", deviceId); Cookie c = new Cookie("fdsfdsa","rrfdehjafdsh"); //ok c.setPath("/spuuid"); //ok c.setPath("/"); resp.addCookie(c); return "你好:" + name; } } |
解决
移动到preHandle中 setCookie
参考:
http://www.cnblogs.com/insaneXs/p/7738109.html