API java : AccessController


java.security
Class AccessController

java.lang.Object
  extended by java.security.AccessController

public final class AccessController
extends Object

The AccessController class is used for access control operations and decisions.

More specifically, the AccessController class is used for three purposes:

  • to decide whether an access to a critical system resource is to be allowed or denied, based on the security policy currently in effect,

  • to mark code as being "privileged", thus affecting subsequent access determinations, and

  • to obtain a "snapshot" of the current calling context so access-control decisions from a different context can be made with respect to the saved context.

The checkPermission method determines whether the access request indicated by a specified permission should be granted or denied. A sample call appears below. In this example, checkPermission will determine whether or not to grant "read" access to the file named "testFile" in the "/temp" directory.

  1. FilePermission perm = new FilePermission("/temp/testFile", "read");
  2. AccessController.checkPermission(perm);

If a requested access is allowed, checkPermission returns quietly. If denied, an AccessControlException is thrown. AccessControlException can also be thrown if the requested permission is of an incorrect type or contains an invalid value. Such information is given whenever possible. Suppose the current thread traversed m callers, in the order of caller 1 to caller 2 to caller m. Then caller m invoked the checkPermission method. The checkPermission method determines whether access is granted or denied based on the following algorithm:

  1. i = m;
  2.  
  3. while (i > 0) {
  4.  
  5. if (caller i's domain does not have the permission)
  6. throw AccessControlException
  7.  
  8. else if (caller i is marked as privileged) {
  9. if (a context was specified in the call to doPrivileged)
  10. context.checkPermission(permission)
  11. return;
  12. }
  13. i = i - 1;
  14. };
  15.  
  16. // Next, check the context inherited when
  17. // the thread was created. Whenever a new thread is created, the
  18. // AccessControlContext at that time is
  19. // stored and associated with the new thread, as the "inherited"
  20. // context.
  21.  
  22. inheritedContext.checkPermission(permission);

A caller can be marked as being "privileged" (see doPrivileged and below). When making access control decisions, the checkPermission method stops checking if it reaches a caller that was marked as "privileged" via a doPrivileged call without a context argument (see below for information about a context argument). If that caller's domain has the specified permission, no further checking is done and checkPermission returns quietly, indicating that the requested access is allowed. If that domain does not have the specified permission, an exception is thrown, as usual.

The normal use of the "privileged" feature is as follows. If you don't need to return a value from within the "privileged" block, do the following:

  1. somemethod() {
  2. ...normal code here...
  3. AccessController.doPrivileged(new PrivilegedAction() {
  4. public Object run() {
  5. // privileged code goes here, for example:
  6. System.loadLibrary("awt");
  7. return null; // nothing to return
  8. }
  9. });
  10. ...normal code here...
  11. }

PrivilegedAction is an interface with a single method, named run, that returns an Object. The above example shows creation of an implementation of that interface; a concrete implementation of the run method is supplied. When the call to doPrivileged is made, an instance of the PrivilegedAction implementation is passed to it. The doPrivileged method calls the run method from the PrivilegedAction implementation after enabling privileges, and returns the run method's return value as the doPrivileged return value (which is ignored in this example).

If you need to return a value, you can do something like the following:

  1. somemethod() {
  2. ...normal code here...
  3. String user = (String) AccessController.doPrivileged(
  4. public Object run() {
  5. return System.getProperty("user.name");
  6. }
  7. }
  8. );
  9. ...normal code here...
  10. }

If the action performed in your run method could throw a "checked" exception (those listed in the throws clause of a method), then you need to use the PrivilegedExceptionAction interface instead of the PrivilegedAction interface:

  1. somemethod() throws FileNotFoundException {
  2. ...normal code here...
  3. try {
  4. public Object run() throws FileNotFoundException {
  5. return new FileInputStream("someFile");
  6. }
  7. }
  8. );
  9. // e.getException() should be an instance of FileNotFoundException,
  10. // as only "checked" exceptions will be "wrapped" in a
  11. // <code>PrivilegedActionException</code>.
  12. throw (FileNotFoundException) e.getException();
  13. }
  14. ...normal code here...
  15. }

Be *very* careful in your use of the "privileged" construct, and always remember to make the privileged code section as small as possible.

