Package org.apache.shiro.subject
Interface SubjectContext
-
- All Known Implementing Classes:
DefaultSubjectContext
public interface SubjectContext extends Map<String,Object>
ASubjectContext
is a 'bucket' of data presented to aSecurityManager
which interprets this data to constructSubject
instances. It is essentially a Map of data with a few additional type-safe methods for easy retrieval of objects commonly used to construct Subject instances. While this interface contains type-safe setters and getters for common data types, the map can contain anything additional that might be needed by theSecurityManager
orSubjectFactory
implementation to constructSubject
instances.Data Resolution
TheSubjectContext
interface also allows for heuristic resolution of data used to construct a subject instance. That is, if an attribute has not been explicitly provided via a setter method, theresolve*
methods can use heuristics to obtain that data in another way from other attributes. For example, if one callsgetPrincipals()
and no principals are returned, perhaps the principals exist in thesession
or another attribute in the context. TheresolvePrincipals()
will know how to resolve the principals based on heuristics. If theresolve*
methods returnnull
then the data could not be achieved by any heuristics and must be considered as not available in the context. The general idea is that the normal getters can be called to see if the value was explicitly set. Theresolve*
methods should be used when actually constructing theSubject
instance to ensure the most specific/accurate data can be used. USAGE: Most Shiro end-users will never use aSubjectContext
instance directly and instead will use aSubject.Builder
(which internally uses aSubjectContext
) and buildSubject
instances that way.- Since:
- 1.0
- See Also:
SecurityManager.createSubject
,SubjectFactory
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description AuthenticationInfo
getAuthenticationInfo()
AuthenticationToken
getAuthenticationToken()
String
getHost()
Returns the host name or IP that should reflect the constructedSubject
's originating location.PrincipalCollection
getPrincipals()
Returns the principals (aka identity) that the constructedSubject
should reflect.SecurityManager
getSecurityManager()
Returns the SecurityManager instance that should be used to back the constructedSubject
instance ornull
if one has not yet been provided to this context.Session
getSession()
Returns theSession
to use when building theSubject
instance.Serializable
getSessionId()
Returns the session id of the session that should be associated with the constructedSubject
instance.Subject
getSubject()
Returns any existingSubject
that may be in use at the time the newSubject
instance is being created.boolean
isAuthenticated()
Returnstrue
if the constructedSubject
should be considered authenticated,false
otherwise.boolean
isSessionCreationEnabled()
Returnstrue
if the constructedSubject
should be allowed to create a session,false
otherwise.boolean
resolveAuthenticated()
String
resolveHost()
PrincipalCollection
resolvePrincipals()
SecurityManager
resolveSecurityManager()
Resolves theSecurityManager
instance that should be used to back the constructedSubject
instance (typically used to supportDelegatingSubject
implementations).Session
resolveSession()
void
setAuthenticated(boolean authc)
Sets whether or not the constructedSubject
instance should be considered as authenticated.void
setAuthenticationInfo(AuthenticationInfo info)
void
setAuthenticationToken(AuthenticationToken token)
void
setHost(String host)
Sets the host name or IP that should reflect the constructedSubject
's originating location.void
setPrincipals(PrincipalCollection principals)
Sets the principals (aka identity) that the constructedSubject
should reflect.void
setSecurityManager(SecurityManager securityManager)
Sets the SecurityManager instance that should be used to back the constructedSubject
instance (typically used to supportDelegatingSubject
implementations).void
setSession(Session session)
Sets theSession
to use when building theSubject
instance.void
setSessionCreationEnabled(boolean enabled)
Sets whether or not the constructedSubject
instance should be allowed to create a session,false
otherwise.void
setSessionId(Serializable sessionId)
Sets the session id of the session that should be associated with the constructedSubject
instance.void
setSubject(Subject subject)
Sets the existingSubject
that may be in use at the time the newSubject
instance is being created.-
Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
-
-
-
Method Detail
-
getSecurityManager
SecurityManager getSecurityManager()
Returns the SecurityManager instance that should be used to back the constructedSubject
instance ornull
if one has not yet been provided to this context.- Returns:
- the SecurityManager instance that should be used to back the constructed
Subject
instance ornull
if one has not yet been provided to this context.
-
setSecurityManager
void setSecurityManager(SecurityManager securityManager)
Sets the SecurityManager instance that should be used to back the constructedSubject
instance (typically used to supportDelegatingSubject
implementations).- Parameters:
securityManager
- the SecurityManager instance that should be used to back the constructedSubject
instance.
-
resolveSecurityManager
SecurityManager resolveSecurityManager()
Resolves theSecurityManager
instance that should be used to back the constructedSubject
instance (typically used to supportDelegatingSubject
implementations).- Returns:
- the
SecurityManager
instance that should be used to back the constructedSubject
instance
-
getSessionId
Serializable getSessionId()
Returns the session id of the session that should be associated with the constructedSubject
instance. The construction process is expected to resolve the session with the specified id and then construct the Subject instance based on the resolved session.- Returns:
- the session id of the session that should be associated with the constructed
Subject
instance.
-
setSessionId
void setSessionId(Serializable sessionId)
Sets the session id of the session that should be associated with the constructedSubject
instance. The construction process is expected to resolve the session with the specified id and then construct the Subject instance based on the resolved session.- Parameters:
sessionId
- the session id of the session that should be associated with the constructedSubject
instance.
-
getSubject
Subject getSubject()
Returns any existingSubject
that may be in use at the time the newSubject
instance is being created. This is typically used in the case where the existingSubject
instance returned by this method is unauthenticated and a newSubject
instance is being created to reflect a successful authentication - you want to return most of the state of the previousSubject
instance when creating the newly authenticated instance.- Returns:
- any existing
Subject
that may be in use at the time the newSubject
instance is being created.
-
setSubject
void setSubject(Subject subject)
Sets the existingSubject
that may be in use at the time the newSubject
instance is being created. This is typically used in the case where the existingSubject
instance returned by this method is unauthenticated and a newSubject
instance is being created to reflect a successful authentication - you want to return most of the state of the previousSubject
instance when creating the newly authenticated instance.- Parameters:
subject
- the existingSubject
that may be in use at the time the newSubject
instance is being created.
-
getPrincipals
PrincipalCollection getPrincipals()
Returns the principals (aka identity) that the constructedSubject
should reflect.- Returns:
- the principals (aka identity) that the constructed
Subject
should reflect.
-
resolvePrincipals
PrincipalCollection resolvePrincipals()
-
setPrincipals
void setPrincipals(PrincipalCollection principals)
Sets the principals (aka identity) that the constructedSubject
should reflect.- Parameters:
principals
- the principals (aka identity) that the constructedSubject
should reflect.
-
getSession
Session getSession()
Returns theSession
to use when building theSubject
instance. Note that it is more common to specify asessionId
to acquire the desired session rather than having to construct aSession
to be returned by this method.- Returns:
- the
Session
to use when building theSubject
instance.
-
setSession
void setSession(Session session)
Sets theSession
to use when building theSubject
instance. Note that it is more common to specify asessionId
to automatically resolve the desired session rather than constructing aSession
to call this method.- Parameters:
session
- theSession
to use when building theSubject
instance.
-
resolveSession
Session resolveSession()
-
isAuthenticated
boolean isAuthenticated()
Returnstrue
if the constructedSubject
should be considered authenticated,false
otherwise. Be careful setting this value totrue
- you should know what you are doing and have a good reason for ignoring Shiro's default authentication state mechanisms.- Returns:
true
if the constructedSubject
should be considered authenticated,false
otherwise.
-
setAuthenticated
void setAuthenticated(boolean authc)
Sets whether or not the constructedSubject
instance should be considered as authenticated. Be careful when specifyingtrue
- you should know what you are doing and have a good reason for ignoring Shiro's default authentication state mechanisms.- Parameters:
authc
- whether or not the constructedSubject
instance should be considered as authenticated.
-
isSessionCreationEnabled
boolean isSessionCreationEnabled()
Returnstrue
if the constructedSubject
should be allowed to create a session,false
otherwise. Shiro's configuration defaults totrue
as most applications find value in Sessions.- Returns:
true
if the constructedSubject
should be allowed to create sessions,false
otherwise.- Since:
- 1.2
-
setSessionCreationEnabled
void setSessionCreationEnabled(boolean enabled)
Sets whether or not the constructedSubject
instance should be allowed to create a session,false
otherwise.- Parameters:
enabled
- whether or not the constructedSubject
instance should be allowed to create a session,false
otherwise.- Since:
- 1.2
-
resolveAuthenticated
boolean resolveAuthenticated()
-
getAuthenticationInfo
AuthenticationInfo getAuthenticationInfo()
-
setAuthenticationInfo
void setAuthenticationInfo(AuthenticationInfo info)
-
getAuthenticationToken
AuthenticationToken getAuthenticationToken()
-
setAuthenticationToken
void setAuthenticationToken(AuthenticationToken token)
-
getHost
String getHost()
Returns the host name or IP that should reflect the constructedSubject
's originating location.- Returns:
- the host name or IP that should reflect the constructed
Subject
's originating location.
-
setHost
void setHost(String host)
Sets the host name or IP that should reflect the constructedSubject
's originating location.- Parameters:
host
- the host name or IP that should reflect the constructedSubject
's originating location.
-
resolveHost
String resolveHost()
-
-