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

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

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

Subject.getPrincipals介绍

[英]Returns this Subject's principals (identifying attributes) in the form of a PrincipalCollection or null if this Subject is anonymous because it doesn't yet have any associated account data (for example, if they haven't logged in).

The word "principals" is nothing more than a fancy security term for identifying attributes associated with a Subject, aka, application user. For example, user id, a surname (family/last name), given (first) name, social security number, nickname, username, etc, are all examples of a principal.
[中]

代码示例

代码示例来源:origin: wuyouzhuguli/FEBS-Shiro

/**
 * 清除权限缓存
 * 使用方法:在需要清除用户权限的地方注入 ShiroRealm,
 * 然后调用其clearCache方法。
 */
public void clearCache() {
  PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
  super.clearCache(principals);
}

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

@SuppressWarnings({"unchecked"})
private Object getPrincipalFromClassName() {
  Object principal = null;
  try {
    Class cls = Class.forName(type);
    principal = getSubject().getPrincipals().oneByType(cls);
  } catch (ClassNotFoundException e) {
    if (log.isErrorEnabled()) {
      log.error("Unable to find class for name [" + type + "]");
    }
  }
  return principal;
}

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

protected void rememberMeLogout(Subject subject) {
  RememberMeManager rmm = getRememberMeManager();
  if (rmm != null) {
    try {
      rmm.onLogout(subject);
    } catch (Exception e) {
      if (log.isWarnEnabled()) {
        String msg = "Delegate RememberMeManager instance of type [" + rmm.getClass().getName() +
            "] threw an exception during onLogout for subject with principals [" +
            (subject != null ? subject.getPrincipals() : null) + "]";
        log.warn(msg, e);
      }
    }
  }
}

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

@RequiresRoles("admin")
@RequestMapping("/account-info")
public String home(Model model) {
  String name = "World";
  Subject subject = SecurityUtils.getSubject();
  PrincipalCollection principalCollection = subject.getPrincipals();
  if (principalCollection != null && !principalCollection.isEmpty()) {
    name = principalCollection.getPrimaryPrincipal().toString();
  }
  model.addAttribute("name", name);
  return "account-info";
}

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

@SuppressWarnings("Duplicates")
@RequestMapping("/")
public String home(HttpServletRequest request, Model model) {
  String name = "World";
  Subject subject = SecurityUtils.getSubject();
  PrincipalCollection principalCollection = subject.getPrincipals();
  if (principalCollection != null && !principalCollection.isEmpty()) {
    Collection<Map> principalMaps = subject.getPrincipals().byType(Map.class);
    if (CollectionUtils.isEmpty(principalMaps)) {
      name = subject.getPrincipal().toString();
    }
    else {
      name = (String) principalMaps.iterator().next().get("username");
    }
  }
  model.addAttribute("name", name);
  return "hello";
}

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

/**
 * 获取封装的 ShiroUser
 *
 * @return ShiroUser
 */
public static ShiroUser getUser() {
  if (isGuest()) {
    return null;
  } else {
    return (ShiroUser) getSubject().getPrincipals().getPrimaryPrincipal();
  }
}

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

/**
 * 获取封装的 ShiroUser
 *
 * @return ShiroUser
 */
public ShiroUser getUser() {
  if (isGuest()) {
    return null;
  } else {
    return (ShiroUser) getSubject().getPrincipals().getPrimaryPrincipal();
  }
}

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

/**
 * 获取ShiroUser,不为空的
 *
 * @return ShiroUser
 */
public static ShiroUser getUserNotNull() {
  if (isGuest()) {
    throw new ServiceException(BizExceptionEnum.NOT_LOGIN);
  } else {
    return (ShiroUser) getSubject().getPrincipals().getPrimaryPrincipal();
  }
}

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

PrincipalCollection principals = subject.getPrincipals();
if (principals != null && !principals.isEmpty()) {
  if (log.isDebugEnabled()) {

代码示例来源:origin: 527515025/springBoot

@Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    logger.info("doGetAuthorizationInfo+"+principalCollection.toString());
    User user = userService.getByUserName((String) principalCollection.getPrimaryPrincipal());

    //把principals放session中 key=userId value=principals
    SecurityUtils.getSubject().getSession().setAttribute(String.valueOf(user.getId()),SecurityUtils.getSubject().getPrincipals());

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //赋予角色
    for(Role userRole:user.getRoles()){
      info.addRole(userRole.getName());
    }
    //赋予权限
    for(Permission permission:permissionService.getByUserId(user.getId())){
//            if(StringUtils.isNotBlank(permission.getPermCode()))
        info.addStringPermission(permission.getName());
    }

    //设置登录次数、时间
//        userService.updateUserLogin(user);
    return info;
  }

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

@Override
public Principal getUserPrincipal() {
  Principal result;
  Subject subject = getSubject();
  PrincipalCollection shiroPrincipals = subject.getPrincipals();
  if (shiroPrincipals != null) {
    result = shiroPrincipals.oneByType(Principal.class);
    if (result == null) {
      result = new ObjectPrincipal(shiroPrincipals.getPrimaryPrincipal());
    }
  }
  else {
    result = originalSecurityContext.getUserPrincipal();
  }
  return result;
}

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

host = delegating.host;
} else {
  principals = subject.getPrincipals();

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

currentPrincipals = subject.getPrincipals();

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

@Override
protected void saveToSession(final Subject subject) {
  boolean updatesDisabled = false;
  Session session = subject.getSession(false);
  if (session == null && !CollectionUtils.isEmpty(subject.getPrincipals())) {
    // Force the creation of the session here to get the id
    session = subject.getSession();
    // Optimize the session creation path: the default saveToSession implementation
    // will call setAttribute() several times in a row, causing unnecessary DAO UPDATE queries
    updatesDisabled = disableUpdatesForSession(subject, session);
  }
  super.saveToSession(subject);
  if (updatesDisabled) {
    enableUpdatesForSession(subject, session);
  }
}

代码示例来源: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

public PrincipalCollection resolvePrincipals() {
  PrincipalCollection principals = getPrincipals();
  if (isEmpty(principals)) {
    //check to see if they were just authenticated:
    AuthenticationInfo info = getAuthenticationInfo();
    if (info != null) {
      principals = info.getPrincipals();
    }
  }
  if (isEmpty(principals)) {
    Subject subject = getSubject();
    if (subject != null) {
      principals = subject.getPrincipals();
    }
  }
  if (isEmpty(principals)) {
    //try the session:
    Session session = resolveSession();
    if (session != null) {
      principals = (PrincipalCollection) session.getAttribute(PRINCIPALS_SESSION_KEY);
    }
  }
  return principals;
}

代码示例来源: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());
}

