security

Get all permissions granted to classes loaded from a specific URL

In this example we shall show you how to get all permissions granted to classes loaded from a specific URL. To get the permissions from a specific URL one should perform the following steps:

  • Create a new URL to be used as a code base.
  • Initialize the URL with a String pathname, which can be either a pathname to a URL or a pathname to a directory.
  • Create a CodeSource associated with the specified URL location and a set of Certificates.
  • Create a PermissionCollection, by getting the permissions for the specific CodeSource, using the getPermissions(CodeSource codesource) API method of the installed Policy Object.
  • Create an enumeration of all the Permission objects, using the elements() API method of the PermissionCollection and return each one of the permissions to a new Permission Object, using the nextElement() API method of the Enumeration,

as described in the code snippet below.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.javacodegeeks.snippets.core;
  
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Policy;
import java.security.cert.Certificate;
import java.util.Enumeration;
 
public class GetGrantedPermissionsForURL {
  
  public static void main(String[] args) {
  
    URL codebase = null;
     
    try {
 
 
 
// Get permissions for a URL (example)
 
// codebase = new URL("http://www.javacodegeeks.com/");
 
 
// Get permissions for a directory
 
codebase = new File(System.getProperty("user.home")).toURL();
 
 
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
 
    // Construct a code source from the code base
    CodeSource codeSource = new CodeSource(codebase, new Certificate[] {});
 
    // Get all granted permissions
    PermissionCollection permissionCollection = Policy.getPolicy().getPermissions(codeSource);
 
    Enumeration permissions = permissionCollection.elements();
    while (permissions.hasMoreElements()) {
 
Permission permission = (Permission) permissions.nextElement();
 
System.out.println(permission.getName());
    }
  }
}

Example Output:

stopThread
localhost:1024-
<all permissions>
line.separator
java.vm.version
java.vm.specification.version
java.vm.specification.vendor
java.vendor.url
java.vm.name
os.name
java.vm.vendor
path.separator
java.specification.name
os.version
os.arch
java.class.version
java.version
file.separator
java.vendor
java.vm.specification.name
java.specification.version
java.specification.vendor

 
This was an example of how to get all permissions granted to classes loaded from a specific URL in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
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 ....
I agree to the Terms and Privacy Policy

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron 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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button