1 //定義請求通過的Session過濾器 2 public class SessionFilter extends OncePerRequestFilter implements Filter { 3 @Override 4 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 5 throws ServletException, IOException { 6 // 從cookie中獲取sessionId,若是這次請求沒有sessionId,重寫爲此次請求設置一個sessionId 7 String sid = CookieUtil.getCookieValue(request, GlobalConstant.JSESSIONID); 8 if (StringUtils.isEmpty(sid) || sid.length() != 36) { 9 sid = UUID.randomUUID().toString(); 10 CookieUtil.setCookie(request, response, GlobalConstant.JSESSIONID, sid, 60 * 60); 11 } 12 // 交給自定義的HttpServletRequestWrapper處理 13 filterChain.doFilter(new HttpServletRequestWrapper(sid, request, response), response); 14 } 15 }
1 //Cookie 2 public static void setCookie(HttpServletRequest request, 3 HttpServletResponse response, String name, String value, int seconds) { 4 if (StringUtils.isEmpty(name) || StringUtils.isEmpty(value)) 5 return; 6 Cookie cookie = new Cookie(name, value); 7 //cookie.setDomain(domain); 8 cookie.setMaxAge(seconds); 9 cookie.setPath("/"); 10 response.setHeader("P3P", 11 "CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'"); 12 response.addCookie(cookie); 13 } 14 15 public String getCookieValue(String name) 16 throws UnsupportedEncodingException { 17 Cookie cookies[] = request.getCookies(); 18 if (cookies != null) { 19 for (int i = 0; i < cookies.length; i++) { 20 if (name.equalsIgnoreCase(cookies[i].getName())) { 21 return cookies[i].getValue(); 22 } 23 } 24 } 25 return ""; 26 }
1 //SessionService實現 sidKey == sessionID+SessionKey 2 public Object getSession(String sidKey) { 3 Object realValue = null; 4 try { 5 String key = 「SESSION_DISTRIBUTED_SESSIONID」 + sidKey; 6 realValue = SerializeUtil.unserialize(RedisUtils.getInstance().get(key.getBytes())); 7 } catch (Exception e) { 8 LOG.error("Redis獲取session異常" + e.getMessage(), e.getCause()); 9 } 10 return realValue; 11 } 12 13 public void saveSession(String sidKey, Object value) { 14 try { 15 String key = 「SESSION_DISTRIBUTED_SESSIONID」 + sidKey; 16 boolean isSetSuccess = RedisUtils.getInstance().set(key.getBytes(), SerializeUtil.serialize(value)); 17 if (!isSetSuccess) { 18 LOG.error("Redis保存session異常"); 19 } 20 } catch (Exception e) { 21 LOG.error("Redis保存session異常" + e.getMessage(), e.getCause()); 22 } 23 } 24 25 public void removeSession(String sidKey) { 26 try { 27 String key =「SESSION_DISTRIBUTED_SESSIONID」+ sidKey; 28 RedisUtils.getInstance().del(key.getBytes()); 29 } catch (Exception e) { 30 LOG.error("Redis刪除session的attribute異常" + e.getMessage(), e.getCause()); 31 } 32 } 33 34 public void removeAllSession(String sid) { 35 try { 36 String keyPattern =「SESSION_DISTRIBUTED_SESSIONID」 + sid + "*"; 37 Set<byte[]> keys = RedisUtils.getInstance().keys(keyPattern.getBytes()); 38 for (byte[] key : keys) { 39 RedisUtils.getInstance().del(key); 40 } 41 } catch (Exception e) { 42 LOG.error("Redis刪除session異常" + e.getMessage(), e.getCause()); 43 } 44 } 45 46 public Set<String> getAllKeys(String sid) { 47 try { 48 Set<String> keysResult = new HashSet<String>(); 49 String keyPattern =「SESSION_DISTRIBUTED_SESSIONID」 + sid + "*"; 50 Set<byte[]> keys = RedisUtils.getInstance().keys(keyPattern.getBytes()); 51 52 for (byte[] key : keys) { 53 keysResult.add(new String(key)); 54 } 55 return keysResult; 56 } catch (Exception e) { 57 LOG.error("Redis刪除session異常" + e.getMessage(), e.getCause()); 58 return null; 59 } 60 }
1 HttpServletRequestWrapper extends javax.servlet.http.HttpServletRequestWrapper 2 private HttpSession session; 3 4 private HttpServletRequest request; 5 6 private HttpServletResponse response; 7 8 private String sid = ""; 9 10 public HttpServletRequestWrapper(HttpServletRequest request) { 11 super(request); 12 } 13 14 public HttpServletRequestWrapper(String sid, HttpServletRequest request) { 15 super(request); 16 this.sid = sid; 17 } 18 19 public HttpServletRequestWrapper(String sid, HttpServletRequest request, HttpServletResponse response) { 20 super(request); 21 this.request = request; 22 this.response = response; 23 this.sid = sid; 24 if (this.session == null) { 25 this.session = new HttpSessionWrapper(sid, super.getSession(false), request, response); 26 } 27 } 28 29 @Override 30 public HttpSession getSession(boolean create) { 31 if (this.session == null) { 32 if (create) { 33 this.session = new HttpSessionWrapper(this.sid, super.getSession(create), this.request, this.response); 34 return this.session; 35 } else { 36 return null; 37 } 38 } 39 return this.session; 40 } 41 42 @Override 43 public HttpSession getSession() { 44 if (this.session == null) { 45 this.session = new HttpSessionWrapper(this.sid, super.getSession(), this.request, this.response); 46 } 47 return this.session; 48 }
1 HttpSessionWrapper implements HttpSession{ 2 3 private String sid = ""; 4 5 private HttpSession session; 6 7 private HttpServletRequest request; 8 9 private HttpServletResponse response; 10 11 private SessionService sessionService = (SessionService) SpringContextHolder.getBean("sessionService"); 12 13 public HttpSessionWrapper() { 14 } 15 16 public HttpSessionWrapper(HttpSession session) { 17 this.session = session; 18 } 19 20 public HttpSessionWrapper(String sid, HttpSession session) { 21 this(session); 22 this.sid = sid; 23 } 24 25 public HttpSessionWrapper(String sid, HttpSession session, 26 HttpServletRequest request, HttpServletResponse response) { 27 this(sid, session); 28 this.request = request; 29 this.response = response; 30 } 31 32 33 @Override 34 public Object getAttribute(String name) { 35 return sessionService.getSession(this.sid+"#"+name); 36 } 37 38 @Override 39 public void setAttribute(String name, Object value) { 40 sessionService.saveSession(this.sid+"#"+name, value); 41 } 42 43 @Override 44 public void invalidate() { 45 sessionService.removeAllSession(this.sid); 46 CookieUtil.removeCookieValue(this.request,this.response, GlobalConstant.JSESSIONID); 47 } 48 49 @Override 50 public void removeAttribute(String name) { 51 sessionService.removeSession(this.sid+"#"+name); 52 } 53 54 @Override 55 public Object getValue(String name) { 56 return this.session.getValue(name); 57 } 58 59 @SuppressWarnings("unchecked") 60 @Override 61 public Enumeration getAttributeNames() { 62 return (new Enumerator(sessionService.getAllKeys(this.sid), true)); 63 } 64 65 @Override 66 public String getId() { 67 return this.sid; 68 }69 }