1 shiro受權數據庫
// 基於資源受權ide
System.out.println("是否擁有某一個權限:" + subject.isPermitted("user:delete"));spa
System.out.println("是否擁有多個權限:" + subject.isPermittedAll("user:create:1", "user:delete"));ip
對應的check方法:ci
subject.checkPermission("sys:user:delete");資源
subject.checkPermissions("user:create:1","user:delete");字符串
// 受權get
@Overrideit
protected AuthorizationInfo doGetAuthorizationInfo(io
PrincipalCollection principals) {
// 獲取身份信息
String username = (String) principals.getPrimaryPrincipal();
// 根據身份信息從數據庫中查詢權限數據
//....這裏使用靜態數據模擬
List<String> permissions = new ArrayList<String>();
permissions.add("user:create");
permissions.add("user.delete");
//將權限信息封閉爲AuthorizationInfo
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
for(String permission:permissions){
simpleAuthorizationInfo.addStringPermission(permission);
}
return simpleAuthorizationInfo;
}
權限字符串的規則是:「資源表示符:操做:資源實力標識符」,意思是對哪一個資源的哪一個實例具備什麼操做,「:」是資源/操做/實例的分隔符,權限字符串也可使用【*】通配符
例如:
用戶建立權限:user:create 活 user:create:*
用戶修改實例001的權限:user:update:001
用戶實例001的全部權限:user:*:001