本地配置HOSTS域名,访问系统登录后设置cookie(name=Auth-Token path=/ expires=1800),然后重定向到首页,在首页取不到Auth-Token这个Cookie,如果设置expires=-1就可以取到
登录是设置Auth-Token:
String token=AuthServiceImpl.me.genAuthToken(u); //设置用户登录Token 时效30分钟 String servername = getRequest().getServerName(); CookieUtil.addCookie(getRequest(), getResponse(), "Auth-Token", token, 1800);
CookieUtil.addCookie代码:
public static Cookie addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, Integer expire) { Cookie cookie = new Cookie(name, value); if (expire != null) { cookie.setMaxAge(expire); } cookie.setDomain(request.getServerName()); String ctx = request.getContextPath(); cookie.setPath(StringUtils.isBlank(ctx) ? "/" : ctx); response.addCookie(cookie); return cookie; }
ActionHandle中获取cookie代码:
String oldsid = CookieUtil.getCookieValue(request, "sid"); String oldsurl = CookieUtil.getCookieValue(request, "surl"); String token = CookieUtil.getCookieValue(request, "Auth-Token"); SessionBean bean = SessionThreadLocal.get(); if (bean == null) { bean = new SessionBean(); } if (StringUtils.isNotEmpty(token)) { //CookieUtil.setCookie(response, "Auth-Token", token, "/"); CookieUtil.addCookie(request,response, "Auth-Token", token,1800); }
sid,surl都设置超时为-1,可以取到,Auth-Token设置为1800取不到。
项目:JFinal