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.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

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