Package org.apache.shiro.realm
Class CachingRealm
- java.lang.Object
-
- org.apache.shiro.realm.CachingRealm
-
- All Implemented Interfaces:
LogoutAware
,org.apache.shiro.cache.CacheManagerAware
,Realm
,org.apache.shiro.util.Nameable
- Direct Known Subclasses:
AuthenticatingRealm
public abstract class CachingRealm extends Object implements Realm, org.apache.shiro.util.Nameable, org.apache.shiro.cache.CacheManagerAware, LogoutAware
A very basic abstract extension point for theRealm
interface that provides caching support for subclasses. It also provides a convenience method,getAvailablePrincipal(org.apache.shiro.subject.PrincipalCollection)
, which is useful across all realm subclasses for obtaining a realm-specific principal/identity. All actual Realm method implementations are left to subclasses.
-
-
Constructor Summary
Constructors Constructor Description CachingRealm()
Default no-argument constructor that defaultscachingEnabled
(for general caching) totrue
and sets a defaultname
based on the class name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
afterCacheManagerSet()
Template method that may be implemented by subclasses should they wish to react to aCacheManager
instance being set on the realm instance via thesetCacheManager(org.apache.shiro.cache.CacheManager)
mutator.protected void
clearCache(PrincipalCollection principals)
Clears out any cached data associated with the specified account identity/identities.protected void
doClearCache(PrincipalCollection principals)
This implementation does nothing - it is a template to be overridden by subclasses if necessary.protected Object
getAvailablePrincipal(PrincipalCollection principals)
A utility method for subclasses that returns the first available principal of interest to this particular realm.org.apache.shiro.cache.CacheManager
getCacheManager()
Returns the CacheManager used for data caching to reduce EIS round trips, or null if caching is disabled.String
getName()
Returns the (application-unique) name assigned to thisRealm
.boolean
isCachingEnabled()
void
onLogout(PrincipalCollection principals)
If caching is enabled, this will clear any cached data associated with the specified account identity.void
setCacheManager(org.apache.shiro.cache.CacheManager cacheManager)
Sets the CacheManager to be used for data caching to reduce EIS round trips.void
setCachingEnabled(boolean cachingEnabled)
Sets whether or not caching should be used if aCacheManager
has beenconfigured
.void
setName(String name)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.shiro.realm.Realm
getAuthenticationInfo, supports
-
-
-
-
Constructor Detail
-
CachingRealm
public CachingRealm()
Default no-argument constructor that defaultscachingEnabled
(for general caching) totrue
and sets a defaultname
based on the class name. Note that while in general, caching may be enabled by default, subclasses have control over if specific caching is enabled.
-
-
Method Detail
-
getCacheManager
public org.apache.shiro.cache.CacheManager getCacheManager()
Returns the CacheManager used for data caching to reduce EIS round trips, or null if caching is disabled.- Returns:
- the CacheManager used for data caching to reduce EIS round trips, or null if caching is disabled.
-
setCacheManager
public void setCacheManager(org.apache.shiro.cache.CacheManager cacheManager)
Sets the CacheManager to be used for data caching to reduce EIS round trips. This property is null by default, indicating that caching is turned off.- Specified by:
setCacheManager
in interfaceorg.apache.shiro.cache.CacheManagerAware
- Parameters:
cacheManager
- the CacheManager to use for data caching, or null to disable caching.
-
isCachingEnabled
public boolean isCachingEnabled()
Returnstrue
if caching should be used if aCacheManager
has beenconfigured
,false
otherwise. The default value istrue
since the large majority of Realms will benefit from caching if a CacheManager has been configured. However, memory-only realms should set this value tofalse
since they would manage account data in memory already lookups would already be as efficient as possible.- Returns:
true
if caching will be globally enabled if aCacheManager
has been configured,false
otherwise
-
setCachingEnabled
public void setCachingEnabled(boolean cachingEnabled)
Sets whether or not caching should be used if aCacheManager
has beenconfigured
.- Parameters:
cachingEnabled
- whether or not to globally enable caching for this realm.
-
getName
public String getName()
Description copied from interface:Realm
Returns the (application-unique) name assigned to thisRealm
. All realms configured for a single application must have a unique name.
-
setName
public void setName(String name)
- Specified by:
setName
in interfaceorg.apache.shiro.util.Nameable
-
afterCacheManagerSet
protected void afterCacheManagerSet()
Template method that may be implemented by subclasses should they wish to react to aCacheManager
instance being set on the realm instance via thesetCacheManager(org.apache.shiro.cache.CacheManager)
mutator.
-
onLogout
public void onLogout(PrincipalCollection principals)
If caching is enabled, this will clear any cached data associated with the specified account identity. Subclasses are free to override for additional behavior, but be sure to callsuper.onLogout
first. This default implementation merely callsclearCache(org.apache.shiro.subject.PrincipalCollection)
.- Specified by:
onLogout
in interfaceLogoutAware
- Parameters:
principals
- the application-specific Subject/user identifier that is logging out.- Since:
- 1.2
- See Also:
clearCache(org.apache.shiro.subject.PrincipalCollection)
,getAvailablePrincipal(org.apache.shiro.subject.PrincipalCollection)
-
clearCache
protected void clearCache(PrincipalCollection principals)
Clears out any cached data associated with the specified account identity/identities. This implementation will return quietly if the principals argument is null or empty. Otherwise it delegates todoClearCache(org.apache.shiro.subject.PrincipalCollection)
.- Parameters:
principals
- the principals of the account for which to clear any cached data.- Since:
- 1.2
-
doClearCache
protected void doClearCache(PrincipalCollection principals)
This implementation does nothing - it is a template to be overridden by subclasses if necessary.- Parameters:
principals
- principals the principals of the account for which to clear any cached data.- Since:
- 1.2
-
getAvailablePrincipal
protected Object getAvailablePrincipal(PrincipalCollection principals)
A utility method for subclasses that returns the first available principal of interest to this particular realm. The heuristic used to acquire the principal is as follows:- Attempt to get this particular Realm's 'primary' principal in the
PrincipalCollection
via aprincipals.
call.fromRealm
(getName()
) - If the previous call does not result in any principals, attempt to get the overall 'primary' principal
from the PrincipalCollection via
PrincipalCollection.getPrimaryPrincipal()
. - If there are no principals from that call (or the PrincipalCollection argument was null to begin with),
return
null
- Parameters:
principals
- the PrincipalCollection holding all principals (from all realms) associated with a single Subject.- Returns:
- the 'primary' principal attributed to this particular realm, or the fallback 'master' principal if it
exists, or if not
null
. - Since:
- 1.2
- Attempt to get this particular Realm's 'primary' principal in the
-
-