service中获取session的方式(通过UserContext)实现

package com.qiao.p2p.base.unti;
/**
 * 用于存放当前用户上下文
 */

import com.qiao.p2p.base.entity.Logininfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UserContext {
	public final static String permissionInSession="PERMISSION_IN_SESSION";
	public static final String MENUINSESSION = "MENU_IN_SISSION";
	public final static String userInSession="loginInfo";


/**
	 * 反向获取request与session
	 * @return
	 */
private static HttpSession getSession(){
	HttpServletRequest request=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	return request.getSession();
}
public static void setCurrent(Logininfo logininfo){
	getSession().setAttribute(userInSession,logininfo);
}
public static Logininfo getCurrent(){
    return (Logininfo) getSession().getAttribute(userInSession);
}

public static ThreadLocal  local=new ThreadLocal<>();//日志用
public static void set(HttpServletRequest request) {
	local.set(request);
}
public static HttpServletRequest get() {
	 HttpServletRequest request=local.get();
	return request;
}

}

你可能感兴趣的:(#,ssm)