grant permission within code

Hi, guys,

I have to modify the java.policy file before I run my applet for the first time.

How can I grant permissions from within my code rather

than manually editing the file (I don't want the client to do anything other than run my applet).

Anybody can help? Thank you in advance.

Janet

[341 byte] By [kfsong] at [2007-9-26 3:55:09]
# 1

> Hi, guys,

> I have to modify the java.policy file before I run my

> applet for the first time.

> How can I grant permissions from within my code

> rather

> than manually editing the file (I don't want the

> client to do anything other than run my applet).

> Anybody can help? Thank you in advance.

>

> Janet

>

What you can do for a file permission:

FilePermission p = new FilePermission("/tmp/*", "read, write");

You can find out yourself other types of permissions,right?

To get the policy:

Policy currentPolicy = Policy.getPolicy();

To find out all the permissions:

cPermissionCollection permissions = currentPolicy.getPermissions(codeBase);

In fact, each class has a protetion domain, an object that encapsulates both the code source and the collection of permissions of the class. Teh getProtectionDomain method of the "Class" class returns that domain:

ProtectionDomain domain = anObject.getClass().getProtetionDomain();

Have fun.

Burt

bujinwang at 2007-6-29 12:44:57 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 2

Hi,

I tried to set permission from within my code, but even the getPolicy method will cause an "java.security.SecurityPermission" exception. What can I do now?

My code is as following:

Policy isgPolicy;

URL codeBase;

try{

isgPolicy=Policy.getPolicy();

codeBase=new URL("bsp/rm/gui");

PermissionCollection isgPC=isgPolicy.getPermissions(new CodeSource(codeBase,null));

isgPC.add(new FilePermission("java.lang.RuntimePermission","writeFileDescriptor"));

}catch(Exception e){};

Thank you for help.

Janet

kfsong at 2007-6-29 12:44:57 > top of Java-index,Security,Other Security APIs, Tools, and Issues...