Package org.apache.shiro.mgt
Class DefaultSubjectDAO
- java.lang.Object
-
- org.apache.shiro.mgt.DefaultSubjectDAO
-
- All Implemented Interfaces:
SubjectDAO
public class DefaultSubjectDAO extends Object implements SubjectDAO
DefaultSubjectDAO
implementation that stores Subject state in the Subject's Session by default (but this can be disabled - see below). The Subject instance can be re-created at a later time by first acquiring the associated Session (typically from aSessionManager
) via a session ID or session key and then building aSubject
instance fromSession
attributes.Controlling how Sessions are used
Whether or not aSubject
'sSession
is used or not to persist its own state is controlled on a per-Subject basis as determined by the configuredsessionStorageEvaluator
. The defaultEvaluator
is aDefaultSessionStorageEvaluator
, which supports enabling or disabling session usage for Subject persistence at a global level for all subjects (and defaults to allowing sessions to be used).Disabling Session Persistence Entirely
Because the defaultSessionStorageEvaluator
instance is aDefaultSessionStorageEvaluator
, you can disable Session usage for Subject state entirely by configuring that instance directly, e.g.:((DefaultSessionStorageEvaluator)sessionDAO.getSessionStorageEvaluator()).setSessionStorageEnabled(false);
or, for example, inshiro.ini
:securityManager.subjectDAO.sessionStorageEvaluator.sessionStorageEnabled = false
but note: ONLY do this your application is 100% stateless and you DO NOT need subjects to be remembered across remote invocations, or in a web environment across HTTP requests.Supporting Both Stateful and Stateless Subject paradigms
Perhaps your application needs to support a hybrid approach of both stateful and stateless Subjects:- Stateful: Stateful subjects might represent web end-users that need their identity and authentication state to be remembered from page to page.
- Stateless: Stateless subjects might represent API clients (e.g. REST clients) that authenticate on every request, and therefore don't need authentication state to be stored across requests in a session.
SessionStorageEvaluator
interface and configure it via thesetSessionStorageEvaluator(SessionStorageEvaluator)
method, or, withshiro.ini
:myEvaluator = com.my.CustomSessionStorageEvaluator securityManager.subjectDAO.sessionStorageEvaluator = $myEvaluator
Unless overridden, the default evaluator is aDefaultSessionStorageEvaluator
, which enables session usage for Subject state by default.
-
-
Constructor Summary
Constructors Constructor Description DefaultSubjectDAO()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
delete(Subject subject)
Removes any existing subject state from the subject's session (if the session exists).SessionStorageEvaluator
getSessionStorageEvaluator()
Returns theSessionStorageEvaluator
that will determine if aSubject
's state may be persisted in the Subject's session.protected boolean
isSessionStorageEnabled(Subject subject)
Determines if the subject's session will be used to persist subject state or not.protected void
mergeAuthenticationState(Subject subject)
Merges the Subject's current authentication state with whatever may be in any available session.protected void
mergePrincipals(Subject subject)
Merges the Subject's currentSubject.getPrincipals()
with whatever may be in any available session.protected void
removeFromSession(Subject subject)
Removes any existing subject state from the Subject's session (if the session exists).Subject
save(Subject subject)
Saves the subject's state to the subject'ssession
only ifsessionStorageEnabled(subject)
.protected void
saveToSession(Subject subject)
Saves the subject's state (it's principals and authentication state) to itssession
.void
setSessionStorageEvaluator(SessionStorageEvaluator sessionStorageEvaluator)
Sets theSessionStorageEvaluator
that will determine if aSubject
's state may be persisted in the Subject's session.
-
-
-
Method Detail
-
isSessionStorageEnabled
protected boolean isSessionStorageEnabled(Subject subject)
Determines if the subject's session will be used to persist subject state or not. This implementation merely delegates to the internalSessionStorageEvaluator
(aDefaultSessionStorageEvaluator
by default).- Parameters:
subject
- the subject to inspect to determine if the subject's session will be used to persist subject state or not.- Returns:
true
if the subject's session will be used to persist subject state,false
otherwise.- See Also:
setSessionStorageEvaluator(SessionStorageEvaluator)
,DefaultSessionStorageEvaluator
-
getSessionStorageEvaluator
public SessionStorageEvaluator getSessionStorageEvaluator()
Returns theSessionStorageEvaluator
that will determine if aSubject
's state may be persisted in the Subject's session. The default instance is aDefaultSessionStorageEvaluator
.- Returns:
- the
SessionStorageEvaluator
that will determine if aSubject
's state may be persisted in the Subject's session. - See Also:
DefaultSessionStorageEvaluator
-
setSessionStorageEvaluator
public void setSessionStorageEvaluator(SessionStorageEvaluator sessionStorageEvaluator)
Sets theSessionStorageEvaluator
that will determine if aSubject
's state may be persisted in the Subject's session. The default instance is aDefaultSessionStorageEvaluator
.- Parameters:
sessionStorageEvaluator
- theSessionStorageEvaluator
that will determine if aSubject
's state may be persisted in the Subject's session.- See Also:
DefaultSessionStorageEvaluator
-
save
public Subject save(Subject subject)
Saves the subject's state to the subject'ssession
only ifsessionStorageEnabled(subject)
. If session storage is not enabled for the specificSubject
, this method does nothing. In either case, the argumentSubject
is returned directly (a new Subject instance is not created).- Specified by:
save
in interfaceSubjectDAO
- Parameters:
subject
- the Subject instance for which its state will be created or updated.- Returns:
- the same
Subject
passed in (a new Subject instance is not created).
-
saveToSession
protected void saveToSession(Subject subject)
Saves the subject's state (it's principals and authentication state) to itssession
. The session can be retrieved at a later time (typically from aSessionManager
to be used to recreate theSubject
instance.- Parameters:
subject
- the subject for which state will be persisted to its session.
-
mergePrincipals
protected void mergePrincipals(Subject subject)
Merges the Subject's currentSubject.getPrincipals()
with whatever may be in any available session. Only updates the Subject's session if the session does not match the current principals state.- Parameters:
subject
- the Subject for which principals will potentially be merged into the Subject's session.
-
mergeAuthenticationState
protected void mergeAuthenticationState(Subject subject)
Merges the Subject's current authentication state with whatever may be in any available session. Only updates the Subject's session if the session does not match the current authentication state.- Parameters:
subject
- the Subject for which principals will potentially be merged into the Subject's session.
-
removeFromSession
protected void removeFromSession(Subject subject)
Removes any existing subject state from the Subject's session (if the session exists). If the session does not exist, this method does not do anything.- Parameters:
subject
- the subject for which any existing subject state will be removed from its session.
-
delete
public void delete(Subject subject)
Removes any existing subject state from the subject's session (if the session exists).- Specified by:
delete
in interfaceSubjectDAO
- Parameters:
subject
- the Subject instance for which any persistent state should be deleted.
-
-