二进制权限控制

 

使用二进制 权限控制

分类: 项目模块2013-10-18 11:00 115人阅读 评论(0) 收藏 举报

目录(?)[+]

二进制权限说明

权限由 int rightPostion(权限位/组) long rightCode(权限码) 组成

int[postion][code]  :最大能到达 (2的31次方)*(63

 

权限码Long:一个字节有8位,long有8个字节,一个long就有64位,一位代表一个权限,就能表示64个权限

有一位是符号位 所以一个long能代表63个权限.

权限位Int:Int有4个字节,32位,一位是符号位 最大为:2的31次方

 

我们只用63中的60个就好啦!

 

添加更新权限

[java] view plaincopy

  1. /** 

  2.  * 保存/更新权限 

  3.  */  

  4. public void saveOrUpdateRight(Right model){  

  5.     //insert  

  6.     if(model.getId() == null){  

  7.         int rightPos = 0 ;  

  8.         long rightCode = 1 ;  

  9.         //查询最大权限位上的最大权限码  

  10.         String hql = "select max(r.rightPos),max(r.rightCode) from Right r " +  

  11.                 "where r.rightPos = (select max(rr.rightPos) from Right rr)" ;  

  12.         Object[] arr = (Object[]) this.uniqueResult(hql);  

  13.         Integer topRightPos = (Integer) arr[0] ;  

  14.         Long topRightCode = (Long) arr[1];  

  15.         if(topRightPos == null){//如果表里还没有记录  

  16.             rightPos = 0 ;  

  17.             rightCode = 1 ;  

  18.         }  

  19.         else{  

  20.             if(topRightCode >= (1L << 60)){  

  21.                 rightPos = topRightPos + 1;  

  22.                 rightCode = 1 ;  

  23.             }  

  24.             else{  

  25.                 rightPos = topRightPos ;  

  26.                 rightCode = topRightCode << 1 ;  

  27.             }  

  28.         }  

  29.         model.setRightPos(rightPos);  

  30.         model.setRightCode(rightCode);  

  31.     }  

  32.     this.saveOrUpdateEntity(model);  

  33. }  


 

用户登入(得到具有的权限)

loginAction

[html] view plaincopy

  1. /**  

  2.  * 该方法只在doLogin之前运行  

  3.  */  

  4. public void validateDoLogin(){  

  5.     User user =userService.validateLoginInfo(model.getEmail(),DataUtil.md5(model.getPassword()));  

  6.     if(user == null){  

  7.         addActionError("email/password wrong");  

  8.     }  

  9.     else{  

  10.           

  11.         //初始化权限总和数组  

  12.         int maxRightPos = rightService.getMaxRightPos();  

  13.         user.setRightSum(new long[maxRightPos + 1]);  

  14.         //计算用户的权限总和  

  15.         user.calculateRightSum();  

  16.         //user--->session  

  17.         sessionMap.put("user", user);  

  18.     }  

  19. }  


 

User Bean

 

[html] view plaincopy

  1. //是否是超级管理员  

  2. private boolean superAdmin ;  

  3. //用户权限总和  

  4. private long[] rightSum ;  


 

[html] view plaincopy

  1. /**  

  2.  * 计算用户的权限总和  

  3.  */  

  4. public void calculateRightSum() {  

  5.     int pos = 0 ;  

  6.     long code = 0 ;  

  7.     for(Role role : roles){  

  8.         //判断超级管理员  

  9.         if("-1".equals(role.getRoleValue())){  

  10.             this.superAdmin = true ;  

  11.             roles = null ;//去除 User类里面与role的绑定 减小存放进Session的User的大小  

  12.             return ;  

  13.         }  

  14.         for(Right r : role.getRights()){  

  15.             pos = r.getRightPos() ;//权限位  

  16.             code = r.getRightCode() ;//权限码  

  17.             rightSum[pos] = rightSum[pos] | code ;  //或运算叠加权限 (01101 | 1000011101)  

  18.         }  

  19.     }  

  20.     roles = null ;  

  21. }  

  22.   

  23. /**  

  24.  * 判断用户是否有指定的权限  

  25.  */  

  26. public boolean hasRight(Right r) {  

  27.     int pos = r.getRightPos();  

  28.     long code = r.getRightCode();  

  29.     long ret = rightSum[pos] & code ;    

  30.     //权限总和与当前权限不等于0说明有权限(11101 & 00100=11101 有)  

  31.     //权限总和与当前权限不等于0说明有权限(11101 & 00010=00000没 有)  

  32.     return !(ret == 0);  

  33. }  

 


RightFilterInterceptor

 

[html] view plaincopy

  1. /**  

  2.  * 判断是否有权限  

  3.  */  

  4. @SuppressWarnings("unchecked")  

  5. public static boolean hasRight(String ns,String actionName,HttpServletRequest req,BaseAction action){  

  6.     if(!ValidateUtil.isValid(ns)  

  7.             || "/".equals(ns)){  

  8.         ns = "" ;  

  9.     }  

  10.       

  11.     //处理?参数  

  12.     if(actionName.contains("?")){  

  13.         actionName = actionName.substring(0, actionName.indexOf("?"));  

  14.     }  

  15.       

  16.     String url = ns + "/" + actionName ;  

  17.     HttpSession s = req.getSession();  

  18.     ServletContext sc = s.getServletContext();  

  19.     Map<String, Right> map = (Map<String, Right>) sc.getAttribute("all_rights_map");  

  20.     Right r = map.get(url);  

  21.     if(r == null){  

  22.         return false ;   

  23.     }  

  24.     //公共资源  

  25.     if(r.isCommon()){  

  26.         return true ;  

  27.     }  

  28.     else{  

  29.         User user = (User) s.getAttribute("user");  

  30.         //登录?  

  31.         if(user == null){  

  32.             return false ;  

  33.         }  

  34.         else{  

  35.             //userAware  

  36.             if(action != null   

  37.                     && action instanceof UserAware){  

  38.                 ((UserAware)action).setUser(user);  

  39.             }  

  40.             //超级管理员?  

  41.             if(user.isSuperAdmin()){  

  42.                 return true ;  

  43.             }  

  44.             else{  

  45.                 //有权限?  

  46.                 if(user.hasRight(r)){  

  47.                     return true ;  

  48.                 }  

  49.                 else{  

  50.                     return false ;  

  51.                 }  

  52.             }  

  53.         }  

  54.           

  55.     }  

  56. }  


IniRightListener


使用监听器:listener.  spring listener.
在web服务器启动完成前,spring容器初始化之后,将所有权限,查询出来,存放到application(ServletContext)中.

[html] view plaincopy

  1. /**  

  2.  * 初始化权限监听器  

  3.  */  

  4. @SuppressWarnings("rawtypes")  

  5. @Component  

  6. public class IniRightListener implements ApplicationListener,ServletContextAware{  

  7.   

  8.     @Resource  

  9.     private RightService rs ;  

  10.       

  11.     //接受servletContext对象  

  12.     private ServletContext sc;  

  13.       

  14.     public void onApplicationEvent(ApplicationEvent arg0) {  

  15.         //是否是上下文刷新事件  

  16.         if(arg0 instanceof ContextRefreshedEvent){  

  17.             List<Right> list = rs.findAllEntities();  

  18.             Map<String, Right> map = new HashMap<String, Right>();  

  19.             for(Right r : list){  

  20.                 map.put(r.getRightUrl(), r);  

  21.             }  

  22.             if(sc != null){  

  23.                 sc.setAttribute("all_rights_map", map);  

  24.                 System.out.println("权限初始化完成了!!");  

  25.             }  

  26.         }  

  27.     }  

  28.   

  29.     //注入sc  

  30.     public void setServletContext(ServletContext servletContext) {  

  31.         //  

  32.         System.out.println("注入sc");  

  33.         this.sc = servletContext ;  

  34.     }  

  35. }  


转载于:https://my.oschina.net/cheeryzxh007/blog/266110

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值