org.apache.shiro.subject.Subject.isPermitted()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(247)

本文整理了Java中org.apache.shiro.subject.Subject.isPermitted()方法的一些代码示例,展示了Subject.isPermitted()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Subject.isPermitted()方法的具体详情如下:
包路径:org.apache.shiro.subject.Subject
类名称:Subject
方法名:isPermitted

Subject.isPermitted介绍

[英]Returns true if this Subject is permitted to perform an action or access a resource summarized by the specified permission string.

This is an overloaded method for the corresponding type-safe Permission variant. Please see the class-level JavaDoc for more information on these String-based permission methods.
[中]如果允许此主题执行操作或访问由指定权限字符串汇总的资源,则返回true。
这是对应类型安全权限变量的重载方法。有关这些基于字符串的权限方法的更多信息,请参阅类级JavaDoc。

代码示例

代码示例来源:origin: Graylog2/graylog2-server

protected boolean isAnyPermitted(String... permissions) {
  final boolean[] permitted = getSubject().isPermitted(permissions);
  for (boolean p : permitted) {
    if (p) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: apache/shiro

public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {

    Subject subject = getSubject(request, response);
    String[] perms = (String[]) mappedValue;

    boolean isPermitted = true;
    if (perms != null && perms.length > 0) {
      if (perms.length == 1) {
        if (!subject.isPermitted(perms[0])) {
          isPermitted = false;
        }
      } else {
        if (!subject.isPermittedAll(perms)) {
          isPermitted = false;
        }
      }
    }

    return isPermitted;
  }
}

代码示例来源:origin: Graylog2/graylog2-server

protected boolean isPermitted(String permission) {
  return getSubject().isPermitted(permission);
}

代码示例来源:origin: Graylog2/graylog2-server

protected boolean isPermitted(String permission, String instanceId) {
  return getSubject().isPermitted(permission + ":" + instanceId);
}

代码示例来源:origin: stylefeng/Guns

/**
 * 验证当前用户是否拥有指定权限,使用时与lacksPermission 搭配使用
 *
 * @param permission 权限名
 * @return 拥有权限:true,否则false
 */
public boolean hasPermission(String permission) {
  return getSubject() != null && permission != null
      && permission.length() > 0
      && getSubject().isPermitted(permission);
}

代码示例来源:origin: killbill/killbill

@Override
public Set<Permission> getCurrentUserPermissions(final TenantContext context) {
  final Permission[] killbillPermissions = Permission.values();
  final String[] killbillPermissionsString = getAllPermissionsAsStrings();
  final Subject subject = SecurityUtils.getSubject();
  // Bulk (optimized) call
  final boolean[] permissions = subject.isPermitted(killbillPermissionsString);
  final Set<Permission> userPermissions = new HashSet<Permission>();
  for (int i = 0; i < permissions.length; i++) {
    if (permissions[i]) {
      userPermissions.add(killbillPermissions[i]);
    }
  }
  return userPermissions;
}

代码示例来源:origin: stylefeng/Guns

/**
 * 验证当前用户是否拥有指定权限,使用时与lacksPermission 搭配使用
 *
 * @param permission 权限名
 * @return 拥有权限:true,否则false
 */
public static boolean hasPermission(String permission) {
  return getSubject() != null && permission != null
      && permission.length() > 0
      && getSubject().isPermitted(permission);
}

代码示例来源:origin: apache/shiro

protected boolean isPermitted(String p) {
  return getSubject() != null && getSubject().isPermitted(p);
}

代码示例来源:origin: apache/usergrid

public static boolean isPermittedAccessToUser( UUID userId ) {
  if ( isServiceAdmin() ) {
    return true;
  }
  if ( userId == null ) {
    return false;
  }
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  return currentUser.isPermitted( "users:access:" + userId );
}

代码示例来源:origin: apache/usergrid

public void checkPermissionsForPath( ServiceContext context, String path ) {
  Subject currentUser = SubjectUtils.getSubject();
  if ( currentUser == null ) {
    return;
  }
  if( isServiceAdmin() ){
    if(logger.isDebugEnabled()){
      logger.debug("Subject is the sysadmin, short-circuiting and allowing access");
    }
    return;
  }
  String perm = getPermissionFromPath(
    em.getApplicationRef().getUuid(), context.getAction().toString().toLowerCase(), path );
  boolean permitted = currentUser.isPermitted( perm );
  if ( logger.isDebugEnabled() ) {
    logger.debug( PATH_MSG, path, context.getAction(), perm, permitted );
  }
  SubjectUtils.checkPermission( perm );
  Subject subject = SubjectUtils.getSubject();
  if (logger.isDebugEnabled()) {
    logger.debug("Checked subject {} for perm {}", subject != null ? subject.toString() : "", perm);
    logger.debug("------------------------------------------------------------------------------");
  }
}

代码示例来源:origin: killbill/killbill

@Override
public void checkCurrentUserPermissions(final List<Permission> permissions, final Logical logical, final TenantContext context) throws SecurityApiException {
  final String[] permissionsString = Lists.<Permission, String>transform(permissions, Functions.toStringFunction()).toArray(new String[permissions.size()]);
  try {
    final Subject subject = SecurityUtils.getSubject();
    if (permissionsString.length == 1) {
      subject.checkPermission(permissionsString[0]);
    } else if (Logical.AND.equals(logical)) {
      subject.checkPermissions(permissionsString);
    } else if (Logical.OR.equals(logical)) {
      boolean hasAtLeastOnePermission = false;
      for (final String permission : permissionsString) {
        if (subject.isPermitted(permission)) {
          hasAtLeastOnePermission = true;
          break;
        }
      }
      // Cause the exception if none match
      if (!hasAtLeastOnePermission) {
        subject.checkPermission(permissionsString[0]);
      }
    }
  } catch (final AuthorizationException e) {
    throw new SecurityApiException(e, ErrorCode.SECURITY_NOT_ENOUGH_PERMISSIONS);
  }
}

代码示例来源:origin: Graylog2/graylog2-server

if (!getSubject().isPermitted(RestPermissions.USERS_PASSWORDCHANGE + ":" + user.getName())) {
  throw new ForbiddenException("Not allowed to change password for user " + username);
if (getSubject().isPermitted(RestPermissions.USERS_PASSWORDCHANGE + ":*")) {
  if (username.equals(getSubject().getPrincipal())) {
    LOG.debug("User {} is allowed to change the password of any user, but attempts to change own password. Must supply the old password.", getSubject().getPrincipal());

代码示例来源:origin: apache/shiro

if (currentUser.isPermitted("lightsaber:weild")) {
  log.info("You may use a lightsaber ring.  Use it wisely.");
} else {
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
  log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
      "Here are the keys - have fun!");

代码示例来源:origin: apache/usergrid

public static boolean isPermittedAccessToOrganization( Identifier identifier ) {
  if ( isServiceAdmin() ) {
    return true;
  }
  OrganizationInfo organization = getOrganization( identifier );
  if ( organization == null ) {
    return false;
  }
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  return currentUser.isPermitted( "organizations:access:" + organization.getUuid() );
}

代码示例来源:origin: apache/usergrid

public static boolean isPermittedAccessToApplication( Identifier identifier ) {
  if ( isServiceAdmin() ) {
    return true;
  }
  ApplicationInfo application = getApplication( identifier );
  if ( application == null ) {
    return false;
  }
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  return currentUser.isPermitted( "applications:access:" + application.getId() );
}

代码示例来源:origin: apache/usergrid

public static boolean isApplicationAdmin( Identifier identifier ) {
  if ( isServiceAdmin() ) {
    return true;
  }
  ApplicationInfo application = getApplication( identifier );
  if ( application == null ) {
    return false;
  }
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  return currentUser.isPermitted( "applications:admin:" + application.getId() );
}

代码示例来源:origin: apache/shiro

if (currentUser.isPermitted("lightsaber:wield")) {
  log.info("You may use a lightsaber ring.  Use it wisely.");
} else {
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
  log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
      "Here are the keys - have fun!");

代码示例来源:origin: apache/shiro

@Test
public void testPermissionPresent() throws Exception {
  String testMethodName = name.getMethodName();
  JdbcRealm realm = realmMap.get(testMethodName);
  createDefaultSchema(testMethodName, false);
  realm.setSaltStyle(JdbcRealm.SaltStyle.NO_SALT);
  realm.setPermissionsLookupEnabled(true);
  
  Subject.Builder builder = new Subject.Builder(securityManager);
  Subject currentUser = builder.buildSubject();
  UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);
  currentUser.login(token);
  Assert.assertTrue(currentUser.isPermitted(testPermissionString));
}

代码示例来源:origin: apache/shiro

@Test
public void testPermissionNotPresent() throws Exception {
  String testMethodName = name.getMethodName();
  JdbcRealm realm = realmMap.get(testMethodName);
  createDefaultSchema(testMethodName, false);
  realm.setSaltStyle(JdbcRealm.SaltStyle.NO_SALT);
  realm.setPermissionsLookupEnabled(true);
  
  Subject.Builder builder = new Subject.Builder(securityManager);
  Subject currentUser = builder.buildSubject();
  UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);
  currentUser.login(token);
  Assert.assertFalse(currentUser.isPermitted("testDomain:testTarget:specialAction"));
}

代码示例来源:origin: apache/shiro

for (String permission : perms) if (getSubject().isPermitted(permission)) hasAtLeastOnePermission = true;

相关文章