zoukankan      html  css  js  c++  java
  • 共享全局对象

    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中变量
    实体类中设置对应需要共享的变量





  • 相关阅读:
    Wannafly挑战赛13 C:zzf的好矩阵(思维)
    Wannafly挑战赛13 B:Jxc军训(逆元)
    TZOJ 1221 Tempter of the Bone(回溯+剪枝)
    AtCoder Regular Contest 092 C
    TZOJ 3030 Courses(二分图匹配)
    TOJ 2778 数据结构练习题――分油问题(广搜和哈希)
    PAT L3-001 凑零钱(01背包dp记录路径)
    [HNOI2009]通往城堡之路
    [HNOI2006]潘多拉的宝盒
    [bzoj4361]isn
  • 原文地址:https://www.cnblogs.com/yuguohua/p/12844121.html
Copyright © 2011-2022 走看看