java权限设计思路之一
2015-06-09 14:45
162 查看
package com.hety.auth;
/**
*
*
* @author hety
* @version 1.0 2015-6-9 下午2:27:06
*/
public class AuthTest {
public static void main(String[] args) {
/*
* 如果用户有权限:添加A---2;删除B---3;修改B---4
那用户的权限值 purview =2^2+2^3+2^4=28,也就是2的权的和了。
化成二进制可以表示为11100
这样,如果要验证用户是否有删除B的权限,就可以通过位与运算来实现。
在Java里,位与运算运算符号为&
即是:int value = purview &((int)Math.pow(2,3));
*/
int test =11;
int c = ( ((int)Math.pow(2,1)+(int)Math.pow(2,2)+(int)Math.pow(2,3)+(int)Math.pow(2,4)) );
System.out.println("2^2+2^3+2^4="+c);
boolean e = checkPower(c, test);
System.out.println("auth:"+e );
}
// userPurview是用户具有的总权限
// optPurview是一个操作要求的权限为一个整数(没有经过权的!)
public static boolean checkPower(int userPurview, int optPurview) {
int purviewValue = (int) Math.pow(2, optPurview);
return (userPurview & purviewValue) == purviewValue;
}
}
/**
*
*
* @author hety
* @version 1.0 2015-6-9 下午2:27:06
*/
public class AuthTest {
public static void main(String[] args) {
/*
* 如果用户有权限:添加A---2;删除B---3;修改B---4
那用户的权限值 purview =2^2+2^3+2^4=28,也就是2的权的和了。
化成二进制可以表示为11100
这样,如果要验证用户是否有删除B的权限,就可以通过位与运算来实现。
在Java里,位与运算运算符号为&
即是:int value = purview &((int)Math.pow(2,3));
*/
int test =11;
int c = ( ((int)Math.pow(2,1)+(int)Math.pow(2,2)+(int)Math.pow(2,3)+(int)Math.pow(2,4)) );
System.out.println("2^2+2^3+2^4="+c);
boolean e = checkPower(c, test);
System.out.println("auth:"+e );
}
// userPurview是用户具有的总权限
// optPurview是一个操作要求的权限为一个整数(没有经过权的!)
public static boolean checkPower(int userPurview, int optPurview) {
int purviewValue = (int) Math.pow(2, optPurview);
return (userPurview & purviewValue) == purviewValue;
}
}
相关文章推荐
- java中byte与int互转
- java中byte与int互转
- Java泛型 通配符的限定
- java的类加载器ClassLoader
- java自动获得电脑网络配置并存储在beanUtil中
- java的类加载器ClassLoader
- JAVA 中的 Collection FrameWork
- 修改eclipse/MyEclipse中项目包的显示结构
- java集合类都有哪些?主要方法?
- Spring学习笔记之Spring与JDBC整合
- java的类加载器ClassLoader
- java设定窗口步长,依次统计窗口内数值总和
- java设定窗口步长,依次统计窗口内数值总和
- springmvc快速入门
- JAVA字符串分割,两个分隔符的,怎么实现
- android studio 快捷键设置为eclipse的习惯
- spring MVC 2-helloword 入门
- JavaBean的详细及引用
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发
- JAVA 语言如何进行异常处理,关键字:throws,throw,try,catch,finally 分别代表什么意义?在 try 块中可以抛出异常吗?