使用ThreadLocal保存用户登录信息
2014-11-18 19:37
591 查看
最近在使用WebSocket来进行消息推送,一,系统推送。二,用户对用户进行推送。
SystemSession类:
在配置一个用户session状态变更的监听类MyHttpSessionAttributeListener:
这样之后,就可以在程序中直接SystemSession.get() 就能得到用户对象,如:
SystemSession类:
import com.srkj.code.model.User; /** * 线程共享类 * @author 胡汉三 * * 2014-11-18 下午2:40:27 */ public class SystemSession { //声明 private static ThreadLocal<User> local = new ThreadLocal<User>(); /** * 赋值 * @param session */ public static void set(User session) { local.set(session); } /** * 取值 * @return */ public static User get() { return local.get(); } /** * 移除 */ public static void remove(){ local.remove(); } }使用过滤器拦截用户请求:
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import com.srkj.code.model.User; /** * Session过滤器 * @author 胡汉三 * * 2014-11-18 下午2:41:35 */ public class MyFilter implements Filter{ public void destroy() {} public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; User userSession = (User)request.getSession().getAttribute("userInfo"); if (userSession != null) { //重新设值session request.getSession().setAttribute("userInfo", userSession); } filterChain.doFilter(req, res); } public void init(FilterConfig arg0) throws ServletException {} }web.xml配置:
<filter> <filter-name>myFilter</filter-name> <filter-class>com.srkj.code.util.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在配置一个用户session状态变更的监听类MyHttpSessionAttributeListener:
import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import com.srkj.code.model.User; /** * Session监听器 * @author 胡汉三 * * 2014-11-18 下午2:42:02 */ public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener { /** * 添加Session */ public void attributeAdded(HttpSessionBindingEvent event) { if ("userInfo".equals(event.getName())) { SystemSession.set((User)event.getValue()); } } /** * 替代Session */ public void attributeReplaced(HttpSessionBindingEvent event) { if ("userInfo".equals(event.getName())) { SystemSession.set((User)event.getValue()); } } /** * 销毁Session */ public void attributeRemoved(HttpSessionBindingEvent event) { if("userInfo".equals(event.getName())){ SystemSession.remove(); } } }web.xml监听配置:
<listener> <listener-class>com.srkj.code.util.MyHttpSessionAttributeListener</listener-class> </listener>
这样之后,就可以在程序中直接SystemSession.get() 就能得到用户对象,如:
public ChatAnnotation() { nickname = SystemSession.get().getUuuid(); }
相关文章推荐
- ThreadLocal使用 --用于保存每个登录用户的信息-userInfo
- php使用session来保存用户登录信息
- php同时使用session和cookie来保存用户登录信息的实现代码
- php使用session来保存用户登录信息
- php同时使用session和cookie来保存用户登录信息
- 使用COOKIE对像实现保存用户基本信息(结合Session),ASP.Net实现用户登录全过程
- 登录时保存用户信息 --- NSUserDefaults的简单使用
- php同时使用session和cookie来保存用户登录信息
- php同时使用session和cookie来保存用户登录信息
- php中如何同时使用session和cookie来保存用户登录信息
- java使用cookies保存用户登录信息
- php中如何同时使用session和cookie来保存用户登录信息
- PHP中如何使用session实现保存用户登录信息
- php使用session来保存用户登录信息
- php使用session来保存用户登录信息
- php同时使用session和cookie来保存用户登录信息
- php使用cookie来保存用户登录信息
- php中使用cookie来保存用户登录信息的实现代码
- PHP中如何使用session实现保存用户登录信息