java权限设计思路之一

来源:互联网 发布:163 smtp ssl 端口 编辑:程序博客网 时间:2024/04/28 16:08
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;
}


}
0 0
原创粉丝点击