Note that checkPermission always performs security checks within the context of the currently executing thread. Sometimes a security check that should be made within a given context will actually need to be done from within a different context (for example, from within a worker thread). The getContext method and AccessControlContext class are provided for this situation. The getContext method takes a "snapshot" of the current calling context, and places it in an AccessControlContext object, which it returns. A sample call is the following:

AccessControlContext itself has a checkPermission method that makes access decisions based on the context it encapsulates, rather than that of the current execution thread. Code within a different context can thus call that method on the previously-saved AccessControlContext object. A sample call is the following:

  1. acc.checkPermission(permission)

There are also times where you don't know a priori which permissions to check the context against. In these cases you can use the doPrivileged method that takes a context:

  1. somemethod() {
  2. AccessController.doPrivileged(new PrivilegedAction() {
  3. public Object run() {
  4. // Code goes here. Any permission checks within this
  5. // run method will require that the intersection of the
  6. // callers protection domain and the snapshot's
  7. // context have the desired permission.
  8. }
  9. }, acc);
  10. ...normal code here...
  11. }

See Also:
AccessControlContext

Method Summary
static void checkPermission(Permission perm)
          Determines whether the access request indicated by the specified permission should be allowed or denied, based on the security policy currently in effect.
static
<T> T
doPrivileged(PrivilegedAction<T> action)
          Performs the specified PrivilegedAction with privileges enabled.
static
<T> T
doPrivileged(PrivilegedAction<T> action, AccessControlContext context)
          Performs the specified PrivilegedAction with privileges enabled and restricted by the specified AccessControlContext.
static
<T> T
doPrivileged(PrivilegedExceptionAction<T> action)
          Performs the specified PrivilegedExceptionAction with privileges enabled.
static
<T> T
doPrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context)
          Performs the specified PrivilegedExceptionAction with privileges enabled and restricted by the specified AccessControlContext.
static AccessControlContext getContext()
          This method takes a "snapshot" of the current calling context, which includes the current Thread's inherited AccessControlContext, and places it in an AccessControlContext object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

doPrivileged

public static <T> T doPrivileged(PrivilegedAction<T> action)
Performs the specified PrivilegedAction with privileges enabled. The action is performed with all of the permissions possessed by the caller's protection domain.

If the action's run method throws an (unchecked) exception, it will propagate through this method.

Parameters:
action - the action to be performed.
Returns:
the value returned by the action's run method.
Throws:
NullPointerException - if the action is null
See Also:
doPrivileged(PrivilegedAction,AccessControlContext), doPrivileged(PrivilegedExceptionAction)

doPrivileged

public static <T> T doPrivileged(PrivilegedAction<T> action,
                                 AccessControlContext context)
Performs the specified PrivilegedAction with privileges enabled and restricted by the specified AccessControlContext. The action is performed with the intersection of the permissions possessed by the caller's protection domain, and those possessed by the domains represented by the specified AccessControlContext.

If the action's run method throws an (unchecked) exception, it will propagate through this method.

Parameters:
action - the action to be performed.
context - an access control context representing the restriction to be applied to the caller's domain's privileges before performing the specified action. If the context is null, then no additional restriction is applied.
Returns:
the value returned by the action's run method.
Throws:
NullPointerException - if the action is null
See Also:
doPrivileged(PrivilegedAction), doPrivileged(PrivilegedExceptionAction,AccessControlContext)

doPrivileged

public static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
                      throws PrivilegedActionException
Performs the specified PrivilegedExceptionAction with privileges enabled. The action is performed with all of the permissions possessed by the caller's protection domain.

If the action's run method throws an unchecked exception, it will propagate through this method.

Parameters:
action - the action to be performed
Returns:
the value returned by the action's run method
Throws:
PrivilegedActionException - if the specified action's run method threw a checked exception
NullPointerException - if the action is null
See Also:
doPrivileged(PrivilegedAction), doPrivileged(PrivilegedExceptionAction,AccessControlContext)

doPrivileged

public static <T> T doPrivileged(PrivilegedExceptionAction<T> action,
                                 AccessControlContext context)
                      throws PrivilegedActionException
Performs the specified PrivilegedExceptionAction with privileges enabled and restricted by the specified AccessControlContext. The action is performed with the intersection of the the permissions possessed by the caller's protection domain, and those possessed by the domains represented by the specified AccessControlContext.

If the action's run method throws an unchecked exception, it will propagate through this method.

