springmvc修改session存入redis
思想:修改 session 方法,使 session 添加获取属性时,将需要存储的信息存入redis,或者从redis获取存储的信息。
1. 将拦截所有请求,交给sessionFilter处理
1 <!-- spring-session拦截器 --> 2 <filter> 3 <filter-name>sessionFilter</filter-name> 4 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>sessionFilter</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
<bean id="sessionFilter" class="com.gtafe.util.redis.SessionFilter"></bean>
1 public class SessionFilter extends GenericFilterBean { 2 3 @Override 4 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 5 throws IOException, ServletException { 6 System.out.println("filter"); 7 HttpServletRequest re = (HttpServletRequest) request; 8 HttpServletResponse res = (HttpServletResponse) response; 9 10 MyHttpServletRequestWrapper wrapper = new MyHttpServletRequestWrapper(re, res); 11 chain.doFilter(wrapper, response); 12 } 13 14 }
2. 修改获取 session 的方法
1 public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper { 2 private CacheHttpSession session; 3 private HttpServletResponse response; 4 private HttpServletRequest myRequest; 5 6 public MyHttpServletRequestWrapper(HttpServletRequest request, HttpServletResponse response) { 7 super(request); 8 this.myRequest = request; 9 this.response = response; 10 } 11 12 @Override 13 public HttpSession getSession(boolean create) { 14 15 16 if (session != null) { 17 System.out.println(" XD ============ 获取 session ============== sid = " + session.getId()); 18 return session; 19 } 20 21 String sid = null; 22 Cookie[] cookies = myRequest.getCookies();// 这样便可以获取一个cookie数组 23 System.out.println(" XD cookies ==== " + cookies); 24 if(cookies != null){ 25 for (Cookie cookie : cookies) { 26 System.out.println(" XD key = " + cookie.getName() + " value = " + cookie.getValue()); 27 if("mycookies".equals(cookie.getName())){ 28 sid = cookie.getValue(); 29 } 30 } 31 } 32 if(sid == null){ 33 sid = UUID.randomUUID().toString(); 34 } 35 System.out.println(" XD ============ 创建 session ============== sid = " + sid); 36 37 writeSidToCookie(sid); 38 session = new CacheHttpSession(null, sid); 39 return session; 40 } 41 42 @Override 43 public HttpSession getSession() { 44 return getSession(false); 45 } 46 47 protected void writeSidToCookie(String sid) { // TODO 48 Cookie mycookies = new Cookie("mycookies", sid); 49 mycookies.setMaxAge(-1); 50 //mycookies.setDomain("locahost"); 51 mycookies.setPath("/"); 52 response.addCookie(mycookies); 53 } 54 }
3. 重写的session的方法
public class CacheHttpSession extends HttpSessionWrapper { private String sid; public CacheHttpSession(HttpSession session, String sid) { super(session); this.sid = sid; } @SuppressWarnings("unchecked") @Override public Enumeration getAttributeNames() { return RedisUtil.getHashObjs(sid); } @SuppressWarnings("unchecked") @Override public void setAttribute(String name, Object value) { RedisUtil.setHObj(sid, name, value); } @Override public Object getAttribute(String name) { return RedisUtil.getHObj(sid, name); } @Override public void removeAttribute(String name) { RedisUtil.removeHObj(sid, name); } @Override public void invalidate() { RedisUtil.expirObj(sid); } @Override public String getId() { return sid; } }
4. 默认的 session 的方法实现
1 public class HttpSessionWrapper implements HttpSession { 2 3 private HttpSession session; 4 public HttpSessionWrapper(HttpSession session) { 5 this.session = session; 6 } 7 @Override 8 public long getCreationTime() { 9 return this.session.getCreationTime(); 10 } 11 @Override 12 public String getId() { 13 return this.session.getId(); 14 } 15 @Override 16 public long getLastAccessedTime() { 17 return this.session.getLastAccessedTime(); 18 } 19 @Override 20 public ServletContext getServletContext() { 21 return this.session.getServletContext(); 22 } 23 @Override 24 public void setMaxInactiveInterval(int interval) { 25 this.session.setMaxInactiveInterval(interval); 26 } 27 @Override 28 public int getMaxInactiveInterval() { 29 return this.session.getMaxInactiveInterval(); 30 } 31 @Override 32 public HttpSessionContext getSessionContext() { 33 return this.session.getSessionContext(); 34 } 35 @Override 36 public Object getAttribute(String name) { 37 return this.session.getAttribute(name); 38 } 39 @Override 40 public Object getValue(String name) { 41 return this.session.getValue(name); 42 } 43 @Override 44 public Enumeration<String> getAttributeNames() { 45 return this.session.getAttributeNames(); 46 } 47 @Override 48 public String[] getValueNames() { 49 return this.session.getValueNames(); 50 } 51 @Override 52 public void setAttribute(String name, Object value) { 53 this.session.setAttribute(name,value); 54 } 55 @Override 56 public void putValue(String name, Object value) { 57 this.session.putValue(name,value); 58 } 59 @Override 60 public void removeAttribute(String name) { 61 this.session.removeAttribute(name); 62 } 63 @Override 64 public void removeValue(String name) { 65 this.session.removeValue(name); 66 } 67 @Override 68 public void invalidate() { 69 this.session.invalidate(); 70 } 71 @Override 72 public boolean isNew() { 73 return this.session.isNew(); 74 } 75 76 }