Shiro中认证的关键对象

Shiro中认证的关键对象

  • Subject:主体

访问系统用户,主体可以是用户、程序,进行认证的都称为主体

  • Principal:身份信息

是主体(Subject)进行身份认证的标识,标识必须具有唯一性,如用户名、手机号、邮箱地址等,一个主体可以有多个身份,但必须有一个主身份(Primary PrinciPal)

  • credential:凭证信息

是只有主体自己知道的安全信息,如密码、证书等。

SimpleAuthorizationInfo -> AuthorizationInfo -> Serializable // 授权

SimpleAuthenticationInfo ->SaltedAuthenticationInfo -> AuthenticationInfo //认证

关于使用登录,先判断是否开启缓存,是,先调用认证的get(), 发现为空,然后查询数据库,set进redis中

这里的key就是用户名,

org.apache.shiro.authc.SimpleAuthenticationInfo cannot be cast to org.apache.shiro.authz.AuthorizationInfo

info = doGetAuthorizationInfo(principals);
// If the info is not null and the cache has been created, then cache the authorization info.
if (info != null && cache != null) {
  if (log.isTraceEnabled()) {
    log.trace("Caching authorization info for principals: [" + principals + "].");
  }
  Object key = getAuthorizationCacheKey(principals);
  cache.put(key, info);
}
Object key = getAuthorizationCacheKey(principals);
info = cache.get(key);
if (log.isTraceEnabled()) {
if (info == null) {
log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]");
} else {
log.trace("AuthorizationInfo found in cache for principals [" + principals + "]");
}
}
private void cacheAuthenticationInfoIfPossible(AuthenticationToken token, AuthenticationInfo info) {
  if (!isAuthenticationCachingEnabled(token, info)) {
    log.debug("AuthenticationInfo caching is disabled for info [{}].  Submitted token: [{}].", info, token);
    //return quietly, caching is disabled for this token/info pair:
    return;
  }

  Cache<Object, AuthenticationInfo> cache = getAvailableAuthenticationCache();
  if (cache != null) {
    Object key = getAuthenticationCacheKey(token);
    cache.put(key, info);
    log.trace("Cached AuthenticationInfo for continued authentication.  key=[{}], value=[{}].", key, info);
  }
}
private AuthenticationInfo getCachedAuthenticationInfo(AuthenticationToken token) {
  AuthenticationInfo info = null;

  Cache<Object, AuthenticationInfo> cache = getAvailableAuthenticationCache();
  if (cache != null && token != null) {
    log.trace("Attempting to retrieve the AuthenticationInfo from cache.");
    Object key = getAuthenticationCacheKey(token);
    info = cache.get(key);
    if (info == null) {
      log.trace("No AuthorizationInfo found in cache for key [{}]", key);
    } else {
      log.trace("Found cached AuthorizationInfo for key [{}]", key);
    }
  }

  return info;
}
上一篇:前端技术笔记系列:React学习笔记(二)


下一篇:React