基于spring-session的Sesion工具类
来源:互联网 发布:加拿大预科知乎 编辑:程序博客网 时间:2024/05/18 02:46
spring-session-data-redis可结合redis实现session的共享,并且使用SessionRepositoryFilter代理了所有session的相关方法。
然鹅,这么牛逼的模块却只能用在session上,不能自定义某些操作,略有不爽。于是自己写了一个session工具类:
private SessionRepository sessionRepository;public void setSessionRepository(RedisConnectionFactory redisConnectionFactory) { sessionRepository = new RedisOperationsSessionRepository(redisConnectionFactory);}创建session:
public String createSession() { Session session = sessionRepository.createSession(); if (session == null) { throw new IllegalStateException("Can not create redis session. "); } sessionRepository.save(session); String sessionId = session.getId(); RequestUtil.getRequest().setAttribute(sessionIdName, sessionId); if (cookieMode) { Cookie cookie = new Cookie(sessionIdName, sessionId); cookie.setPath("/"); cookie.setMaxAge(-1); ResponseUtil.getResponse().addCookie(cookie); } return sessionId;}
写一个判断通过何种方式获取sessionId的方法:
private String getSessionIdFromRequest() { HttpServletRequest request = RequestUtil.getRequest(); String sessionId = (String) request.getAttribute(sessionIdName); if (sessionId != null) { return sessionId; } else if (cookieMode) { Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals(sessionIdName)) { sessionId = cookie.getValue(); } } } } else if (fromHead) { sessionId = request.getHeader(sessionIdName); } else { sessionId = request.getParameter(sessionIdName); } if (sessionId == null) throw new IllegalArgumentException("No sessionId param found."); return sessionId;}
getAttribute、setAttribute:
public void setAttribute(String key, Object value) { setAttribute(getSessionIdFromRequest(), key, value);}public void setAttribute(String sessionId, String key, Object value) { Session session = sessionRepository.getSession(sessionId); if (session == null) return; session.setAttribute("timestamp_", new Date().getTime()); session.setAttribute(key, value); sessionRepository.save(session);}public <T> T getAttribute(String key) { return getAttribute(getSessionIdFromRequest(), key);}public <T> T getAttribute(String sessionId, String key) { Session session = sessionRepository.getSession(sessionId); if (session == null) return null; session.setAttribute("timestamp_", new Date().getTime()); sessionRepository.save(session); return (T) session.getAttribute(key);}
附上完整代码:
@SuppressWarnings("unchecked")public class SessionUtil { private SessionRepository sessionRepository; private String sessionIdName = "token"; private boolean fromHead = false; private boolean cookieMode = false; public void setSessionIdName(String sessionIdName) { this.sessionIdName = sessionIdName; } public String getSessionIdName() { return sessionIdName; } public void setSessionRepository(RedisConnectionFactory redisConnectionFactory) { sessionRepository = new RedisOperationsSessionRepository(redisConnectionFactory); } public void setFromHead(boolean fromHead) { this.fromHead = fromHead; } public void setCookieMode(boolean cookieMode) { this.cookieMode = cookieMode; } public void setMaxInactive(int maxInactive) { ((RedisOperationsSessionRepository) sessionRepository).setDefaultMaxInactiveInterval(maxInactive); } public void setNamespace(String namespace) { if (namespace != null) ((RedisOperationsSessionRepository) sessionRepository).setRedisKeyNamespace(namespace); } public String createSession() { Session session = sessionRepository.createSession(); if (session == null) { throw new IllegalStateException("Can not create redis session. "); } sessionRepository.save(session); String sessionId = session.getId(); RequestUtil.getRequest().setAttribute(sessionIdName, sessionId); if (cookieMode) { Cookie cookie = new Cookie(sessionIdName, sessionId); cookie.setPath("/"); cookie.setMaxAge(-1); ResponseUtil.getResponse().addCookie(cookie); } return sessionId; } private String getSessionIdFromRequest() { HttpServletRequest request = RequestUtil.getRequest(); String sessionId = (String) request.getAttribute(sessionIdName); if (sessionId != null) { return sessionId; } else if (cookieMode) { Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals(sessionIdName)) { sessionId = cookie.getValue(); } } } } else if (fromHead) { sessionId = request.getHeader(sessionIdName); } else { sessionId = request.getParameter(sessionIdName); } if (sessionId == null) throw new IllegalArgumentException("No sessionId param found."); return sessionId; } public void setAttribute(String key, Object value) { setAttribute(getSessionIdFromRequest(), key, value); } public void setAttribute(String sessionId, String key, Object value) { Session session = sessionRepository.getSession(sessionId); if (session == null) return; session.setAttribute("timestamp_", new Date().getTime()); session.setAttribute(key, value); sessionRepository.save(session); } public <T> T getAttribute(String key) { return getAttribute(getSessionIdFromRequest(), key); } public <T> T getAttribute(String sessionId, String key) { Session session = sessionRepository.getSession(sessionId); if (session == null) return null; session.setAttribute("timestamp_", new Date().getTime()); sessionRepository.save(session); return (T) session.getAttribute(key); } public void removeAttribute(String key) { removeAttribute(getSessionIdFromRequest(), key); } public void removeAttribute(String sessionId, String key) { Session session = sessionRepository.getSession(sessionId); if (session == null) return; session.setAttribute("timestamp_", new Date().getTime()); session.removeAttribute(key); sessionRepository.save(session); } public void invalidate() { invalidate(getSessionIdFromRequest()); } public void invalidate(String sessionId) { sessionRepository.delete(sessionId); }}
spring boot的配置:
@Beanpublic SessionUtil sessionUtil(RedisConnectionFactory factory) { SessionUtil sessionUtil = new SessionUtil(); sessionUtil.setSessionRepository(factory); sessionUtil.setMaxInactive(3600); sessionUtil.setNamespace("xxx");// sessionUtil.setCookieMode(true); return sessionUtil;}
可以用以下几种方式调用:
1.最基本的方式,通过sessionId直接调用:
sessionUtil.setAttribute(sessionId, "userName", "Eric");sessionUtil.getAttribute(sessionId, "userName");
2.通过post、get请求的参数传递sessionId
3.通过request请求的head传递sessionId参数(对应代码的fromHean设为true)
4.通过cookie传递sessionId参数(对应代码里的cookieMode设为true)请求中实现以上三种方式中的其中一种可以直接使用sessionUtil工具类:
sessionUtil.setAttribute("userName", "Eric");sessionUtil.getAttribute("userName");
源码可参见GitHub:
https://github.com/rewerma/redis-session
阅读全文
0 0
- 基于spring-session的Sesion工具类
- spring中获得sesion的工具类
- 同顶级域名或IP,不同应用,基于spring-sesion的session共享
- Spring session 工具类
- sesion applicaiton调用spring bean
- 基于Spring的 Redis工具类
- 基于session的spring国际化配置
- 基于redis的Spring session配置
- 基于Spring session data redis的session共享
- 基于Spring session 和 redis 的session共享问题
- 基于spring的redisTemplate的缓存工具类
- 基于spring的redisTemplate的缓存工具类
- dwr 出现 Sesion Error 的解决方法
- 基于Spring Boot-Spring Session-Redis的分布式Session共享解决方案
- 基于Spring+session bean+hibernate的j2ee开发
- spring ApplicationContext.xml之基于Session的国际化配置初探
- 基于java config的springSecurity(六)--集成spring session
- Spring MVC国际化——基于Session的国际化
- ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
- 从零开始的JAVA之路.序章
- Linux历史及发展浅谈
- HDU 1251 统计难题 (字典树模板题)
- 【简记】Operating System—— distributed system
- 基于spring-session的Sesion工具类
- Python所涉及领域
- 选择虚拟主机还是服务器对网站排名有什么样的影响?
- CSS基础-背景和精灵图 13
- @RestController与@Controller区别
- Java
- 简单工厂模式
- java 中的值传递,与引用传递
- 03.20 Linux文件属性