SubjectThreadState

Manages thread-state for {@link Subject Subject} access (supporting {@code SecurityUtils.}{@link hunt.shiro.SecurityUtils#getSubject() getSubject()} calls) during a thread's execution. <p/> The {@link #bind bind} method will bind a {@link Subject} and a {@link hunt.shiro.mgt.SecurityManager SecurityManager} to the {@link ThreadContext} so they can be retrieved from the {@code ThreadContext} later by any {@code SecurityUtils.}{@link hunt.shiro.SecurityUtils#getSubject() getSubject()} calls that might occur during the thread's execution.

Constructors

this
this(Subject subject)

Creates a new {@code SubjectThreadState} that will bind and unbind the specified {@code Subject} to the thread

Members

Functions

bind
void bind()

Binds a {@link Subject} and {@link hunt.shiro.mgt.SecurityManager SecurityManager} to the {@link ThreadContext} so they can be retrieved later by any {@code SecurityUtils.}{@link hunt.shiro.SecurityUtils#getSubject() getSubject()} calls that might occur during the thread's execution. <p/> Prior to binding, the {@code ThreadContext}'s existing {@link ThreadContext#getResources() resources} are retained so they can be restored later via the {@link #restore restore} call.

clear
void clear()

Completely {@link ThreadContext#remove removes} the {@code ThreadContext} state. Typically this method should only be called in special cases - it is more 'correct' to {@link #restore restore} a thread to its previous state than to clear it entirely.

getSubject
Subject getSubject()

Returns the {@code Subject} instance managed by this {@code ThreadState} implementation.

restore
void restore()

{@link ThreadContext#remove Remove}s all thread-state that was bound by this instance. If any previous thread-bound resources existed prior to the {@link #bind bind} call, they are restored back to the {@code ThreadContext} to ensure the thread state is exactly as it was before binding.

Inherited Members

From ThreadState

bind
void bind()

Binds any state that should be made accessible during a thread's execution. This should typically always be called in a {@code try/finally} block paired with the {@link #restore} call to guarantee that the thread is cleanly restored back to its original state. For example: <pre> ThreadState state = //acquire or instantiate as necessary <b>try { state.bind(); doSomething(); //execute any logic downstream logic that might need to access the state } </b> finally { state.restore(); } </pre>

restore
void restore()

Restores a thread to its state before bind {@link #bind bind} was invoked. This should typically always be called in a {@code finally} block to guarantee that the thread is cleanly restored back to its original state before {@link #bind bind}'s bind was called. For example: <pre> ThreadState state = //acquire or instantiate as necessary try { state.bind(); doSomething(); //execute any logic downstream logic that might need to access the state } <b>finally { state.restore(); }</b> </pre>

clear
void clear()

Completely clears/removes the {@code ThreadContext} state. Typically this method should only be called in special cases - it is more 'correct' to {@link #restore restore} a thread to its previous state than to clear it entirely.

Meta