Package org.apache.shiro.subject.support
Class DefaultSubjectContext
- java.lang.Object
-
- org.apache.shiro.util.MapContext
-
- org.apache.shiro.subject.support.DefaultSubjectContext
-
- All Implemented Interfaces:
Serializable
,Map<String,Object>
,SubjectContext
public class DefaultSubjectContext extends MapContext implements SubjectContext
Default implementation of theSubjectContext
interface. Note that the getters and setters are not simple pass-through methods to an underlying attribute; the getters will employ numerous heuristics to acquire their data attribute as best as possible (for example, ifgetPrincipals()
is invoked, if the principals aren't in the backing map, it might check to see if there is a subject or session in the map and attempt to acquire the principals from those objects).- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
AUTHENTICATED_SESSION_KEY
The session key that is used to store whether or not the user is authenticated.static String
PRINCIPALS_SESSION_KEY
The session key that is used to store subject principals.static String
SESSION_CREATION_ENABLED
-
Constructor Summary
Constructors Constructor Description DefaultSubjectContext()
DefaultSubjectContext(SubjectContext ctx)
-
Method Summary
All Methods Instance Methods Concrete 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 class org.apache.shiro.util.MapContext
clear, containsKey, containsValue, entrySet, get, getTypedValue, isEmpty, keySet, nullSafePut, put, putAll, remove, size, values
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
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
-
-
-
-
Field Detail
-
SESSION_CREATION_ENABLED
public static final String SESSION_CREATION_ENABLED
-
PRINCIPALS_SESSION_KEY
public static final String PRINCIPALS_SESSION_KEY
The session key that is used to store subject principals.
-
AUTHENTICATED_SESSION_KEY
public static final String AUTHENTICATED_SESSION_KEY
The session key that is used to store whether or not the user is authenticated.
-
-
Constructor Detail
-
DefaultSubjectContext
public DefaultSubjectContext()
-
DefaultSubjectContext
public DefaultSubjectContext(SubjectContext ctx)
-
-
Method Detail
-
getSecurityManager
public SecurityManager getSecurityManager()
Description copied from interface:SubjectContext
Returns the SecurityManager instance that should be used to back the constructedSubject
instance ornull
if one has not yet been provided to this context.- Specified by:
getSecurityManager
in interfaceSubjectContext
- 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
public void setSecurityManager(SecurityManager securityManager)
Description copied from interface:SubjectContext
Sets the SecurityManager instance that should be used to back the constructedSubject
instance (typically used to supportDelegatingSubject
implementations).- Specified by:
setSecurityManager
in interfaceSubjectContext
- Parameters:
securityManager
- the SecurityManager instance that should be used to back the constructedSubject
instance.
-
resolveSecurityManager
public SecurityManager resolveSecurityManager()
Description copied from interface:SubjectContext
Resolves theSecurityManager
instance that should be used to back the constructedSubject
instance (typically used to supportDelegatingSubject
implementations).- Specified by:
resolveSecurityManager
in interfaceSubjectContext
- Returns:
- the
SecurityManager
instance that should be used to back the constructedSubject
instance
-
getSessionId
public Serializable getSessionId()
Description copied from interface:SubjectContext
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.- Specified by:
getSessionId
in interfaceSubjectContext
- Returns:
- the session id of the session that should be associated with the constructed
Subject
instance.
-
setSessionId
public void setSessionId(Serializable sessionId)
Description copied from interface:SubjectContext
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.- Specified by:
setSessionId
in interfaceSubjectContext
- Parameters:
sessionId
- the session id of the session that should be associated with the constructedSubject
instance.
-
getSubject
public Subject getSubject()
Description copied from interface:SubjectContext
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.- Specified by:
getSubject
in interfaceSubjectContext
- Returns:
- any existing
Subject
that may be in use at the time the newSubject
instance is being created.
-
setSubject
public void setSubject(Subject subject)
Description copied from interface:SubjectContext
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.- Specified by:
setSubject
in interfaceSubjectContext
- Parameters:
subject
- the existingSubject
that may be in use at the time the newSubject
instance is being created.
-
getPrincipals
public PrincipalCollection getPrincipals()
Description copied from interface:SubjectContext
Returns the principals (aka identity) that the constructedSubject
should reflect.- Specified by:
getPrincipals
in interfaceSubjectContext
- Returns:
- the principals (aka identity) that the constructed
Subject
should reflect.
-
setPrincipals
public void setPrincipals(PrincipalCollection principals)
Description copied from interface:SubjectContext
Sets the principals (aka identity) that the constructedSubject
should reflect.- Specified by:
setPrincipals
in interfaceSubjectContext
- Parameters:
principals
- the principals (aka identity) that the constructedSubject
should reflect.
-
resolvePrincipals
public PrincipalCollection resolvePrincipals()
- Specified by:
resolvePrincipals
in interfaceSubjectContext
-
getSession
public Session getSession()
Description copied from interface:SubjectContext
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.- Specified by:
getSession
in interfaceSubjectContext
- Returns:
- the
Session
to use when building theSubject
instance.
-
setSession
public void setSession(Session session)
Description copied from interface:SubjectContext
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.- Specified by:
setSession
in interfaceSubjectContext
- Parameters:
session
- theSession
to use when building theSubject
instance.
-
resolveSession
public Session resolveSession()
- Specified by:
resolveSession
in interfaceSubjectContext
-
isSessionCreationEnabled
public boolean isSessionCreationEnabled()
Description copied from interface:SubjectContext
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.- Specified by:
isSessionCreationEnabled
in interfaceSubjectContext
- Returns:
true
if the constructedSubject
should be allowed to create sessions,false
otherwise.
-
setSessionCreationEnabled
public void setSessionCreationEnabled(boolean enabled)
Description copied from interface:SubjectContext
Sets whether or not the constructedSubject
instance should be allowed to create a session,false
otherwise.- Specified by:
setSessionCreationEnabled
in interfaceSubjectContext
- Parameters:
enabled
- whether or not the constructedSubject
instance should be allowed to create a session,false
otherwise.
-
isAuthenticated
public boolean isAuthenticated()
Description copied from interface:SubjectContext
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.- Specified by:
isAuthenticated
in interfaceSubjectContext
- Returns:
true
if the constructedSubject
should be considered authenticated,false
otherwise.
-
setAuthenticated
public void setAuthenticated(boolean authc)
Description copied from interface:SubjectContext
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.- Specified by:
setAuthenticated
in interfaceSubjectContext
- Parameters:
authc
- whether or not the constructedSubject
instance should be considered as authenticated.
-
resolveAuthenticated
public boolean resolveAuthenticated()
- Specified by:
resolveAuthenticated
in interfaceSubjectContext
-
getAuthenticationInfo
public AuthenticationInfo getAuthenticationInfo()
- Specified by:
getAuthenticationInfo
in interfaceSubjectContext
-
setAuthenticationInfo
public void setAuthenticationInfo(AuthenticationInfo info)
- Specified by:
setAuthenticationInfo
in interfaceSubjectContext
-
getAuthenticationToken
public AuthenticationToken getAuthenticationToken()
- Specified by:
getAuthenticationToken
in interfaceSubjectContext
-
setAuthenticationToken
public void setAuthenticationToken(AuthenticationToken token)
- Specified by:
setAuthenticationToken
in interfaceSubjectContext
-
getHost
public String getHost()
Description copied from interface:SubjectContext
Returns the host name or IP that should reflect the constructedSubject
's originating location.- Specified by:
getHost
in interfaceSubjectContext
- Returns:
- the host name or IP that should reflect the constructed
Subject
's originating location.
-
setHost
public void setHost(String host)
Description copied from interface:SubjectContext
Sets the host name or IP that should reflect the constructedSubject
's originating location.- Specified by:
setHost
in interfaceSubjectContext
- Parameters:
host
- the host name or IP that should reflect the constructedSubject
's originating location.
-
resolveHost
public String resolveHost()
- Specified by:
resolveHost
in interfaceSubjectContext
-
-