当前位置:网站首页>用二进制进行权限管理
用二进制进行权限管理
2022-04-23 05:45:00 【dawnsun001】
直接给例子吧!例子中将讲解 1.权限表示 2.权限判断 3.添加权限 4.取消权限
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- /**
- * 四种权限 ,当前定义为int,以下二进制表示只取后四位作说明
- */
- // 添加
- int c = 1;// ...0001
- // 查询
- int r = 2;// ...0010
- // 修改
- int u = 4;// ...0100
- // 删除
- int d = 8;// ...1000
- /**
- *
- * 大家可以观察四种权限的二进制表示的规律 ,都是2的N次方,
- * 就表示本身,添加权限有最后一位为其它为0,查询倒数第二位为1其它都为0,修改倒数第三个为1其它都为0,删除倒数第四个为1其它都为0
- *
- */
- /**
- *1111---- 这样表示有哪种权限时可以用 |(按位或) 操作
- *
- */
- // 用户A有添加和修改权限
- int usera = c | r | u;
- // 用户B有添加和删除权限
- int userb = c | d;
- /**
- * 2222---- 判断用户是否有某种权限用用户权限和要判断的权限进行 &(按位与) 操作,结果为要判断的权限值时表示用户有此权限,否则没有此权限
- */
- if ((usera & u) == u) {
- System.out.println("用户a有更新权限");
- } else {
- System.out.println("用户a没有有更新权限");
- }
- /**
- * 3333---- 给用户添加权限用用户权限和要添加的权限|(按位或) 操作再覆盖之前权限值
- */
- if ((userb & u) == u) {
- System.out.println("用户b有更新权限");
- } else {
- System.out.println("用户b没有更新权限");
- }
- // 给用户b添加更新权限
- userb = userb | u;
- if ((userb & u) == u) {
- System.out.println("用户b有更新权限");
- } else {
- System.out.println("用户b没有更新权限");
- }
- /**
- * 4444---- 取消用户某种权限,用用户权限和要取消的权限按位取反后进行按位 操作,再覆盖之前权限值
- */
- if ((usera & r) == r) {
- System.out.println("用户a有查询权限");
- } else {
- System.out.println("用户a没有查询权限");
- }
- //取消用户a的查询权限
- usera = usera & (~r);
- if ((usera & r) == r) {
- System.out.println("用户a有查询权限");
- } else {
- System.out.println("用户a没有查询权限");
- }
- }
- }
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dawnsun2013/article/details/18141019
边栏推荐
- Create binary tree
- Motor and drive (Qi Jinqing Edition)
- [leetcode 350] intersection of two arrays II
- Basic knowledge of network in cloud computing
- Custom exception class
- SQL -- data definition
- [leetcode 54] spiral matrix
- Storing inherited knowledge in cloud computing
- Definition of C class and method
- POI and easyexcel exercises
猜你喜欢
随机推荐
Common sense of thread pool
scikit-learn sklearn 0.18 官方文档中文版
Plane semi intersecting plate
深拷贝和浅拷贝的区别
Motor and drive (Qi Jinqing Edition)
Rainbow (DP)
A sharp tool to improve work efficiency
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
C # Foundation
小区房价可视化
RPC must know and know
Rust的闭包类型(Fn, FnMut, FnOne的区别)
The problem that the page will refresh automatically after clicking the submit button on the form is solved
Code neat way to learn
word排版遇到的格式问题
8. Integer Decomposition
SQL optimization best practices
PHP processing JSON_ Decode() parses JSON stringify
Qthread simple test understanding
The bottom implementation principle of thread - static agent mode


![[leetcode 54] spiral matrix](/img/c0/9a55a62befb783a5bfc39dc3a96cb2.png)



![[leetcode 202] happy number](/img/b2/a4e65688aef3a0cec8088bcaba048f.jpg)


