本文實(shí)例講述了Java簡單實(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ù)載均衡情況下,如果用戶訪問不同的機(jī)器,如果沒有做session同步,用戶就會(huì)被提出,這樣用戶體驗(yàn)非常不好,所以我們很有必要做session同步,把session放到reids緩存服務(wù)器就能很好的解決問題。下面是代碼簡單的實(shí)現(xiàn)。
一、配置web.xml過濾器:
<filter> <filter-name>sessionFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>sessionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
二、配置對(duì)應(yīng)的過濾器名字:
<bean id="sessionFilter" class="com.plateno.interceptor.SessionFilter"> <property name="redisTemplate" ref="redisTemplate"/> </bean>
三、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 Enumeration<String> getAttributeNames() { final byte[] key = stringSerializer.serialize(sid); Object result = redisTemplate.execute(new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { Set<byte[]> set = connection.keys(key); return set; } }); if(result != null) { Set<byte[]> s = (Set<byte[]>)result; Set<String> ss = new HashSet<String>(); for(byte[] b : s) { ss.add(stringSerializer.deserialize(b)); } Enumeration<String> en = new Vector(ss).elements(); return en; } return null; } @SuppressWarnings("unchecked") @Override public void setAttribute(String name, Object value) { final byte[] key = stringSerializer.serialize(name); final byte[] v = stringSerializer.serialize((String)value); redisTemplate.execute(new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { connection.set(key, v); return null; } }); } @Override public Object getAttribute(String name) { final byte[] key = stringSerializer.serialize(name); @SuppressWarnings("unchecked") Object value = redisTemplate.execute(new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key); } }); return value; } @Override public String getId() { return sid; } }
六、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 Enumeration<String> getAttributeNames() { 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)然這其中還有很多問題,比如sessionId的生成,session過去,對(duì)象序列化(測試為了方便統(tǒng)一用了string),等等很多問題。以后有時(shí)間再完善。
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java+MySQL數(shù)據(jù)庫程序設(shè)計(jì)總結(jié)》、《Java操作Excel技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java文件與目錄操作技巧匯總》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
名稱欄目:Java簡單實(shí)現(xiàn)session保存到redis的方法示例
標(biāo)題來源:http://jinyejixie.com/article10/gpiddo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、網(wǎng)站維護(hù)、品牌網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、定制開發(fā)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)