共享全局对象

public class UserContext {

public static final String SUBJECT_IN_SESSION = "SUBJECT_IN_SESSION";
public static final String EXPRESSIONS_IN_SESSION = "EXPRESSIONS_IN_SESSION";
//获取session 公共的整个项目 方便调用 所以写出
public static HttpSession getSession(){
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return requestAttributes.getRequest().getSession();
}

//共享当前用户
public static void setCurrentEmp(SubJectObjectUtil currentEmp){
getSession().setAttribute(SUBJECT_IN_SESSION,currentEmp);
}


//获取当前用户
public static SubJectObjectUtil getCurrentEmp(){
return (SubJectObjectUtil) getSession().getAttribute(SUBJECT_IN_SESSION);
}

//共享当前用户的权限表达式
public static void setExpressions(List<String> expressions){
getSession().setAttribute(EXPRESSIONS_IN_SESSION, expressions);
}

//获取当前用户的权限表达式
public static List<String> getExpressions(){
return (List<String>) getSession().getAttribute(EXPRESSIONS_IN_SESSION);
}
}



在拦截器中拦截请求 获取对应对用户信息 那对应对用户信息set 到对应到对象中 在设置对象到session中 在该项目中调用getsesion 获到对应到详细信息 ‘
String token1 = request.getHeader("token");
String manufactorId = JSONObject.parseObject(jwtUtil.parseJWT(token1).getSubject()).getString("id");
SubJectObjectUtil subJectObjectUtil1 = new SubJectObjectUtil();//设置到实体类中
subJectObjectUtil1.setManufactorId(manufactorId);
UserContext.setCurrentEmp(subJectObjectUtil1); //设置到session 中  
SubJectObjectUtil session1 = UserContext.getCurrentEmp();//获取session中变量
System.out.println(session1.getManufactorId());//获取session中变量
实体类中设置对应需要共享的变量





posted @ 2020-05-07 16:57  余乐  阅读(158)  评论(0编辑  收藏  举报