< prev index next >

## src/java.base/share/classes/javax/security/auth/Subject.java

Print this page

     267      * Subjects (from nested {@code doAs} calls).
     268      * In this situation, the most recent {@code Subject} associated
     269      * with the {@code AccessControlContext} is returned.
     270      *
     271      * @param  acc the {@code AccessControlContext} from which to retrieve
     272      *          the {@code Subject}.
     273      *
     274      * @return  the {@code Subject} associated with the provided
     275      *          {@code AccessControlContext}, or {@code null}
     276      *          if no {@code Subject} is associated
     277      *          with the provided {@code AccessControlContext}.
     278      *
     279      * @throws SecurityException if a security manager is installed and the
     280      *          caller does not have an
     281      *          {@link AuthPermission#AuthPermission(String)
     282      *          AuthPermission("getSubject")} permission to get the
     283      *          {@code Subject}.
     284      *
     285      * @throws NullPointerException if the provided
     286      *          {@code AccessControlContext} is {@code null}.

     287      */

     288     public static Subject getSubject(final AccessControlContext acc) {
     289
     290         java.lang.SecurityManager sm = System.getSecurityManager();
     291         if (sm != null) {
     292             sm.checkPermission(AuthPermissionHolder.GET_SUBJECT_PERMISSION);
     293         }
     294
     295         Objects.requireNonNull(acc, ResourcesMgr.getString
     296                 ("invalid.null.AccessControlContext.provided"));
     297
     298         // return the Subject from the DomainCombiner of the provided context
     299         return AccessController.doPrivileged
     300             (new java.security.PrivilegedAction<>() {
     301             public Subject run() {
     302                 DomainCombiner dc = acc.getDomainCombiner();
     303                 if (!(dc instanceof SubjectDomainCombiner)) {
     304                     return null;
     305                 }
     306                 SubjectDomainCombiner sdc = (SubjectDomainCombiner)dc;
     307                 return sdc.getSubject();
     446      * @param <T> the type of the value returned by the PrivilegedAction's
     447      *                  {@code run} method.
     448      *
     449      * @param action the code to be run as the specified
     450      *                  {@code Subject}.
     451      *
     452      * @param acc the {@code AccessControlContext} to be tied to the
     453      *                  specified <i>subject</i> and <i>action</i>.
     454      *
     455      * @return the value returned by the PrivilegedAction's
     456      *                  {@code run} method.
     457      *
     458      * @throws NullPointerException if the {@code PrivilegedAction}
     459      *                  is {@code null}.
     460      *
     461      * @throws SecurityException if a security manager is installed and the
     462      *                  caller does not have a
     463      *                  {@link AuthPermission#AuthPermission(String)
     464      *                  AuthPermission("doAsPrivileged")} permission to invoke
     465      *                  this method.

     466      */

     467     public static <T> T doAsPrivileged(final Subject subject,
     468                         final java.security.PrivilegedAction<T> action,
     469                         final java.security.AccessControlContext acc) {
     470
     471         java.lang.SecurityManager sm = System.getSecurityManager();
     472         if (sm != null) {
     473             sm.checkPermission(AuthPermissionHolder.DO_AS_PRIVILEGED_PERMISSION);
     474         }
     475
     476         Objects.requireNonNull(action,
     477                 ResourcesMgr.getString("invalid.null.action.provided"));
     478
     479         // set up the new Subject-based AccessControlContext
     480         // for doPrivileged
     481         final AccessControlContext callerAcc =
     482                 (acc == null ?
     483                 new AccessControlContext(NULL_PD_ARRAY) :
     484                 acc);
     485
     486         // call doPrivileged and push this new context on the stack
     512      *
     513      * @param acc the {@code AccessControlContext} to be tied to the
     514      *                  specified <i>subject</i> and <i>action</i>.
     515      *
     516      * @return the value returned by the
     517      *                  PrivilegedExceptionAction's {@code run} method.
     518      *
     519      * @throws PrivilegedActionException if the
     520      *                  {@code PrivilegedExceptionAction.run}
     521      *                  method throws a checked exception.
     522      *
     523      * @throws NullPointerException if the specified
     524      *                  {@code PrivilegedExceptionAction} is
     525      *                  {@code null}.
     526      *
     527      * @throws SecurityException if a security manager is installed and the
     528      *                  caller does not have a
     529      *                  {@link AuthPermission#AuthPermission(String)
     530      *                  AuthPermission("doAsPrivileged")} permission to invoke
     531      *                  this method.

     532      */

     533     public static <T> T doAsPrivileged(final Subject subject,
     534                         final java.security.PrivilegedExceptionAction<T> action,
     535                         final java.security.AccessControlContext acc)
     536                         throws java.security.PrivilegedActionException {
     537
     538         java.lang.SecurityManager sm = System.getSecurityManager();
     539         if (sm != null) {
     540             sm.checkPermission(AuthPermissionHolder.DO_AS_PRIVILEGED_PERMISSION);
     541         }
     542
     543         Objects.requireNonNull(action,
     544                 ResourcesMgr.getString("invalid.null.action.provided"));
     545
     546         // set up the new Subject-based AccessControlContext for doPrivileged
     547         final AccessControlContext callerAcc =
     548                 (acc == null ?
     549                 new AccessControlContext(NULL_PD_ARRAY) :
     550                 acc);
     551
     552         // call doPrivileged and push this new context on the stack
     267      * Subjects (from nested {@code doAs} calls).
     268      * In this situation, the most recent {@code Subject} associated
     269      * with the {@code AccessControlContext} is returned.
     270      *
     271      * @param  acc the {@code AccessControlContext} from which to retrieve
     272      *          the {@code Subject}.
     273      *
     274      * @return  the {@code Subject} associated with the provided
     275      *          {@code AccessControlContext}, or {@code null}
     276      *          if no {@code Subject} is associated
     277      *          with the provided {@code AccessControlContext}.
     278      *
     279      * @throws SecurityException if a security manager is installed and the
     280      *          caller does not have an
     281      *          {@link AuthPermission#AuthPermission(String)
     282      *          AuthPermission("getSubject")} permission to get the
     283      *          {@code Subject}.
     284      *
     285      * @throws NullPointerException if the provided
     286      *          {@code AccessControlContext} is {@code null}.
     287      *
     288      * @deprecated This method depends on {@link AccessControlContext}
     289      *       which, in conjunction with
     290      *       {@linkplain SecurityManager the Security Manager}, is deprecated
     291      *       and subject to removal in a future release. However, obtaining a
     292      *       Subject is useful independent of the Security Manager, so a
     293      *       replacement for this method may be added in a future release.
     294      */
     295     @Deprecated(since="17", forRemoval=true)
     296     public static Subject getSubject(final AccessControlContext acc) {
     297
     298         java.lang.SecurityManager sm = System.getSecurityManager();
     299         if (sm != null) {
     300             sm.checkPermission(AuthPermissionHolder.GET_SUBJECT_PERMISSION);
     301         }
     302
     303         Objects.requireNonNull(acc, ResourcesMgr.getString
     304                 ("invalid.null.AccessControlContext.provided"));
     305
     306         // return the Subject from the DomainCombiner of the provided context
     307         return AccessController.doPrivileged
     308             (new java.security.PrivilegedAction<>() {
     309             public Subject run() {
     310                 DomainCombiner dc = acc.getDomainCombiner();
     311                 if (!(dc instanceof SubjectDomainCombiner)) {
     312                     return null;
     313                 }
     314                 SubjectDomainCombiner sdc = (SubjectDomainCombiner)dc;
     315                 return sdc.getSubject();
     454      * @param <T> the type of the value returned by the PrivilegedAction's
     455      *                  {@code run} method.
     456      *
     457      * @param action the code to be run as the specified
     458      *                  {@code Subject}.
     459      *
     460      * @param acc the {@code AccessControlContext} to be tied to the
     461      *                  specified <i>subject</i> and <i>action</i>.
     462      *
     463      * @return the value returned by the PrivilegedAction's
     464      *                  {@code run} method.
     465      *
     466      * @throws NullPointerException if the {@code PrivilegedAction}
     467      *                  is {@code null}.
     468      *
     469      * @throws SecurityException if a security manager is installed and the
     470      *                  caller does not have a
     471      *                  {@link AuthPermission#AuthPermission(String)
     472      *                  AuthPermission("doAsPrivileged")} permission to invoke
     473      *                  this method.
     474      *
     475      * @deprecated This method is only useful in conjunction with
     476      *       {@linkplain SecurityManager the Security Manager}, which is
     477      *       deprecated and subject to removal in a future release.
     478      *       Consequently, this method is also deprecated and subject to
     479      *       removal. There is no replacement for the Security Manager or this
     480      *       method.
     481      */
     482     @Deprecated(since="17", forRemoval=true)
     483     public static <T> T doAsPrivileged(final Subject subject,
     484                         final java.security.PrivilegedAction<T> action,
     485                         final java.security.AccessControlContext acc) {
     486
     487         java.lang.SecurityManager sm = System.getSecurityManager();
     488         if (sm != null) {
     489             sm.checkPermission(AuthPermissionHolder.DO_AS_PRIVILEGED_PERMISSION);
     490         }
     491
     492         Objects.requireNonNull(action,
     493                 ResourcesMgr.getString("invalid.null.action.provided"));
     494
     495         // set up the new Subject-based AccessControlContext
     496         // for doPrivileged
     497         final AccessControlContext callerAcc =
     498                 (acc == null ?
     499                 new AccessControlContext(NULL_PD_ARRAY) :
     500                 acc);
     501
     502         // call doPrivileged and push this new context on the stack
     528      *
     529      * @param acc the {@code AccessControlContext} to be tied to the
     530      *                  specified <i>subject</i> and <i>action</i>.
     531      *
     532      * @return the value returned by the
     533      *                  PrivilegedExceptionAction's {@code run} method.
     534      *
     535      * @throws PrivilegedActionException if the
     536      *                  {@code PrivilegedExceptionAction.run}
     537      *                  method throws a checked exception.
     538      *
     539      * @throws NullPointerException if the specified
     540      *                  {@code PrivilegedExceptionAction} is
     541      *                  {@code null}.
     542      *
     543      * @throws SecurityException if a security manager is installed and the
     544      *                  caller does not have a
     545      *                  {@link AuthPermission#AuthPermission(String)
     546      *                  AuthPermission("doAsPrivileged")} permission to invoke
     547      *                  this method.
     548      *
     549      * @deprecated This method is only useful in conjunction with
     550      *       {@linkplain SecurityManager the Security Manager}, which is
     551      *       deprecated and subject to removal in a future release.
     552      *       Consequently, this method is also deprecated and subject to
     553      *       removal. There is no replacement for the Security Manager or this
     554      *       method.
     555      */
     556     @Deprecated(since="17", forRemoval=true)
     557     public static <T> T doAsPrivileged(final Subject subject,
     558                         final java.security.PrivilegedExceptionAction<T> action,
     559                         final java.security.AccessControlContext acc)
     560                         throws java.security.PrivilegedActionException {
     561
     562         java.lang.SecurityManager sm = System.getSecurityManager();
     563         if (sm != null) {
     564             sm.checkPermission(AuthPermissionHolder.DO_AS_PRIVILEGED_PERMISSION);
     565         }
     566
     567         Objects.requireNonNull(action,
     568                 ResourcesMgr.getString("invalid.null.action.provided"));
     569
     570         // set up the new Subject-based AccessControlContext for doPrivileged
     571         final AccessControlContext callerAcc =
     572                 (acc == null ?
     573                 new AccessControlContext(NULL_PD_ARRAY) :
     574                 acc);
     575
     576         // call doPrivileged and push this new context on the stack

< prev index next >