Parameters:
action - the action to be performed
context - an access control context representing the restriction to be applied to the caller's domain's privileges before performing the specified action. If the context is null, then no additional restriction is applied.
Returns:
the value returned by the action's run method
Throws:
PrivilegedActionException - if the specified action's run method threw a checked exception
NullPointerException - if the action is null
See Also:
doPrivileged(PrivilegedAction), doPrivileged(PrivilegedExceptionAction,AccessControlContext)

getContext

public static AccessControlContext getContext()
This method takes a "snapshot" of the current calling context, which includes the current Thread's inherited AccessControlContext, and places it in an AccessControlContext object. This context may then be checked at a later point, possibly in another thread.

Returns:
the AccessControlContext based on the current context.
See Also:
AccessControlContext

checkPermission

public static void checkPermission(Permission perm)
                            throws AccessControlException
Determines whether the access request indicated by the specified permission should be allowed or denied, based on the security policy currently in effect. This method quietly returns if the access request is permitted, or throws a suitable AccessControlException otherwise.

Parameters:
perm - the requested permission.
Throws:
AccessControlException - if the specified permission is not permitted, based on the current security policy.
NullPointerException - if the specified permission is null and is checked based on the security policy currently in effect.

Ces informations proviennent du site de http://java.sun.com

Remarques

Contenu

Le contenu de cette page provient du site de Sun, et est généré depuis un cache sur l'infobrol après certains traitements automatisés. La présentation peut donc différer du document original, mais le contenu aussi. Vous pouvez utiliser ce bouton pour afficher la page originale du site de Sun :

Quels sont les motivations de cette démarche?

Maintenir les pages en cache sur différents sites peut offrir plus de disponibilité.

Chaque page est indexée dans la base de donnée, ce qui permet de retrouver facilement les informations, au moyen des sommaires, du moteur de recherche interne, etc.

Des facilités sont mises en place pour que les membres de l'infobrol puissent effectuer des traductions en français des différents documents. Ceci devrait permettre aux débutants en programmation Java de consulter les API en français s'ils maîtrisent moins bien la langue de Shakespeare. Dans le cas où une traduction a été soumise, elle est disponible au moyen d'un lien en bas de page. Si la traduction a été validée, la page s'affiche par défaut en français, et un lien en bas de page permet d'atteindre la version en anglais.

Le code sur l'infobrol est automatiquement coloré selon la syntaxe, et les différents mots clés sont transformés en liens pour accéder rapidement aux informations.

Vous avez la possibilité de partager vos expériences en proposant vos propres extraits de code en utilisant le bouton "ajouter un commentaire" en bas de page. Si vous visitez simplement l'infobrol, vous avez déjà accès à cette fonction, mais si vous étes membre du brol, vous pouvez en plus utiliser des boutons supplémentaires de mise en forme, dont la coloration automatique de vos extraits de codes.

Réseaux sociaux

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.

 

Nuage de mots clés

6 mots clés dont 0 définis manuellement (plus d'information...).

Avertissement

Cette page ne possède pas encore de mots clés manuels, ceci est donc un exemple automatique (les niveaux de pertinence sont fictifs, mais les liens sont valables). Pour tester le nuage avec une page qui contient des mots définis manuellement, vous pouvez cliquer ici.

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher le nuage de mots clés.

 

Astuce pour imprimer les couleurs des cellules de tableaux : http://www.gaudry.be/ast-rf-450.html
Aucun commentaire pour cette page

© Ce document issu de l′infobrol est enregistré sous le certificat Cyber PrInterDeposit Digital Numbertection. Enregistrement IDDN n° 5329-2249
Document créé le 31/08/06 01:18, dernière modification le Vendredi 17 Juin 2011, 12:12
Source du document imprimé : http:///www.gaudry.be/java-api-rf-java/security/AccessController.html
St.Gaudry©07.01.02
Outils (masquer)
||
Recherche (afficher)
Recherche :

Utilisateur (masquer)
Navigation (masquer)
Apparence (afficher)
Stats (afficher)
15838 documents
455 astuces.
550 niouzes.
3107 definitions.
447 membres.
8121 messages.

Document genere en :
0,60 seconde

Mises à jour :
Mises à jour du site
Citation (masquer)
Les changements et les progrès sont très rarement des cadeaux d'en haut. Ils proviennent de luttes d'ici bas.

Noam Chomsky
 
l'infobrol
Nous sommes le Mardi 23 Mai 2017, 10:49, toutes les heures sont au format GMT+1.00 Heure, heure d'été (+1)