1 public class CookieUtil { 2 //domain 可以访问该Cookie的域名。如果设置为“.google.com”,则所有以“google.com” 3 //结尾的域名都可以访问该Cookie。注意第一个字符必须为“.”。 4 private static final String COOKIE_DOMAIN = ".zhangjiang.com"; 5 //name Cookie的名称,Cookie一旦创建,名称便不可更改 6 private static final String COOKIE_NAME = "zhangjiang_login_token"; 7 8 public static String readLoginToken(HttpServletRequest request) { 9 Cookie[] cookies = request.getCookies(); 10 if (cookies != null) { 11 for (Cookie cookie : cookies) { 12 log.info("read cookieName:{}, cookieValue:{}", cookie.getName(), cookie.getValue()); 13 if (StringUtils.equals(COOKIE_NAME, cookie.getName())) { 14 log.info("return cookieName:{}, cookieValue:{}", cookie.getName(), cookie.getValue()); 15 return cookie.getValue(); 16 } 17 } 18 } 19 return null; 20 } 21 public static void delLoginToken(HttpServletRequest request, HttpServletResponse res ponse) { 22 Cookie[] cookies = request.getCookies(); 23 if (cookies != null) { 24 for (Cookie cookie : cookies) { 25 if (StringUtils.equals(COOKIE_NAME, cookie.getName())) { 26 cookie.setDomain(COOKIE_DOMAIN); 27 //path Cookie的使用路径。如果设置为“/sessionWeb/”,则只有 28 //contextPath为“/sessionWeb”的程序可以访问该Cookie。如果设置为“/”,则本 29 //域名下contextPath都可以访问该Cookie。注意最后一个字符必须为“/”。 30 cookie.setPath("/"); 31 // maxAge 设置为 0,表示将其删除 32 cookie.setMaxAge(0); 33 log.info("del cookieName:{}, cookieValue:{}", cookie.getName(), cookie.getValue()); 34 response.addCookie(cookie); 35 return; 36 } 37 } 38 } 39 } 40 } 41 public static void writeLoginToken(HttpServletResponse response, String token) { 42 Cookie cookie = new Cookie(COOKIE_NAME, token); 43 cookie.setDomain(COOKIE_DOMAIN); 44 cookie.setPath("/"); 45 // 防止脚本攻击 46 cookie.setHttpOnly(true); 47 // 单位是秒,如果是 ‐1,代表永久; 48 // 如果 MaxAge 不设置,cookie 不会写入硬盘,而是在内存,只在当前页面有效 49 cookie.setMaxAge(60 * 60 * 24 * 365); 50 log.info("write cookieName:{}, cookieValue:{}", cookie.getName(), 51 cookie.getValue()); 52 response.addCookie(cookie); 53 } 54 ======登陆成功==== 55 CookieUtil.writeLoginToken(response, session.getId()); 56 自定义的RedisUtil.setEx(session.getId(), 自定义的JsonUtil.obj2Str(serverResponse.getData()), 超时时间); 57 =======退出登陆======== 58 String loginToken = CookieUtil.readLoginToken(request); 59 CookieUtil.delLoginToken(request, response); 60 自定义的RedisUtil.del(loginToken); 61 ========获取用户信息======= 62 String loginToken = CookieUtil.readLoginToken(request); 63 if (StringUtils.isEmpty(loginToken)) { 64 return "用户未登录,无法获取当前用户信息"; 65 } 66 String userJsonStr = 自定义的RedisUtil.get(loginToken); 67 User user = 自定义的JsonUtil.str2Obj(userJsonStr, User.class); 68 69 SessionExpireFilter 过滤器 70 另外,在用户登录后,每次操作后,都需要重置 Session 的有效期。可以使用过滤器来实现 71 public class SessionExpireFilter implements Filter { 72 73 @Override 74 public void init(自定义配置类FilterConfig filterConfig) throws ServletException { } 75 76 @Override 77 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 78 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; 79 String loginToken = CookieUtil.readLoginToken(httpServletRequest); 80 if (StringUtils.isNotEmpty(loginToken)) { 81 String userJsonStr = 自定义的RedisUtil.get(loginToken); 82 User user = 自定义的JsonUtil.str2Obj(userJsonStr, User.class); 83 if (user != null) { 84 自定义的RedisUtil.expire(loginToken, 超时时间); 85 } 86 } 87 filterChain.doFilter(servletRequest, servletResponse); 88 } 89 90 @Override 91 public void destroy() { } 92 }
原文链接:https://www.cnblogs.com/powerZhangFly/p/13782289.html
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/18087