Class AuthorizingRealm
- All Implemented Interfaces:
LogoutAware,Authorizer,PermissionResolverAware,RolePermissionResolverAware,org.apache.shiro.cache.CacheManagerAware,org.apache.shiro.lang.util.Initializable,org.apache.shiro.lang.util.Nameable,Realm
- Direct Known Subclasses:
AbstractLdapRealm,DefaultLdapRealm,JdbcRealm,SimpleAccountRealm
AuthorizingRealm extends the AuthenticatingRealm's capabilities by adding Authorization
(access control) support.
This implementation will perform all role and permission checks automatically (and subclasses do not have to
write this logic) as long as the
getAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection) method returns an
AuthorizationInfo. Please see that method's JavaDoc for an in-depth explanation.
If you find that you do not want to utilize the AuthorizationInfo construct,
you are of course free to subclass the AuthenticatingRealm directly instead and
implement the remaining Realm interface methods directly. You might do this if you want to have better control
over how the Role and Permission checks occur for your specific data source. However, using AuthorizationInfo
(and its default implementation SimpleAuthorizationInfo)
is sufficient in the large
majority of Realm cases.- Since:
- 0.2
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionAuthorizingRealm(CredentialsMatcher matcher) AuthorizingRealm(org.apache.shiro.cache.CacheManager cacheManager) AuthorizingRealm(org.apache.shiro.cache.CacheManager cacheManager, CredentialsMatcher matcher) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidThis implementation attempts to acquire an authentication cache if one is not already configured.protected voidcheckPermission(Permission permission, AuthorizationInfo info) voidcheckPermission(PrincipalCollection subjectIdentifier, String permission) Ensures the corresponding Subject/user implies the specified permission String.voidcheckPermission(PrincipalCollection principal, Permission permission) Ensures a subject/userPermission.implies(Permission)implies} the specified Permission.protected voidcheckPermissions(Collection<Permission> permissions, AuthorizationInfo info) voidcheckPermissions(PrincipalCollection subjectIdentifier, String... permissions) Ensures the corresponding Subject/userimpliesall of the specified permission strings.voidcheckPermissions(PrincipalCollection principal, Collection<Permission> permissions) Ensures the corresponding Subject/userimpliesall of the specified permission strings.protected voidcheckRole(String role, AuthorizationInfo info) voidcheckRole(PrincipalCollection principal, String role) Asserts the corresponding Subject/user has the specified role by returning quietly if they do or throwing anAuthorizationExceptionif they do not.protected voidcheckRoles(Collection<String> roles, AuthorizationInfo info) voidcheckRoles(PrincipalCollection principal, String... roles) Same ascheckRoles(PrincipalCollection subjectPrincipal, Collection<String> roleIdentifiers)but doesn't require a collection as an argument.voidcheckRoles(PrincipalCollection principal, Collection<String> roles) Asserts the corresponding Subject/user has all of the specified roles by returning quietly if they do or throwing anAuthorizationExceptionif they do not.protected voidclearCachedAuthorizationInfo(PrincipalCollection principals) Clears out the AuthorizationInfo cache entry for the specified account.protected voiddoClearCache(PrincipalCollection principals) Callssuper.doClearCacheto ensure any cached authentication data is removed and then callsclearCachedAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)to remove any cached authorization data.protected abstract AuthorizationInfodoGetAuthorizationInfo(PrincipalCollection principals) Retrieves the AuthorizationInfo for the given principals from the underlying data store.org.apache.shiro.cache.Cache<Object, AuthorizationInfo> protected ObjectgetAuthorizationCacheKey(PrincipalCollection principals) protected AuthorizationInfogetAuthorizationInfo(PrincipalCollection principals) Returns an account's authorization-specific information for the specifiedprincipals, ornullif no account could be found.protected Collection<Permission> booleanhasAllRoles(PrincipalCollection principal, Collection<String> roleIdentifiers) Returns true if the corresponding Subject/user has all of the specified roles, false otherwise.protected booleanhasRole(String roleIdentifier, AuthorizationInfo info) booleanhasRole(PrincipalCollection principal, String roleIdentifier) Returns true if the corresponding Subject/user has the specified role, false otherwise.protected boolean[]hasRoles(List<String> roleIdentifiers, AuthorizationInfo info) boolean[]hasRoles(PrincipalCollection principal, List<String> roleIdentifiers) Checks if the corresponding Subject/user has the specified roles, returning a boolean array indicating which roles are associated with the given subject.booleanReturnstrueif authorization caching should be utilized if aCacheManagerhas beenconfigured,falseotherwise.protected boolean[]isPermitted(List<Permission> permissions, AuthorizationInfo info) protected booleanisPermitted(Permission permission, AuthorizationInfo info) booleanisPermitted(PrincipalCollection principals, String permission) Returns true if the corresponding subject/user is permitted to perform an action or access a resource summarized by the specified permission string.boolean[]isPermitted(PrincipalCollection subjectIdentifier, String... permissions) Checks if the corresponding Subject implies the given permission strings and returns a boolean array indicating which permissions are implied.boolean[]isPermitted(PrincipalCollection principals, List<Permission> permissions) Checks if the corresponding Subject/user implies the given Permissions and returns a boolean array indicating which permissions are implied.booleanisPermitted(PrincipalCollection principals, Permission permission) Returns true if the corresponding subject/user is permitted to perform an action or access a resource summarized by the specified permission.protected booleanisPermittedAll(Collection<Permission> permissions, AuthorizationInfo info) booleanisPermittedAll(PrincipalCollection subjectIdentifier, String... permissions) Returns true if the corresponding Subject/user implies all of the specified permission strings, false otherwise.booleanisPermittedAll(PrincipalCollection principal, Collection<Permission> permissions) Returns true if the corresponding Subject/user implies all of the specified permissions, false otherwise.protected voidonInit()Initializes this realm and potentially enables a cache, depending on configuration.voidsetAuthorizationCache(org.apache.shiro.cache.Cache<Object, AuthorizationInfo> authorizationCache) voidsetAuthorizationCacheName(String authorizationCacheName) voidsetAuthorizationCachingEnabled(boolean authorizationCachingEnabled) Sets whether or not authorization caching should be utilized if aCacheManagerhas beenconfigured,falseotherwise.voidvoidsetPermissionResolver(PermissionResolver permissionResolver) Sets the specified PermissionResolver on this instance.voidsetRolePermissionResolver(RolePermissionResolver permissionRoleResolver) Sets the specified RolePermissionResolver on this instance.Methods inherited from class org.apache.shiro.realm.AuthenticatingRealm
assertCredentialsMatch, clearCachedAuthenticationInfo, doGetAuthenticationInfo, getAuthenticationCache, getAuthenticationCacheKey, getAuthenticationCacheKey, getAuthenticationCacheName, getAuthenticationInfo, getAuthenticationTokenClass, getCredentialsMatcher, init, isAuthenticationCachingEnabled, isAuthenticationCachingEnabled, setAuthenticationCache, setAuthenticationCacheName, setAuthenticationCachingEnabled, setAuthenticationTokenClass, setCredentialsMatcher, supportsMethods inherited from class org.apache.shiro.realm.CachingRealm
clearCache, getAvailablePrincipal, getCacheManager, getName, isCachingEnabled, onLogout, setCacheManager, setCachingEnabledMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.shiro.lang.util.Initializable
init
-
Constructor Details
-
AuthorizingRealm
public AuthorizingRealm() -
AuthorizingRealm
-
AuthorizingRealm
-
AuthorizingRealm
public AuthorizingRealm(org.apache.shiro.cache.CacheManager cacheManager, CredentialsMatcher matcher)
-
-
Method Details
-
setName
- Specified by:
setNamein interfaceorg.apache.shiro.lang.util.Nameable- Overrides:
setNamein classAuthenticatingRealm
-
setAuthorizationCache
public void setAuthorizationCache(org.apache.shiro.cache.Cache<Object, AuthorizationInfo> authorizationCache) -
getAuthorizationCache
-
getAuthorizationCacheName
-
setAuthorizationCacheName
-
isAuthorizationCachingEnabled
Returnstrueif authorization caching should be utilized if aCacheManagerhas beenconfigured,falseotherwise. The default value istrue.- Returns:
trueif authorization caching should be utilized,falseotherwise.
-
setAuthorizationCachingEnabled
Sets whether or not authorization caching should be utilized if aCacheManagerhas beenconfigured,falseotherwise. The default value istrue.- Parameters:
authorizationCachingEnabled- the value to set
-
getPermissionResolver
-
setPermissionResolver
Description copied from interface:PermissionResolverAwareSets the specified PermissionResolver on this instance.- Specified by:
setPermissionResolverin interfacePermissionResolverAware- Parameters:
permissionResolver- the PermissionResolver being set.
-
getRolePermissionResolver
-
setRolePermissionResolver
Description copied from interface:RolePermissionResolverAwareSets the specified RolePermissionResolver on this instance.- Specified by:
setRolePermissionResolverin interfaceRolePermissionResolverAware- Parameters:
permissionRoleResolver- the RolePermissionResolver being set.
-
onInit
Initializes this realm and potentially enables a cache, depending on configuration. When this method is called, the following logic is executed:- If the
cacheproperty has been set, it will be used to cache the AuthorizationInfo objects returned fromgetAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)method invocations. All future calls togetAuthorizationInfowill attempt to use this cache first to alleviate any potentially unnecessary calls to an underlying data store. - If the
cacheproperty has not been set, thecacheManagerproperty will be checked. If acacheManagerhas been set, it will be used to create an authorizationcache, and this newly created cache which will be used as specified in #1. - If neither the
(org.apache.shiro.cache.Cache) cacheorcacheManagerproperties are set, caching will be disabled and authorization look-ups will be delegated to subclass implementations for each authorization check.
- Overrides:
onInitin classAuthenticatingRealm
- If the
-
afterCacheManagerSet
Description copied from class:AuthenticatingRealmThis implementation attempts to acquire an authentication cache if one is not already configured.- Overrides:
afterCacheManagerSetin classAuthenticatingRealm
-
getAuthorizationInfo
Returns an account's authorization-specific information for the specifiedprincipals, ornullif no account could be found. The resultingAuthorizationInfoobject is used by the other method implementations in this class to automatically perform access control checks for the correspondingSubject. This implementation obtains the actualAuthorizationInfoobject from the subclass's implementation ofdoGetAuthorizationInfo, and then caches it for efficient reuse if caching is enabled (see below). Invocations of this method should be thought of as completely orthogonal to acquiringauthenticationInfo, since either could occur in any order. For example, in "Remember Me" scenarios, the user identity is remembered (and assumed) for their current session and an authentication attempt during that session might never occur. But because their identity would be remembered, that is sufficient enough information to call this method to execute any necessary authorization checks. For this reason, authentication and authorization should be loosely coupled and not depend on each other.Caching
TheAuthorizationInfovalues returned from this method are cached for efficient reuse if caching is enabled. Caching is enabled automatically when anauthorizationCacheinstance has been explicitly configured, or if acacheManagerhas been configured, which will be used to lazily create theauthorizationCacheas needed. If caching is enabled, the authorization cache will be checked first and if found, will return the cachedAuthorizationInfoimmediately. If caching is disabled, or there is a cache miss, the authorization info will be looked up from the underlying data store via thedoGetAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)method, which must be implemented by subclasses.Changed Data
If caching is enabled and if any authorization data for an account is changed at runtime, such as adding or removing roles and/or permissions, the subclass implementation should clear the cached AuthorizationInfo for that account via theclearCachedAuthorizationInfomethod. This ensures that the next call togetAuthorizationInfo(PrincipalCollection)will acquire the account's fresh authorization data, where it will then be cached for efficient reuse. This ensures that stale authorization data will not be reused.- Parameters:
principals- the corresponding Subject's identifying principals with which to look up the Subject'sAuthorizationInfo.- Returns:
- the authorization information for the account associated with the specified
principals, ornullif no account could be found.
-
getAuthorizationCacheKey
-
clearCachedAuthorizationInfo
Clears out the AuthorizationInfo cache entry for the specified account. This method is provided as a convenience to subclasses so they can invalidate a cache entry when they change an account's authorization data (add/remove roles or permissions) during runtime. Because an account's AuthorizationInfo can be cached, there needs to be a way to invalidate the cache for only that account so that subsequent authorization operations don't used the (old) cached value if account data changes. After this method is called, the next authorization check for that same account will result in a call togetAuthorizationInfo, and the resulting return value will be cached before being returned so it can be reused for later authorization checks. If you wish to clear out all associated cached data (and not just authorization data), use theCachingRealm.clearCache(org.apache.shiro.subject.PrincipalCollection)method instead (which will in turn call this method by default).- Parameters:
principals- the principals of the account for which to clear the cached AuthorizationInfo.
-
doGetAuthorizationInfo
Retrieves the AuthorizationInfo for the given principals from the underlying data store. When returning an instance from this method, you might want to consider using an instance ofSimpleAuthorizationInfo, as it is suitable in most cases.- Parameters:
principals- the primary identifying principals of the AuthorizationInfo that should be retrieved.- Returns:
- the AuthorizationInfo associated with this principals.
- See Also:
-
getPermissions
-
isPermitted
Description copied from interface:AuthorizerReturns true if the corresponding subject/user is permitted to perform an action or access a resource summarized by the specified permission string.This is an overloaded method for the corresponding type-safe
Permissionvariant. Please see the class-level JavaDoc for more information on these String-based permission methods.- Specified by:
isPermittedin interfaceAuthorizer- Parameters:
principals- the application-specific subject/user identifier.permission- the String representation of a Permission that is being checked.- Returns:
- true if the corresponding Subject/user is permitted, false otherwise.
- See Also:
-
isPermitted
Description copied from interface:AuthorizerReturns true if the corresponding subject/user is permitted to perform an action or access a resource summarized by the specified permission.More specifically, this method determines if any Permissions associated with the subject
implythe specified permission.- Specified by:
isPermittedin interfaceAuthorizer- Parameters:
principals- the application-specific subject/user identifier.permission- the permission that is being checked.- Returns:
- true if the corresponding Subject/user is permitted, false otherwise.
-
isPermitted
-
isPermitted
Description copied from interface:AuthorizerChecks if the corresponding Subject implies the given permission strings and returns a boolean array indicating which permissions are implied.This is an overloaded method for the corresponding type-safe
Permissionvariant. Please see the class-level JavaDoc for more information on these String-based permission methods.- Specified by:
isPermittedin interfaceAuthorizer- Parameters:
subjectIdentifier- the application-specific subject/user identifier.permissions- the String representations of the Permissions that are being checked.- Returns:
- an array of booleans whose indices correspond to the index of the permissions in the given list. A true value at an index indicates the user is permitted for for the associated Permission string in the list. A false value at an index indicates otherwise.
-
isPermitted
Description copied from interface:AuthorizerChecks if the corresponding Subject/user implies the given Permissions and returns a boolean array indicating which permissions are implied.More specifically, this method should determine if each Permission in the array is
impliedby permissions already associated with the subject.This is primarily a performance-enhancing method to help reduce the number of
Authorizer.isPermitted(org.apache.shiro.subject.PrincipalCollection, java.lang.String)invocations over the wire in client/server systems.- Specified by:
isPermittedin interfaceAuthorizer- Parameters:
principals- the application-specific subject/user identifier.permissions- the permissions that are being checked.- Returns:
- an array of booleans whose indices correspond to the index of the permissions in the given list. A true value at an index indicates the user is permitted for for the associated Permission object in the list. A false value at an index indicates otherwise.
-
isPermitted
-
isPermittedAll
Description copied from interface:AuthorizerReturns true if the corresponding Subject/user implies all of the specified permission strings, false otherwise.This is an overloaded method for the corresponding type-safe
Permissionvariant. Please see the class-level JavaDoc for more information on these String-based permission methods.- Specified by:
isPermittedAllin interfaceAuthorizer- Parameters:
subjectIdentifier- the application-specific subject/user identifier.permissions- the String representations of the Permissions that are being checked.- Returns:
- true if the user has all of the specified permissions, false otherwise.
- See Also:
-
isPermittedAll
Description copied from interface:AuthorizerReturns true if the corresponding Subject/user implies all of the specified permissions, false otherwise.More specifically, this method determines if all of the given Permissions are
implied bypermissions already associated with the subject.- Specified by:
isPermittedAllin interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.permissions- the permissions to check.- Returns:
- true if the user has all of the specified permissions, false otherwise.
-
isPermittedAll
-
checkPermission
public void checkPermission(PrincipalCollection subjectIdentifier, String permission) throws AuthorizationException Description copied from interface:AuthorizerEnsures the corresponding Subject/user implies the specified permission String.If the subject's existing associated permissions do not
Permission.implies(Permission)imply} the given permission, anAuthorizationExceptionwill be thrown.This is an overloaded method for the corresponding type-safe
Permissionvariant. Please see the class-level JavaDoc for more information on these String-based permission methods.- Specified by:
checkPermissionin interfaceAuthorizer- Parameters:
subjectIdentifier- the application-specific subject/user identifier.permission- the String representation of the Permission to check.- Throws:
AuthorizationException- if the user does not have the permission.
-
checkPermission
public void checkPermission(PrincipalCollection principal, Permission permission) throws AuthorizationException Description copied from interface:AuthorizerEnsures a subject/userPermission.implies(Permission)implies} the specified Permission. If the subject's existing associated permissions do notPermission.implies(Permission)imply} the given permission, anAuthorizationExceptionwill be thrown.- Specified by:
checkPermissionin interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.permission- the Permission to check.- Throws:
AuthorizationException- if the user does not have the permission.
-
checkPermission
-
checkPermissions
public void checkPermissions(PrincipalCollection subjectIdentifier, String... permissions) throws AuthorizationException Description copied from interface:AuthorizerEnsures the corresponding Subject/userimpliesall of the specified permission strings.If the subject's existing associated permissions do not
implyall of the given permissions, anAuthorizationExceptionwill be thrown.This is an overloaded method for the corresponding type-safe
Permissionvariant. Please see the class-level JavaDoc for more information on these String-based permission methods.- Specified by:
checkPermissionsin interfaceAuthorizer- Parameters:
subjectIdentifier- the application-specific subject/user identifier.permissions- the string representations of Permissions to check.- Throws:
AuthorizationException- if the user does not have all of the given permissions.
-
checkPermissions
public void checkPermissions(PrincipalCollection principal, Collection<Permission> permissions) throws AuthorizationException Description copied from interface:AuthorizerEnsures the corresponding Subject/userimpliesall of the specified permission strings.If the subject's existing associated permissions do not
implyall of the given permissions, anAuthorizationExceptionwill be thrown.- Specified by:
checkPermissionsin interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.permissions- the Permissions to check.- Throws:
AuthorizationException- if the user does not have all of the given permissions.
-
checkPermissions
-
hasRole
Description copied from interface:AuthorizerReturns true if the corresponding Subject/user has the specified role, false otherwise.- Specified by:
hasRolein interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.roleIdentifier- the application-specific role identifier (usually a role id or role name).- Returns:
- true if the corresponding subject has the specified role, false otherwise.
-
hasRole
-
hasRoles
Description copied from interface:AuthorizerChecks if the corresponding Subject/user has the specified roles, returning a boolean array indicating which roles are associated with the given subject.This is primarily a performance-enhancing method to help reduce the number of
Authorizer.hasRole(org.apache.shiro.subject.PrincipalCollection, java.lang.String)invocations over the wire in client/server systems.- Specified by:
hasRolesin interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.roleIdentifiers- the application-specific role identifiers to check (usually role ids or role names).- Returns:
- an array of booleans whose indices correspond to the index of the roles in the given identifiers. A true value indicates the user has the role at that index. False indicates the user does not have the role at that index.
-
hasRoles
-
hasAllRoles
Description copied from interface:AuthorizerReturns true if the corresponding Subject/user has all of the specified roles, false otherwise.- Specified by:
hasAllRolesin interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.roleIdentifiers- the application-specific role identifiers to check (usually role ids or role names).- Returns:
- true if the user has all the roles, false otherwise.
-
checkRole
Description copied from interface:AuthorizerAsserts the corresponding Subject/user has the specified role by returning quietly if they do or throwing anAuthorizationExceptionif they do not.- Specified by:
checkRolein interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.role- the application-specific role identifier (usually a role id or role name ).- Throws:
AuthorizationException- if the user does not have the role.
-
checkRole
-
checkRoles
public void checkRoles(PrincipalCollection principal, Collection<String> roles) throws AuthorizationException Description copied from interface:AuthorizerAsserts the corresponding Subject/user has all of the specified roles by returning quietly if they do or throwing anAuthorizationExceptionif they do not.- Specified by:
checkRolesin interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.roles- the application-specific role identifiers to check (usually role ids or role names).- Throws:
AuthorizationException- if the user does not have all of the specified roles.
-
checkRoles
public void checkRoles(PrincipalCollection principal, String... roles) throws AuthorizationException Description copied from interface:AuthorizerSame ascheckRoles(PrincipalCollection subjectPrincipal, Collection<String> roleIdentifiers)but doesn't require a collection as an argument. Asserts the corresponding Subject/user has all the specified roles by returning quietly if they do or throwing anAuthorizationExceptionif they do not.- Specified by:
checkRolesin interfaceAuthorizer- Parameters:
principal- the application-specific subject/user identifier.roles- the application-specific role identifiers to check (usually role ids or role names).- Throws:
AuthorizationException- if the user does not have all the specified roles.
-
checkRoles
-
doClearCache
Callssuper.doClearCacheto ensure any cached authentication data is removed and then callsclearCachedAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)to remove any cached authorization data. If overriding in a subclass, be sure to callsuper.doClearCacheto ensure this behavior is maintained.- Overrides:
doClearCachein classAuthenticatingRealm- Parameters:
principals- the principals of the account for which to clear any cached AuthorizationInfo- Since:
- 1.2
-