Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
In a simulation server environment where users are allowed to submit their own code to be run by the server, it would clearly be advantageous for any user-submitted code to be run in side a sandbox, not unlike Applets are within a browser. I wanted to be able to leverage the JVM itself, rather than adding another VM layer to isolate these submitted components. This kind of limitation appears ...
I've written a clunky little breakout clone & I'm writing to disk for save games and high scores. Is there an easy way for me to check if my program is running as an applet, or as an application? eg... if(!anApplet){ //Include disk i/o features } I've tried setting a boolean variable in the 'main' class, setting it to false with init(), and true in the main method but no luck so far. ...
I'm writing an Applet that makes some JSON-RPC calls. I'm using the Google JSON library (GSON) to cast the response JSON into a class. Thsi seems to work fine as is but when I use this code in my Applet, I'm hit with a java.lang.reflect.reflectpermission. From what I read on this thread on SO, it seems that since GSON uses Reflection, I cannot use it in Applets unless I explicitly modify the s...
I'd like to get the list of running processes using a java applet running in a browser. My understanding is that, as long as the applet is signed, it will be able to get this information. Is this accurate? Is this possible with an unsigned applet? Finally, are there any FOS applets available that I could take a look at? Thanks.
I have an applet the loadslibrary using a System call through a static method called loadLibrary. System.loadLibrary("ReadRegistry"); This works fine as long as I call loadLibrary from within the applet. However, if from javascript I access another method, called handleLoad(), which calls the loadLibrary method, I get a java security error when I try to call the System.loadLibrary state...
Is there a legal way to add/remove permissions to Java security policy at runtime?
I am creating a socket connection with an unsigned applet to a different host and I'm getting java.security.AccessControlException: access denied If I sign this applet with either "self-cert" or "CA cert" does the applet gain the permissions to create a socket connection to a different host (not the same host it was downloaded from) and does a security message popup if its been certified by a ...
I'm using a 3rd party library (jar) in my java applet and I'm getting this error: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "*" "read,write") at java.security.AccessControlContext.checkPermission(Unknown Source) at java.security.AccessController.checkPermission(Unknown Source) at java.lang.SecurityManager.checkPermission(Unknown Source) ...
I'm trying to create an excel file and save to local file system using java applet. After sign the application, i can successfully create the file by directly invoking the applet. However, when i try to call the method from javascript, it failed without any error message. I'm wondering if there is any policy control to prevent java method to be called from javascript? <html> <script ...
I have a very simple java applet which reads the audio files data from user directory. I am using applet function in javascript. The problem is my applet closes after 15 seconds automatically and hence the function is not called. I have signed the applet. Certificate is also generated but applet (more precise file chooser ) closes after 15 seconds. Any help?
I'm trying to capture images by using webcam in an applet. The applet makes use of JMF. But I managed to run applet without actually installing JMF. I kept a button in HTML webpage so that when the button gets clicked, the applet should capture the image. But for me it is giving a java.lang.RuntimeException: No permission to capture from applets. How can this exception be resolved so an ...
I'm writing application for downloading CSV file from web and inserting data into table in database. The problem is that I need to setup proxy via System.setProperty("http.proxyHost", "http-proxy.domain.com"); and so on. Application works fine when I'm running it on local system, but the problem is when I launch it from JNLP. At first I have had problems with signing the JARs (I've managed it t...
I have the following class: public class TestApplet extends JApplet { private static final long serialVersionUID = -2137477433249866949L; private JTextArea display; @Override public void init() { //Create the text area and make it uneditable. display = new JTextArea( 1, 80 ); display.setEditable( false ); //Set the layout manager so that the ...
I have an applet with a function which returns a List of all Netdrives if this function is called by javascript. Currently I get a permission denied exception. Google told me: I have to sign it and create a policy file. But I am not getting forward. Could you tell me the steps, how to get this working? Greetings and thanks!
I'm trying to comunicate with a JNI service in the serverpart of my GWT application. The problem is that I'm getting a 'java.security.AccessControlException: access denied (java.lang.RuntimePermission loadLibrary.HelloWorld)' Error. I ´Google for the java Security and Permissions model and found a description on http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA...
All I have signed all the jar files including appletviewer.jar also . i am running latest version of JDK . I was running and viewing applet successfully till today afternoon , but suddenly i am getting this exception now . i can not understand what is happening ... please, help me asap. exception: Permission denied: null. java.lang.SecurityException: Permission denied: null at sun.plugin...
It goes like this, we need to detect if the display is a Projector (or if the system is connected to a Projetor). the catch is this should be done from within the browser. so is it possible to do so by using an Java Applet Flash ActiveX (this does constrains to a single browser, so not an option) searching so far only reaveals display resolution. expecting if there is something still out ...
  /*
   * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.security;
 

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.


 FilePermission perm = new FilePermission("/temp/testFile", "read");
 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:

 for (int i = m; i > 0; i--) {

     if (caller i's domain does not have the permission)
         throw AccessControlException

     else if (caller i is marked as privileged) {
         if (a context was specified in the call to doPrivileged)
             context.checkPermission(permission)
         return;
     
 };

 // Next, check the context inherited when the thread was created.
 // Whenever a new thread is created, the AccessControlContext at
 // that time is stored and associated with the new thread, as the
 // "inherited" context.

 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:

 somemethod() {
     ...normal code here...
     AccessController.doPrivileged(new PrivilegedAction<Void>() {
         public Void run() {
             // privileged code goes here, for example:
             System.loadLibrary("awt");
             return null; // nothing to return
         
     });
     ...normal code here...
 }}

PrivilegedAction is an interface with a single method, named run. 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:

 somemethod() {
     ...normal code here...
     String user = AccessController.doPrivileged(
         new PrivilegedAction<String>() {
         public String run() {
             return System.getProperty("user.name");
             
         });
     ...normal code here...
 }}

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:

 somemethod() throws FileNotFoundException {
     ...normal code here...
     try {
         FileInputStream fis = AccessController.doPrivileged(
         new PrivilegedExceptionAction<FileInputStream>() {
             public FileInputStream run() throws FileNotFoundException {
                 return new FileInputStream("someFile");
             
         });
     } catch (PrivilegedActionException e) {
         // e.getException() should be an instance of FileNotFoundException,
         // as only "checked" exceptions will be "wrapped" in a
         // PrivilegedActionException.
         throw (FileNotFoundException) e.getException();
     }
     ...normal code here...
  }}

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 acc = AccessController.getContext()

 

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:


 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:

 somemethod() {
     AccessController.doPrivileged(new PrivilegedAction<Object>() {
         public Object run() {
             // Code goes here. Any permission checks within this
             // run method will require that the intersection of the
             // callers protection domain and the snapshot's
             // context have the desired permission.
         
     }, acc);
     ...normal code here...
 }}

Author(s):
Li Gong
Roland Schemers
See also:
AccessControlContext
public final class AccessController {

    
Don't allow anyone to instantiate an AccessController
    private AccessController() { }

    
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.

Note that any DomainCombiner associated with the current AccessControlContext will be ignored while the action is performed.

    public static native <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.

This method preserves the current AccessControlContext's DomainCombiner (which may be null) while the action is performed.

Parameters:
action the action to be performed.
Returns:
the value returned by the action's run method.
Throws:
java.lang.NullPointerException if the action is null
Since:
1.6
See also:
doPrivileged(java.security.PrivilegedAction)
DomainCombiner
    public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) {
        DomainCombiner dc = null;
        if (acc == null || (dc = acc.getAssignedCombiner()) == null) {
            return AccessController.doPrivileged(action);
        }
        return AccessController.doPrivileged(actionpreserveCombiner(dc));
    }


    
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:
java.lang.NullPointerException if the action is null
See also:
doPrivileged(java.security.PrivilegedAction)
doPrivileged(java.security.PrivilegedExceptionAction,java.security.AccessControlContext)
    public static native <T> T doPrivileged(PrivilegedAction<T> action,
                                            AccessControlContext context);

    
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.

Note that any DomainCombiner associated with the current AccessControlContext will be ignored while the action is performed.

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
java.lang.NullPointerException if the action is null
See also:
doPrivileged(java.security.PrivilegedAction)
doPrivileged(java.security.PrivilegedExceptionAction,java.security.AccessControlContext)
doPrivilegedWithCombiner(java.security.PrivilegedExceptionAction)
DomainCombiner
    public static native <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.

This method preserves the current AccessControlContext's DomainCombiner (which may be null) while the action is performed.

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
java.lang.NullPointerException if the action is null
Since:
1.6
See also:
doPrivileged(java.security.PrivilegedAction)
doPrivileged(java.security.PrivilegedExceptionAction,java.security.AccessControlContext)
DomainCombiner
    public static <T> T doPrivilegedWithCombiner
        (PrivilegedExceptionAction<T> actionthrows PrivilegedActionException {
        DomainCombiner dc = null;
        if (acc == null || (dc = acc.getAssignedCombiner()) == null) {
            return AccessController.doPrivileged(action);
        }
        return AccessController.doPrivileged(actionpreserveCombiner(dc));
    }

    
preserve the combiner across the doPrivileged call
    private static AccessControlContext preserveCombiner
                                        (DomainCombiner combiner) {

        
callerClass[0] = Reflection.getCallerClass callerClass[1] = AccessController.preserveCombiner callerClass[2] = AccessController.doPrivileged callerClass[3] = caller
        final Class callerClass = sun.reflect.Reflection.getCallerClass(3);
        ProtectionDomain callerPd = doPrivileged
            (new PrivilegedAction<ProtectionDomain>() {
            public ProtectionDomain run() {
                return callerClass.getProtectionDomain();
            }
        });
        // perform 'combine' on the caller of doPrivileged,
        // even if the caller is from the bootclasspath
        ProtectionDomain[] pds = new ProtectionDomain[] {callerPd};
        return new AccessControlContext(combiner.combine(pdsnull), combiner);
    }


    
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
java.lang.NullPointerException if the action is null
See also:
doPrivileged(java.security.PrivilegedAction)
doPrivileged(java.security.PrivilegedExceptionAction,java.security.AccessControlContext)
    public static native <T> T
        doPrivileged(PrivilegedExceptionAction<T> action,
                     AccessControlContext context)
        throws PrivilegedActionException;

    
Returns the AccessControl context. i.e., it gets the protection domains of all the callers on the stack, starting at the first class with a non-null ProtectionDomain.

Returns:
the access control context based on the current stack or null if there was only privileged system code.
    private static native AccessControlContext getStackAccessControlContext();

    
Returns the "inherited" AccessControl context. This is the context that existed when the thread was created. Package private so AccessControlContext can use it.
    static native AccessControlContext getInheritedAccessControlContext();

    
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
    public static AccessControlContext getContext()
    {
        if (acc == null) {
            // all we had was privileged system code. We don't want
            // to return null though, so we construct a real ACC.
            return new AccessControlContext(nulltrue);
        } else {
            return acc.optimize();
        }
    }

    
Determines whether the access request indicated by the specified permission should be allowed or denied, based on the current AccessControlContext and security policy. 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.
java.lang.NullPointerException if the specified permission is null and is checked based on the security policy currently in effect.
    public static void checkPermission(Permission perm)
                 throws AccessControlException
    {
        //System.err.println("checkPermission "+perm);
        //Thread.currentThread().dumpStack();
        if (perm == null) {
            throw new NullPointerException("permission can't be null");
        }
        // if context is null, we had privileged system code on the stack.
        if (stack == null) {
            Debug debug = AccessControlContext.getDebug();
            boolean dumpDebug = false;
            if (debug != null) {
                dumpDebug = !Debug.isOn("codebase=");
                dumpDebug &= !Debug.isOn("permission=") ||
                    Debug.isOn("permission=" + perm.getClass().getCanonicalName());
            }
            if (dumpDebug && Debug.isOn("stack")) {
                Thread.currentThread().dumpStack();
            }
            if (dumpDebug && Debug.isOn("domain")) {
                debug.println("domain (context is null)");
            }
            if (dumpDebug) {
                debug.println("access allowed "+perm);
            }
            return;
        }
        AccessControlContext acc = stack.optimize();
        acc.checkPermission(perm);
    }
New to GrepCode? Check out our FAQ X