本文實(shí)例講述了Java簡(jiǎn)單實(shí)現(xiàn)session保存到redis的方法。分享給大家供大家參考,具體如下:
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的博湖網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
在負(fù)載均衡情況下,如果用戶訪問(wèn)不同的機(jī)器,如果沒(méi)有做session同步,用戶就會(huì)被提出,這樣用戶體驗(yàn)非常不好,所以我們很有必要做session同步,把session放到reids緩存服務(wù)器就能很好的解決問(wèn)題。下面是代碼簡(jiǎn)單的實(shí)現(xiàn)。
一、配置web.xml過(guò)濾器:
sessionFilter org.springframework.web.filter.DelegatingFilterProxy sessionFilter /*
二、配置對(duì)應(yīng)的過(guò)濾器名字:
三、SessionFilter的實(shí)現(xiàn):
public class SessionFilter extends GenericFilterBean { private RedisTemplate redisTemplate; @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("filter"); HttpServletRequest re = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; TerryHttpServletRequestWrapper wrapper = new TerryHttpServletRequestWrapper(re,res,redisTemplate); chain.doFilter(wrapper, response); } public RedisTemplate getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } }
四、TerryHttpServletRequestWrapper的實(shí)現(xiàn):
public class TerryHttpServletRequestWrapper extends HttpServletRequestWrapper { private CacheHttpSession session; private HttpServletResponse response; private RedisTemplate redisTemplate; public TerryHttpServletRequestWrapper(HttpServletRequest request, HttpServletResponse response,RedisTemplate redisTemplate) { super(request); this.response = response; this.redisTemplate = redisTemplate; } @Override public HttpSession getSession(boolean create) { if(session != null) { return session; } String sid = "terry" + System.currentTimeMillis(); writeSidToCookie(sid); session = new CacheHttpSession(null,sid,redisTemplate); return session; } @Override public HttpSession getSession() { return getSession(false); } protected void writeSidToCookie(String sid) { Cookie mycookies = new Cookie("terry", sid); mycookies.setMaxAge(-1); mycookies.setDomain("locahost"); mycookies.setPath("/"); response.addCookie(mycookies); } }
五、CacheHttpSession的實(shí)現(xiàn):
public class CacheHttpSession extends HttpSessionWrapper { private String sid; private RedisTemplate redisTemplate; private StringRedisSerializer stringSerializer = new StringRedisSerializer(); public CacheHttpSession(HttpSession session,String sid,RedisTemplate redisTemplate) { super(session); this.sid = sid; this.redisTemplate = redisTemplate; } @SuppressWarnings("unchecked") @Override public EnumerationgetAttributeNames() { final byte[] key = stringSerializer.serialize(sid); Object result = redisTemplate.execute(new RedisCallback
六、HttpSessionWrapper的實(shí)現(xiàn):
public class HttpSessionWrapper implements HttpSession { private HttpSession session; public HttpSessionWrapper(HttpSession session) { this.session = session; } @Override public long getCreationTime() { return this.session.getCreationTime(); } @Override public String getId() { return this.session.getId(); } @Override public long getLastAccessedTime() { return this.session.getLastAccessedTime(); } @Override public ServletContext getServletContext() { return this.session.getServletContext(); } @Override public void setMaxInactiveInterval(int interval) { this.session.setMaxInactiveInterval(interval); } @Override public int getMaxInactiveInterval() { return this.session.getMaxInactiveInterval(); } @Override public HttpSessionContext getSessionContext() { return this.session.getSessionContext(); } @Override public Object getAttribute(String name) { return this.session.getAttribute(name); } @Override public Object getValue(String name) { return this.session.getValue(name); } @Override public EnumerationgetAttributeNames() { return this.session.getAttributeNames(); } @Override public String[] getValueNames() { return this.session.getValueNames(); } @Override public void setAttribute(String name, Object value) { this.session.setAttribute(name,value); } @Override public void putValue(String name, Object value) { this.session.putValue(name,value); } @Override public void removeAttribute(String name) { this.session.removeAttribute(name); } @Override public void removeValue(String name) { this.session.removeValue(name); } @Override public void invalidate() { this.session.invalidate(); } @Override public boolean isNew() { return this.session.isNew(); } }
以上代碼就能把session保存到redis,當(dāng)然這其中還有很多問(wèn)題,比如sessionId的生成,session過(guò)去,對(duì)象序列化(測(cè)試為了方便統(tǒng)一用了string),等等很多問(wèn)題。以后有時(shí)間再完善。
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java+MySQL數(shù)據(jù)庫(kù)程序設(shè)計(jì)總結(jié)》、《Java操作Excel技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java文件與目錄操作技巧匯總》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。