代码示例来源:origin: org.apache.shiro/shiro-core

protected void rememberMeLogout(Subject subject) {
  RememberMeManager rmm = getRememberMeManager();
  if (rmm != null) {
    try {
      rmm.onLogout(subject);
    } catch (Exception e) {
      if (log.isWarnEnabled()) {
        String msg = "Delegate RememberMeManager instance of type [" + rmm.getClass().getName() +
            "] threw an exception during onLogout for subject with principals [" +
            (subject != null ? subject.getPrincipals() : null) + "]";
        log.warn(msg, e);
      }
    }
  }
}

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

assertNull(subject.getPrincipals());
assertNull(subject.getPrincipals());

代码示例来源:origin: org.apache.shiro/shiro-core

public PrincipalCollection resolvePrincipals() {
  PrincipalCollection principals = getPrincipals();
  if (isEmpty(principals)) {
    //check to see if they were just authenticated:
    AuthenticationInfo info = getAuthenticationInfo();
    if (info != null) {
      principals = info.getPrincipals();
    }
  }
  if (isEmpty(principals)) {
    Subject subject = getSubject();
    if (subject != null) {
      principals = subject.getPrincipals();
    }
  }
  if (isEmpty(principals)) {
    //try the session:
    Session session = resolveSession();
    if (session != null) {
      principals = (PrincipalCollection) session.getAttribute(PRINCIPALS_SESSION_KEY);
    }
  }
  return principals;
}

相关文章