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

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

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

Subject.hasRole介绍

[英]Returns true if this Subject has the specified role, false otherwise.
[中]如果此主题具有指定的角色,则返回true,否则返回false。

代码示例

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

@Override
public boolean isUserInRole(String role) {
  LOG.debug("Checking role {} for user {}.", role, subject.getPrincipal());
  return subject.hasRole(role);
}

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

@Override
public boolean isUserInRole(String role) {
  return getSubject().hasRole(role);
}

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

public static boolean isServiceAdmin() {
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  return currentUser.hasRole( ROLE_SERVICE_ADMIN );
}

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

public static boolean isApplicationUser() {
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  return currentUser.hasRole( ROLE_APPLICATION_USER );
}

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

protected boolean showTagBody(String roleNames) {
  boolean hasAnyRole = false;
  Subject subject = getSubject();
  if (subject != null) {
    // Iterate through roles and check to see if the user has one of the roles
    for (String role : roleNames.split(ROLE_NAMES_DELIMETER)) {
      if (subject.hasRole(role.trim())) {
        hasAnyRole = true;
        break;
      }
    }
  }
  return hasAnyRole;
}

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

/**
 * 验证当前用户是否属于该角色?,使用时与lacksRole 搭配使用
 *
 * @param roleName 角色名
 * @return 属于该角色:true,否则false
 */
public static boolean hasRole(String roleName) {
  return getSubject() != null && roleName != null
      && roleName.length() > 0 && getSubject().hasRole(roleName);
}

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

/**
 * 验证当前用户是否属于该角色?,使用时与lacksRole 搭配使用
 *
 * @param roleName 角色名
 * @return 属于该角色:true,否则false
 */
public boolean hasRole(String roleName) {
  return getSubject() != null && roleName != null
      && roleName.length() > 0 && getSubject().hasRole(roleName);
}

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

public boolean isUserInRole(String s) {
  Subject subject = getSubject();
  boolean inRole = (subject != null && subject.hasRole(s));
  if (!inRole) {
    inRole = super.isUserInRole(s);
  }
  return inRole;
}

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

protected boolean showTagBody(String roleName) {
  boolean hasRole = getSubject() != null && getSubject().hasRole(roleName);
  return !hasRole;
}

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

protected boolean showTagBody(String roleName) {
  return getSubject() != null && getSubject().hasRole(roleName);
}

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

public static boolean isApplicationAdmin() {
  if ( isServiceAdmin() ) {
    return true;
  }
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  boolean admin = currentUser.hasRole( ROLE_APPLICATION_ADMIN );
  return admin;
}

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

public static boolean isOrganizationAdmin() {
  if ( isServiceAdmin() ) {
    return true;
  }
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  return currentUser.hasRole( ROLE_ORGANIZATION_ADMIN );
}

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

public static boolean isAdminUser() {
  if ( isServiceAdmin() ) {
    return true;
  }
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return false;
  }
  return currentUser.hasRole( ROLE_ADMIN_USER );
}

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

@SuppressWarnings( "unchecked" )
public static BiMap<UUID, String> getApplications() {
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return null;
  }
  if ( !currentUser.hasRole( ROLE_APPLICATION_ADMIN ) && !currentUser.hasRole( ROLE_APPLICATION_USER ) ) {
    return null;
  }
  Session session = currentUser.getSession();
  BiMap<UUID, String> applications = HashBiMap.create();
  Map map = (Map)session.getAttribute( "applications" );
  applications.putAll(map);
  return applications;
}

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

public static Set<String> getOrganizationNames() {
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return null;
  }
  if ( !currentUser.hasRole( ROLE_ORGANIZATION_ADMIN ) ) {
    return null;
  }
  BiMap<UUID, String> organizations = getOrganizations();
  if ( organizations == null ) {
    return null;
  }
  return organizations.inverse().keySet();
}

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

public static OrganizationInfo getOrganization() {
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return null;
  }
  if ( !currentUser.hasRole( ROLE_ORGANIZATION_ADMIN ) ) {
    return null;
  }
  Session session = currentUser.getSession();
  OrganizationInfo organization = ( OrganizationInfo ) session.getAttribute( "organization" );
  return organization;
}

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

@Test
public void testRoleNotPresent() throws Exception {
  String testMethodName = name.getMethodName();
  JdbcRealm realm = realmMap.get(testMethodName);
  createDefaultSchema(testMethodName, false);
  realm.setSaltStyle(JdbcRealm.SaltStyle.NO_SALT);
  
  Subject.Builder builder = new Subject.Builder(securityManager);
  Subject currentUser = builder.buildSubject();
  UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);
  currentUser.login(token);
  Assert.assertFalse(currentUser.hasRole("Game Overall Director"));
}

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

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

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

@Test
public void testDefaultConfig() {
  String localhost = "localhost";
  Subject subject = SecurityUtils.getSubject();
  subject.login(new UsernamePasswordToken(USERNAME, PASSWORD, localhost));
  assertTrue(subject.isAuthenticated());
  assertTrue(subject.hasRole(ROLE));
  UsernamePrincipal usernamePrincipal = subject.getPrincipals().oneByType(UsernamePrincipal.class);
  assertTrue(usernamePrincipal.getUsername().equals(USERNAME));
  UserIdPrincipal userIdPrincipal = subject.getPrincipals().oneByType(UserIdPrincipal.class);
  assertTrue(userIdPrincipal.getUserId() == USER_ID);
  assertTrue(realm.hasRole(subject.getPrincipals(), ROLE));
  subject.logout();
}

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

@Test
public void testDefaultConfig() {
  Subject subject = SecurityUtils.getSubject();
  AuthenticationToken token = new UsernamePasswordToken("guest", "guest");
  subject.login(token);
  assertTrue(subject.isAuthenticated());
  assertTrue("guest".equals(subject.getPrincipal()));
  assertTrue(subject.hasRole("guest"));
  Session session = subject.getSession();
  session.setAttribute("key", "value");
  assertEquals(session.getAttribute("key"), "value");
  subject.logout();
  assertNull(subject.getSession(false));
  assertNull(subject.getPrincipal());
  assertNull(subject.getPrincipals());
}

相关文章