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;
}
}
/**
*
*
* @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
- java权限设计思路之一
- java开发的权限设计思路
- 权限设计思路
- rbac权限设计思路
- 权限控制的设计思路
- 对于权限设计的思路
- 用户角色权限设计思路
- 权限管理系统 设计思路
- 用户角色权限设计思路
- 用户角色权限设计思路
- 用户角色权限设计思路
- SSO权限控制设计思路
- 用户角色权限设计思路
- 权限管理数据库设计思路
- 数据库设计之权限设计思路
- Java日历设计思路
- 局域网聊天|权限管理系统设计思路
- 权限管理组件的设计、开发思路
- revit二次开发 获取水管的坐标
- Android设计模式(八)--模板方法模式
- Unity3D面试题
- 命令行解析器
- 华为oj之密码验证合格程序
- java权限设计思路之一
- Python--模块
- 黑马程序员——Java基础---集合
- Socket编程的面纱
- 算法和数据结构就是编程的一个重要部分,你若失掉了算法和数据结构,你就把一切都失掉了。
- HDU ACM 4473 Exam->数论(思维-问题转换)
- 倒排索引 和 倒排表
- 9针串口线简介
- iOS Ad-Hoc 内测笔记