您的位置:首页 > 编程语言 > Java开发

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: