使用ThreadLocal存储Session,方便其他地方调用用户信息

阅读更多

 

package com.urt.core.web;

import javax.servlet.http.HttpSession;

import com.urt.core.authority.entity.User;

/** 
 * 用户Session存储器,方便在Controller层以外获取用户
 * @author happyqing
 * @date 2015-4-25
 */
public class UserSession {
	private static final ThreadLocal userSession = new ThreadLocal();
	
	public static void setUserSession(HttpSession session) {
		userSession.set(session);
    }
  
    public static HttpSession getUserSession() {
        return userSession.get();
    }
    
    public static User getUser(){
		return (User)userSession.get().getAttribute("user");
	}
    
    public static void setAttribute(String key, Object value){
    	userSession.get().setAttribute(key, value);
	}
    
    public static Object getAttribute(String key){
    	return userSession.get().getAttribute(key);
	}

}

 

你可能感兴趣的:(ThreadLocal,存储,会话,Session,用户)