当前位置:网站首页>Permission management with binary

Permission management with binary

2022-04-23 18:08:00 dawnsun001

Just give an example ! Examples will be explained 1. Permission means 2. Authority judgment 3. Add permissions 4. Cancellation of authority

 

  1. public class Test {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.   
  8.         /** 
  9.          *  Four permissions  , Currently defined as int, The following binary representation takes only the last four digits for explanation  
  10.          */  
  11.   
  12.         //  add to   
  13.         int c = 1;// ...0001  
  14.         //  Inquire about   
  15.         int r = 2;// ...0010  
  16.         //  modify   
  17.         int u = 4;// ...0100  
  18.         //  Delete   
  19.         int d = 8;// ...1000  
  20.   
  21.         /** 
  22.          *  
  23.          *  You can observe the rules of binary representation of four permissions  , All are 2 Of N Power , 
  24.          *  It means itself , The last bit of add permission is... Others 0, The penultimate digit of the query is 1 Everything else is 0, Change the penultimate to 1 Everything else is 0, Delete the penultimate as 1 Everything else is 0 
  25.          *  
  26.          */  
  27.   
  28.         /** 
  29.          *1111----  This indicates what kind of permission you have. You can use  |( Press bit or )  operation  
  30.          *  
  31.          */  
  32.   
  33.         //  user A Have the right to add and modify   
  34.         int usera = c | r | u;  
  35.   
  36.         //  user B Have the right to add and delete   
  37.         int userb = c | d;  
  38.   
  39.         /** 
  40.          * 2222----  To judge whether a user has certain permissions, use the user permissions and the permissions to be judged  &( Bitwise AND )  operation , When the result is the permission value to be judged, it means that the user has this permission , Otherwise, you don't have this permission  
  41.          */  
  42.   
  43.         if ((usera & u) == u) {  
  44.             System.out.println(" user a Have update permission ");  
  45.         } else {  
  46.             System.out.println(" user a No update permission ");  
  47.         }  
  48.   
  49.         /** 
  50.          * 3333----  Add permissions to users. Use user permissions and permissions to be added |( Press bit or )  The operation overwrites the previous permission value  
  51.          */  
  52.   
  53.         if ((userb & u) == u) {  
  54.             System.out.println(" user b Have update permission ");  
  55.         } else {  
  56.             System.out.println(" user b No update permission ");  
  57.         }  
  58.   
  59.         //  To the user b Add update permissions   
  60.         userb = userb | u;  
  61.   
  62.         if ((userb & u) == u) {  
  63.             System.out.println(" user b Have update permission ");  
  64.         } else {  
  65.             System.out.println(" user b No update permission ");  
  66.         }  
  67.   
  68.         /** 
  69.          * 4444----  Cancel a user's permission , Use the user authority and the authority to be cancelled to reverse by bit, and then carry out by bit   operation , Overwrite the previous permission value  
  70.          */  
  71.           
  72.         if ((usera & r) == r) {  
  73.             System.out.println(" user a Have query authority ");  
  74.         } else {  
  75.             System.out.println(" user a No query permission ");  
  76.         }  
  77.           
  78.         // Cancel user a The query authority of   
  79.         usera = usera & (~r);  
  80.           
  81.         if ((usera & r) == r) {  
  82.             System.out.println(" user a Have query authority ");  
  83.         } else {  
  84.             System.out.println(" user a No query permission ");  
  85.         }  
  86.     }  
  87.   

版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544289337.html