Migrating from the Deprecated for Removal Subject.getSubject and Subject.doAs Methods to the Subject.current and Subject.callAs Methods
Note:
In JDK 24, the Security Manager has been permanently disabled. See JEP 486 for more information.The following methods from the javax.security.auth.Subject class have been deprecated for removal because they have dependencies on Security Manager APIs, which have also been deprecated for removal:
- Subject getSubject(AccessControlContext)
- T doAs(Subject, PrivilegedAction<T>)
- T doAs(Subject, PrivilegedExceptionAction<T>)
- T doAsPrivileged(Subject, PrivilegedAction<T>, AccessControlContext)
- T doAsPrivileged(Subject, PrivilegedExceptionAction<T>, AccessControlContext)
Starting in JDK 24, these methods, in addition to Subject current() and T callAs(Subject, Callable<T>), behave as follows:
- getSubject: The method throws an UnsupportedOperationException.
- current: The method returns the Subject bound to the period of the execution of the current thread.
- doAs and doAsPrivileged: These methods invoke an action with a Subject as the current subject for the bounded period execution of the action.
- callAs: Behaves just like doAs except the types of its action argument, Callable<T>, and thrown exception, CompletedException, are different.
Because the Security Manager has been permanently disabled, replace the Subject.getSubject(AccessControlContext) and Subject.doAs methods with Subject.current() and Subject.callAs(Subject, Callable<T>), respectively, in your code. See The callAs and current Methods for Performing an Action as a Particular Subject.
Note:
The Subject.doAsPrivileged methods don’t have replacement APIs.You can no longer store a Subject in an
AccessControlContext
, retrieve it with the Subject.current() method, and invoke
AccessController.doPrivileged
with that context.
However, you can access the parent's thread's current
Subject
from the child thread with the Subject.current() method with structured concurrency. See Structured Concurrency in Java Platform, Standard Edition Core
Libraries and The callAs and current Methods for Performing an Action as a Particular Subject for more information.
Check if child threads in your code access the Subject of their parent threads through the AccessControlContext class. If so, modify your code so that parent threads pass the Subject to newly created threads or use structured concurrency.