ThreadLocal取代每次都从session中获取登录用户信息
来源:互联网 发布:淘宝帽子女士 编辑:程序博客网 时间:2024/05/17 03:57
方式一:直接使用JDK中ThreadLocal方式
1.先定义一个存放登录用户信息的实体类,并在这里面操纵用户信息
public class UserContext implements Serializable{ private static ThreadLocal<LoginEntity> loginEntityThreadLocal=new ThreadLocal<>(); public static LoginEntity getUserSession() { return loginEntityThreadLocal.get(); } public static void setUserSession(LoginEntity entity) { loginEntityThreadLocal.set(entity); } public static void removeUserSession() { loginEntityThreadLocal.remove(); }}
2.在过滤其中进行存放用户登录信息
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) resp;HttpSession session = request.getSession();request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");response.addHeader("Access-Control-Allow-Origin","*");response.addHeader("Access-Control-Allow-Credentials", "true");response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,X-Pagination");response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");response.addHeader("Access-Control-Expose-Headers", "X-Pagination");//System.out.println(session.getId());if (isInclude(request.getServletPath())) {LoginEntity loginEntity = (LoginEntity) session.getAttribute(Const.LOGINED_KEY);if (loginEntity != null) {UserContext.setUserSession(loginEntity);chain.doFilter(request, response);} else {unLogin(response);}} else {chain.doFilter(request, response);}}
方式二:RequestContextHolder是Spring中对ThreadLocal进行了封装
1.先定义一个存放登录用户信息的实体类,并在这里面操纵用户信息
public class UserContext implements Serializable{ /** * 获取当前线程绑定的用户登录对象 * * @return */ public static LoginEntity getUserSession() { return (LoginEntity) RequestContextHolder.getRequestAttributes().getAttribute(Constant.LOGINED_KEY, RequestAttributes.SCOPE_REQUEST); } /** * 将用户登录对象绑定到当前线程 * * @param loginEntity */ public static void setUserSession(LoginEntity loginEntity) { RequestContextHolder.getRequestAttributes().setAttribute(Constant.LOGINED_KEY, loginEntity, RequestAttributes.SCOPE_REQUEST); } /** * 将用户登录对象从当前线程销毁 */ public static void removeUserSession() { RequestContextHolder.getRequestAttributes().removeAttribute(Constant.LOGINED_KEY,RequestAttributes.SCOPE_REQUEST); }}
2.在过滤其中进行存放用户登录信息
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) resp;HttpSession session = request.getSession();request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");response.addHeader("Access-Control-Allow-Origin","*");response.addHeader("Access-Control-Allow-Credentials", "true");response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,X-Pagination");response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");response.addHeader("Access-Control-Expose-Headers", "X-Pagination");//System.out.println(session.getId());if (isInclude(request.getServletPath())) {LoginEntity loginEntity = (LoginEntity) session.getAttribute(Const.LOGINED_KEY);if (loginEntity != null) {UserContext.setUserSession(loginEntity);chain.doFilter(request, response);} else {unLogin(response);}} else {chain.doFilter(request, response);}}
阅读全文
2 0
- ThreadLocal取代每次都从session中获取登录用户信息
- 从Active Directory中获取用户信息
- 获取session用户信息
- liferay 获取登录用户信息
- androidQQ登录获取用户信息
- 如何在Service层获取用户session中保存的用户信息的方法(Filter+ThreadLocal)
- edusoho 从某个二维数组中获取获取用户信息
- 如何在主题与皮肤中获取Portal登录用户信息
- 如何在主题与皮肤中获取Portal登录用户信息
- Shiro(3) controller中获取当前登录用户信息
- QQ登录的ifame的id每次都变,怎么去获取登录态
- springMVC框架,后台中session添加用户信息和获取用户信息的问题
- google登录 java获取用户信息
- 微信小程序登录获取用户信息
- 权限管理系统 ThreadLocal的使用:在同一线程中获取用户信息
- sharepoint 中获取用户信息
- 登录后保存用户信息到Session
- php中 session存储用户信息
- 设置eclipse编码格式大全
- kubernetes安装与配置
- VirtualBox + Ubuntu 网络配置
- VMware Ubuntu安装详细过程
- python模块之subprocess
- ThreadLocal取代每次都从session中获取登录用户信息
- iOS The binary you uploaded was invalid
- linux centos 磁盘挂载
- NOTE: cannot fetch plan for SQL_ID:
- C标签的if else
- Spring事件--Application Event
- Lombok的使用与学习
- VirtualBox 安装 Linux时 报错This kernel requires an X86-64 CPU,but only detected an i686 CPU错误的解决办法
- CutDownTimer