How To Create New Java Permissions

Posted on November 6, 2012. Filed under: java/j2EE |


Code Example of Creating New Java permissions and use it in policy file.

##cat /tmp/testfile.txt
test SeurityManager
##vi HelloFile.java
##vi CustomerPermission.java
##
##cat HelloFile.java
import java.io.*;
import java.security.AccessController;
public class HelloFile {
public static void main(String [] args) {
if(System.getSecurityManager()==null){
System.setSecurityManager(new SecurityManager());
}
CustomerPermission cp = new CustomerPermission(“cust”);
AccessController.checkPermission(cp);
try{
BufferedReader br = new BufferedReader(new FileReader(“/tmp/testfile.txt”));
System.out.println(br.readLine());
}catch(Exception e){
e.printStackTrace();
}
}
}
##
##
##cat CustomerPermission.java
import java.security.BasicPermission;

public class CustomerPermission extends BasicPermission{
public CustomerPermission(String name){
super(name);
}

}
##
##javac HelloFile.java CustomerPermission.java
##
##cat HelloFile.policy
grant codeBase “file:/Users/homenetwork/*” {
permission java.io.FilePermission “/tmp/*”, “read”;
};
##

##java -Djava.security.policy=/Users/homenetwork/HelloFile.policy HelloFile

Exception in thread “main” java.security.AccessControlException: access denied (CustomerPermission cust)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:549)
at HelloFile.main(HelloFile.java:9)
##vi HelloFile.policy
##cat HelloFile.policy
grant codeBase “file:/Users/homenetwork/*” {
permission java.io.FilePermission “/tmp/*”, “read”;
permission CustomerPermission “cust”;
};
##
##java -Djava.security.policy=/Users/homenetwork/HelloFile.policy HelloFile
test SeurityManager
##

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: