org.apache.shiro.authz.permission.WildcardPermission Java Examples

The following examples show how to use org.apache.shiro.authz.permission.WildcardPermission. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuthorizingRealmImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testAuthorization() throws Exception {
  buildTestAuthorizationConfig();

  // Fails because the configuration requirement in nexus authorizing realm isn't initialized
  // thus NPE
  SimplePrincipalCollection principal = new SimplePrincipalCollection("username", realm.getName());

  Assert.assertTrue(realm.hasRole(principal, "role"));

  // Verify the permission
  Assert.assertTrue(realm.isPermitted(principal, new WildcardPermission("app:config:read")));
  // Verify other method not allowed
  Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:config:create")));
  Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:config:update")));
  Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:config:delete")));

  // Verify other permission not allowed
  Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:ui:read")));
  Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:ui:create")));
  Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:ui:update")));
  Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:ui:delete")));
}
 
Example #2
Source File: ResourceService.java    From es with Apache License 2.0 6 votes vote down vote up
private boolean hasPermission(String permission, String actualResourceIdentity) {

        //得到权限字符串中的 资源部分,如a:b:create --->资源是a:b
        String permissionResourceIdentity = permission.substring(0, permission.lastIndexOf(":"));

        //如果权限字符串中的资源 是 以资源为前缀 则有权限 如a:b 具有a:b的权限
        if(permissionResourceIdentity.startsWith(actualResourceIdentity)) {
            return true;
        }


        //模式匹配
        WildcardPermission p1 = new WildcardPermission(permissionResourceIdentity);
        WildcardPermission p2 = new WildcardPermission(actualResourceIdentity);

        return p1.implies(p2) || p2.implies(p1);
    }
 
Example #3
Source File: PermissionServiceImpl.java    From wetech-admin with MIT License 5 votes vote down vote up
private boolean hasPermission(Set<String> permissions, Permission resource) {
    if (StringUtils.isEmpty(resource.getPermission())) {
        return true;
    }
    for (String permission : permissions) {
        WildcardPermission p1 = new WildcardPermission(permission);
        WildcardPermission p2 = new WildcardPermission(resource.getPermission());
        if (p1.implies(p2) || p2.implies(p1)) {
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: UserGroup.java    From airpal with Apache License 2.0 5 votes vote down vote up
public void setPermissions(Set<String> permissions)
{
    ImmutableSet.Builder<Permission> builder = ImmutableSet.builder();
    for (String permission : permissions) {
        builder.add(new WildcardPermission(permission));
    }

    this.permissions = builder.build();
}
 
Example #5
Source File: PermissionFactory.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static Permission createPermission(String permission) {
   if(InstancePermission.isInstancePermission(permission)) {
      return new InstancePermission(permission);
   }
   return new WildcardPermission(permission);
}
 
Example #6
Source File: TestPermissionFactory.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreatesWildcardPermission() {
   Permission permission = PermissionFactory.createPermission("swit:*:*");
   assertTrue(permission instanceof WildcardPermission);
}
 
Example #7
Source File: ScopePermission.java    From seed with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor with a permission
 *
 * @param permission the permission
 */
public ScopePermission(String permission) {
    this.permission = new WildcardPermission(permission);
    this.scope = null;
}
 
Example #8
Source File: ScopePermission.java    From seed with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor with permissions and scope.
 *
 * @param permission the permission
 * @param scope      the scope
 */
public ScopePermission(String permission, Scope scope) {
    this.permission = new WildcardPermission(permission);
    this.scope = scope;
}