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 theSubjectContextinterface. 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 StringAUTHENTICATED_SESSION_KEYThe session key that is used to store whether or not the user is authenticated.static StringPRINCIPALS_SESSION_KEYThe session key that is used to store subject principals.static StringSESSION_CREATION_ENABLED
-
Constructor Summary
Constructors Constructor Description DefaultSubjectContext()DefaultSubjectContext(SubjectContext ctx)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AuthenticationInfogetAuthenticationInfo()AuthenticationTokengetAuthenticationToken()StringgetHost()Returns the host name or IP that should reflect the constructedSubject's originating location.PrincipalCollectiongetPrincipals()Returns the principals (aka identity) that the constructedSubjectshould reflect.SecurityManagergetSecurityManager()Returns the SecurityManager instance that should be used to back the constructedSubjectinstance ornullif one has not yet been provided to this context.SessiongetSession()Returns theSessionto use when building theSubjectinstance.SerializablegetSessionId()Returns the session id of the session that should be associated with the constructedSubjectinstance.SubjectgetSubject()Returns any existingSubjectthat may be in use at the time the newSubjectinstance is being created.booleanisAuthenticated()Returnstrueif the constructedSubjectshould be considered authenticated,falseotherwise.booleanisSessionCreationEnabled()Returnstrueif the constructedSubjectshould be allowed to create a session,falseotherwise.booleanresolveAuthenticated()StringresolveHost()PrincipalCollectionresolvePrincipals()SecurityManagerresolveSecurityManager()Resolves theSecurityManagerinstance that should be used to back the constructedSubjectinstance (typically used to supportDelegatingSubjectimplementations).SessionresolveSession()voidsetAuthenticated(boolean authc)Sets whether or not the constructedSubjectinstance should be considered as authenticated.voidsetAuthenticationInfo(AuthenticationInfo info)voidsetAuthenticationToken(AuthenticationToken token)voidsetHost(String host)Sets the host name or IP that should reflect the constructedSubject's originating location.voidsetPrincipals(PrincipalCollection principals)Sets the principals (aka identity) that the constructedSubjectshould reflect.voidsetSecurityManager(SecurityManager securityManager)Sets the SecurityManager instance that should be used to back the constructedSubjectinstance (typically used to supportDelegatingSubjectimplementations).voidsetSession(Session session)Sets theSessionto use when building theSubjectinstance.voidsetSessionCreationEnabled(boolean enabled)Sets whether or not the constructedSubjectinstance should be allowed to create a session,falseotherwise.voidsetSessionId(Serializable sessionId)Sets the session id of the session that should be associated with the constructedSubjectinstance.voidsetSubject(Subject subject)Sets the existingSubjectthat may be in use at the time the newSubjectinstance 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:SubjectContextReturns the SecurityManager instance that should be used to back the constructedSubjectinstance ornullif one has not yet been provided to this context.- Specified by:
getSecurityManagerin interfaceSubjectContext- Returns:
- the SecurityManager instance that should be used to back the constructed
Subjectinstance ornullif one has not yet been provided to this context.
-
setSecurityManager
public void setSecurityManager(SecurityManager securityManager)
Description copied from interface:SubjectContextSets the SecurityManager instance that should be used to back the constructedSubjectinstance (typically used to supportDelegatingSubjectimplementations).- Specified by:
setSecurityManagerin interfaceSubjectContext- Parameters:
securityManager- the SecurityManager instance that should be used to back the constructedSubjectinstance.
-
resolveSecurityManager
public SecurityManager resolveSecurityManager()
Description copied from interface:SubjectContextResolves theSecurityManagerinstance that should be used to back the constructedSubjectinstance (typically used to supportDelegatingSubjectimplementations).- Specified by:
resolveSecurityManagerin interfaceSubjectContext- Returns:
- the
SecurityManagerinstance that should be used to back the constructedSubjectinstance
-
getSessionId
public Serializable getSessionId()
Description copied from interface:SubjectContextReturns the session id of the session that should be associated with the constructedSubjectinstance. 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:
getSessionIdin interfaceSubjectContext- Returns:
- the session id of the session that should be associated with the constructed
Subjectinstance.
-
setSessionId
public void setSessionId(Serializable sessionId)
Description copied from interface:SubjectContextSets the session id of the session that should be associated with the constructedSubjectinstance. 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:
setSessionIdin interfaceSubjectContext- Parameters:
sessionId- the session id of the session that should be associated with the constructedSubjectinstance.
-
getSubject
public Subject getSubject()
Description copied from interface:SubjectContextReturns any existingSubjectthat may be in use at the time the newSubjectinstance is being created. This is typically used in the case where the existingSubjectinstance returned by this method is unauthenticated and a newSubjectinstance is being created to reflect a successful authentication - you want to return most of the state of the previousSubjectinstance when creating the newly authenticated instance.- Specified by:
getSubjectin interfaceSubjectContext- Returns:
- any existing
Subjectthat may be in use at the time the newSubjectinstance is being created.
-
setSubject
public void setSubject(Subject subject)
Description copied from interface:SubjectContextSets the existingSubjectthat may be in use at the time the newSubjectinstance is being created. This is typically used in the case where the existingSubjectinstance returned by this method is unauthenticated and a newSubjectinstance is being created to reflect a successful authentication - you want to return most of the state of the previousSubjectinstance when creating the newly authenticated instance.- Specified by:
setSubjectin interfaceSubjectContext- Parameters:
subject- the existingSubjectthat may be in use at the time the newSubjectinstance is being created.
-
getPrincipals
public PrincipalCollection getPrincipals()
Description copied from interface:SubjectContextReturns the principals (aka identity) that the constructedSubjectshould reflect.- Specified by:
getPrincipalsin interfaceSubjectContext- Returns:
- the principals (aka identity) that the constructed
Subjectshould reflect.
-
setPrincipals
public void setPrincipals(PrincipalCollection principals)
Description copied from interface:SubjectContextSets the principals (aka identity) that the constructedSubjectshould reflect.- Specified by:
setPrincipalsin interfaceSubjectContext- Parameters:
principals- the principals (aka identity) that the constructedSubjectshould reflect.
-
resolvePrincipals
public PrincipalCollection resolvePrincipals()
- Specified by:
resolvePrincipalsin interfaceSubjectContext
-
getSession
public Session getSession()
Description copied from interface:SubjectContextReturns theSessionto use when building theSubjectinstance. Note that it is more common to specify asessionIdto acquire the desired session rather than having to construct aSessionto be returned by this method.- Specified by:
getSessionin interfaceSubjectContext- Returns:
- the
Sessionto use when building theSubjectinstance.
-
setSession
public void setSession(Session session)
Description copied from interface:SubjectContextSets theSessionto use when building theSubjectinstance. Note that it is more common to specify asessionIdto automatically resolve the desired session rather than constructing aSessionto call this method.- Specified by:
setSessionin interfaceSubjectContext- Parameters:
session- theSessionto use when building theSubjectinstance.
-
resolveSession
public Session resolveSession()
- Specified by:
resolveSessionin interfaceSubjectContext
-
isSessionCreationEnabled
public boolean isSessionCreationEnabled()
Description copied from interface:SubjectContextReturnstrueif the constructedSubjectshould be allowed to create a session,falseotherwise. Shiro's configuration defaults totrueas most applications find value in Sessions.- Specified by:
isSessionCreationEnabledin interfaceSubjectContext- Returns:
trueif the constructedSubjectshould be allowed to create sessions,falseotherwise.
-
setSessionCreationEnabled
public void setSessionCreationEnabled(boolean enabled)
Description copied from interface:SubjectContextSets whether or not the constructedSubjectinstance should be allowed to create a session,falseotherwise.- Specified by:
setSessionCreationEnabledin interfaceSubjectContext- Parameters:
enabled- whether or not the constructedSubjectinstance should be allowed to create a session,falseotherwise.
-
isAuthenticated
public boolean isAuthenticated()
Description copied from interface:SubjectContextReturnstrueif the constructedSubjectshould be considered authenticated,falseotherwise. 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:
isAuthenticatedin interfaceSubjectContext- Returns:
trueif the constructedSubjectshould be considered authenticated,falseotherwise.
-
setAuthenticated
public void setAuthenticated(boolean authc)
Description copied from interface:SubjectContextSets whether or not the constructedSubjectinstance 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:
setAuthenticatedin interfaceSubjectContext- Parameters:
authc- whether or not the constructedSubjectinstance should be considered as authenticated.
-
resolveAuthenticated
public boolean resolveAuthenticated()
- Specified by:
resolveAuthenticatedin interfaceSubjectContext
-
getAuthenticationInfo
public AuthenticationInfo getAuthenticationInfo()
- Specified by:
getAuthenticationInfoin interfaceSubjectContext
-
setAuthenticationInfo
public void setAuthenticationInfo(AuthenticationInfo info)
- Specified by:
setAuthenticationInfoin interfaceSubjectContext
-
getAuthenticationToken
public AuthenticationToken getAuthenticationToken()
- Specified by:
getAuthenticationTokenin interfaceSubjectContext
-
setAuthenticationToken
public void setAuthenticationToken(AuthenticationToken token)
- Specified by:
setAuthenticationTokenin interfaceSubjectContext
-
getHost
public String getHost()
Description copied from interface:SubjectContextReturns the host name or IP that should reflect the constructedSubject's originating location.- Specified by:
getHostin 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:SubjectContextSets the host name or IP that should reflect the constructedSubject's originating location.- Specified by:
setHostin interfaceSubjectContext- Parameters:
host- the host name or IP that should reflect the constructedSubject's originating location.
-
resolveHost
public String resolveHost()
- Specified by:
resolveHostin interfaceSubjectContext
-
-