Class SubjectInheritingThread
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
DelegationTokenRenewer,GcTimeMonitor
Java propagates the current Subject to any new Threads in all version up to Java 21. In Java 22-23 the Subject is only propagated if the SecurityManager is enabled, while in Java 24+ it is never propagated.
Hadoop security heavily relies on the original behavior, as Subject is at the core of JAAS. This class wraps thread. It overrides start() and saves the Subject of the current thread, and wraps the payload in a Subject.doAs()/callAs() call to restore it in the newly created Thread.
When specifying a Runnable, this class is used in exactly the same way as Thread.
run() cannot be directly overridden, as that would also override the
subject restoration logic. SubjectInheritingThread provides a work()
method instead, which is wrapped and invoked by its own final run()
method.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionSubjectInheritingThread(Runnable target) Behaves similarly toThread(Runnable)constructor.SubjectInheritingThread(Runnable target, String name) Behaves similarly toThread(Runnable, String)constructor.Behaves similarly toThread(String)constructor.SubjectInheritingThread(ThreadGroup group, Runnable target) Behaves similarly toThread(ThreadGroup, Runnable)constructor.SubjectInheritingThread(ThreadGroup group, Runnable target, String name) Behaves similarly toThread(ThreadGroup, Runnable, String)constructor.SubjectInheritingThread(ThreadGroup group, String name) Behaves similarly toThread(ThreadGroup, String)constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidrun()This cannot be overridden in this class.final voidstart()Behaves similarly to pre-Java 22Thread.start().voidwork()This is the equivalent ofThread.run().Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, suspend, toString, yield
-
Constructor Details
-
SubjectInheritingThread
public SubjectInheritingThread() -
SubjectInheritingThread
Behaves similarly toThread(Runnable)constructor.- Parameters:
target- the object whoserunmethod is invoked when this thread is started. Ifnull, this classesrunmethod does nothing.
-
SubjectInheritingThread
Behaves similarly toThread(ThreadGroup, Runnable)constructor.- Parameters:
group- the thread group. Ifnulland there is a security manager, the group is determined by SecurityManager.getThreadGroup(). If there is not a security manager orSecurityManager.getThreadGroup()returnsnull, the group is set to the current thread's thread group.target- the object whoserunmethod is invoked when this thread is started. Ifnull, this thread's run method is invoked.- Throws:
SecurityException- if the current thread cannot create a thread in the specified thread group
-
SubjectInheritingThread
Behaves similarly toThread(Runnable, String)constructor.- Parameters:
target- the object whoserunmethod is invoked when this thread is started. Ifnull, this thread's run method is invoked.name- the name of the new thread- Throws:
SecurityException- if the current thread cannot create a thread in the specified thread group
-
SubjectInheritingThread
Behaves similarly toThread(String)constructor.- Parameters:
name- the name of the new thread
-
SubjectInheritingThread
Behaves similarly toThread(ThreadGroup, String)constructor.- Parameters:
group- the thread group. Ifnulland there is a security manager, the group is determined by SecurityManager.getThreadGroup(). If there is not a security manager orSecurityManager.getThreadGroup()returnsnull, the group is set to the current thread's thread group.name- the name of the new thread
-
SubjectInheritingThread
Behaves similarly toThread(ThreadGroup, Runnable, String)constructor.- Parameters:
group- the thread group. Ifnulland there is a security manager, the group is determined by SecurityManager.getThreadGroup(). If there is not a security manager orSecurityManager.getThreadGroup()returnsnull, the group is set to the current thread's thread group.target- the object whoserunmethod is invoked when this thread is started. Ifnull, this thread's run method is invoked.name- the name of the new thread- Throws:
SecurityException- if the current thread cannot create a thread in the specified thread group or cannot override the context class loader methods.
-
-
Method Details
-
start
public final void start()Behaves similarly to pre-Java 22Thread.start(). It saves the current Subject before starting the new thread, which is then used as the Subject for the Runnable or the overridden work() method. -
work
public void work()This is the equivalent ofThread.run(). Override this instead ofrun()Subject will be propagated like in pre-Java 22 Thread. -
run
public final void run()This cannot be overridden in this class. Override thework()method instead which behaves like pre-Java 22Thread.run()
-