Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use the following approach while running some Groovy script within an application. I obviously want to prevent the script from running (intentionally or unintentionally) a System.exit</p> <p>I install a java SecurityManager in the usual way:</p> <pre><code>-Djava.security.manager -Djava.security.policy=&lt;policy file&gt; </code></pre> <p>In the <code>&lt;policy file&gt;</code> I give my application all permissions (I do fully trust my application), i.e.: </p> <pre><code>grant { permission java.security.AllPermission; }; </code></pre> <p>I limit the capabilities in the part where the Groovy script is run:</p> <pre><code>list = AccessController.doPrivileged(new PrivilegedExceptionAction&lt;List&lt;Stuff&gt;&gt; () { public List&lt;Stuff&gt; run() throws Exception { return groovyToExecute.someFunction(); } }, allowedPermissionsAcc); </code></pre> <p>The <code>allowedPermissionsAcc</code> doesn't change and therefore I create them in a static block</p> <pre><code>private static final AccessControlContext allowedPermissionsAcc; static { // initialization of the allowed permissions PermissionCollection allowedPermissions = new Permissions(); allowedPermissions.add(new RuntimePermission("accessDeclaredMembers")); // ... &lt;many more permissions here&gt; ... allowedPermissionsAcc = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, allowedPermissions)}); } </code></pre> <p>Now the tricky part is to find the right permissions. </p> <p>If you want to allow access to certain libraries, you will quickly realize that they have not been written with a Security Manager in mind and don't handle one very gracefully, and finding out which permissions they need can be quite tricky. You will run into additional problems if you want to run UnitTests through the Maven Surefire plugin, or run on different platforms, like Linux/Windows, since the behavior can vary :-(. But those issues are another topic</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload