linux文件權限有3個:java
r | 讀 | 4 |
w | 寫 | 2 |
x | 執行 | 1 |
作web權限控制也能夠參考這樣進行。下面作個demo,有時間實際應用看看。linux
import java.util.ArrayList; import java.util.List; /** * 參考linux下文件的權限 * * @author 登瓊 2016-09-26 18:08 * */ public class PowerSecurity4Linux { /** * 定義權限值 */ List<Power> powerList = new ArrayList<>(); { powerList.add(new Power("查看", 1)); powerList.add(new Power("新增", 2)); powerList.add(new Power("更新", 4)); powerList.add(new Power("刪除", 8)); powerList.add(new Power("上傳導入", 16)); powerList.add(new Power("下載導出", 32)); } public static void main(String[] args) { PowerSecurity4Linux p = new PowerSecurity4Linux(); for (int i = 0; i < 100; i++) { p.showAllPower(i); } } /** * 查詢這個權限結果是否包含相關的權限 * * @param powerValue * 權限所有值 * @param value * 權限 * @return */ public boolean isPower(Integer powerValue, int value) { int a = powerValue / value; if (a == 1) { return true; } if (a == 0) { return false; } int maxPower = 0; int i = 2; while (i <= powerValue) { maxPower = i; i = i << 1; } powerValue = powerValue - maxPower; return isPower(powerValue, value); } /** * 打印這個權限值的全部權限名稱 * * @param powerValue * 權限所有值 */ public void showAllPower(Integer powerValue) { StringBuffer a = new StringBuffer(); a.append(powerValue); a.append(":"); for (Power power : powerList) { if (isPower(powerValue, power.getValue())) { a.append(power.getName()); a.append(","); } } System.out.println(a); } } /** * 基礎的權限 * * @author 登瓊 * */ class Power { String name; int value; public Power(String name, int value) { super(); this.name = name; this.value = value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } }