CachingRealm抽象类

CachingRealm抽象类主要是用来做缓存realm的,它实现了Realm,Nameable,CacheManagerAware,LogoutAware接口,先对其解析如下:

1.Realm接口

可以参考Realm接口源码解析,主要有获取名字,是否支持当前token,获取认证信息方法。

2.Nameable接口

可以参考Nameable接口源码解析,只有一个设置名字的方法。

3.CacheManagerAware接口

可以参考CacheManagerAware接口源码解析,只有一个设置缓存管理器的方法。

4.LogoutAware接口

可以参考LogoutAware接口,只有一个退出的方法。

5.CachingRealm抽象类

5.1.数据属性

扫描二维码关注公众号,回复: 276949 查看本文章

private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger();//实例的数目

private String name;//realm名字
private boolean cachingEnabled;//缓存是否可用
private CacheManager cacheManager;//缓存管理器

5.2.构造方法(设置为可缓存,并且把当前的名字记录为类名+实例的数目)

public CachingRealm() {
        this.cachingEnabled = true;
        this.name = getClass().getName() + "_" + INSTANCE_COUNT.getAndIncrement();
}

5.3.获取缓存管理器

 public CacheManager getCacheManager() {
        return this.cacheManager;
}

5.4.设置缓存管理器(实现了CacheManagerAware的方法)

public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
        afterCacheManagerSet();
    }

5.5.返回缓存是否可用

public boolean isCachingEnabled() {
        return cachingEnabled;
}

5.6.设置缓存是否可用

public void setCachingEnabled(boolean cachingEnabled) {
        this.cachingEnabled = cachingEnabled;
}

5.7.获取realm名称(实现了Realm接口的方法)

public String getName() {
        return name;
}

5.8设置realm名称(实现了Nameable接口的方法)

public void setName(String name) {
        this.name = name;
}

5.9.设置完缓存管理器之后的操作

protected void afterCacheManagerSet() {
}

5.10.退出时的操作(实现了LogoutAware接口的方法)

public void onLogout(PrincipalCollection principals) {
        clearCache(principals);
}

5.11.清空当前身份在cache中的信息

protected void clearCache(PrincipalCollection principals) {
        if (!CollectionUtils.isEmpty(principals)) {
            doClearCache(principals);
            log.trace("Cleared cache entries for account with principals [{}]", principals);
        }
    }

5.12.清空当前身份在cache中的信息

protected void doClearCache(PrincipalCollection principals) {
}

5.13.获取可用的身份信息

protected Object getAvailablePrincipal(PrincipalCollection principals) {
        Object primary = null;
        if (!CollectionUtils.isEmpty(principals)) {
            Collection thisPrincipals = principals.fromRealm(getName());
            if (!CollectionUtils.isEmpty(thisPrincipals)) {
                primary = thisPrincipals.iterator().next();
            } else {
                //no principals attributed to this particular realm.  Fall back to the 'master' primary:
                primary = principals.getPrimaryPrincipal();
            }
        }

        return primary;
    }

猜你喜欢

转载自yansxjl.iteye.com/blog/2333997