security

AWTPermission example

This is an example of how to create an AWTPermission. AWTPermissions are permissions granted for the AWT Package of Java. In the AWTPermission API there is a list of all the possible AWTPermission target names, and for each one, there is a description of what the permission allows and a discussion of the risks of granting code the permission. In short, to create and check permissions over the AWT you should:

  • Create a new AWTPermission with a specified name, that indicates a specific permission.
  • Use the checkPermission(Permission perm) API method of the AccessController, in order to check whether the access request indicated by the specific permission should be allowed or not,

as shown in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.awt.AWTPermission;
import java.io.FilePermission;
import java.security.AccessControlException;
import java.security.AccessController;

public class Main {

    public static void main(String args[]) {

  try {


AWTPermission appl = new AWTPermission("myRestrictedAwt");


AccessController.checkPermission(appl);

  } catch (AccessControlException ex) {


System.out.println("Access denied");


  }
    }
}

Output:

Access denied

 
This was an example of how to create and check an AWTPermission in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button