For Programmers: Free Programming Magazines  


Home > Archive > Java Security > December 2004 > SecurityManager Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SecurityManager Question
Robin Hollenstein

2004-12-16, 4:09 pm

John,
I think a SecurityManager cannot be removed once its set. Look at this
code:

public class exps
{

public static void main(String[] args)
{
System.setSecurityManager(new SecurityManager());
System.out.println("First SecurityManager set");
System.setSecurityManager(null);
System.out.println("Second SecurityManager set");
System.out.println(System.getProperty("user.name"));
}

}

I ran it and it gave me the following result:

U:\java exps
First SecurityManager set
Exception in thread "main" java.security.AccessControlException:
access denied (
java.lang.RuntimePermission setSecurityManager)
at java.security.AccessControlContext.checkPermission(Unknown
Source)
at java.security.AccessController.checkPermission(Unknown
Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.System.setSecurityManager0(Unknown Source)
at java.lang.System.setSecurityManager(Unknown Source)
at exps.main(exps.java:8)

U:\

I would also recommend you to check the API-Documentation, specially
the classes <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html">System</a>
and <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/SecurityManager.html">SecurityManager</a>

Hope I helped,

R. Hollenstein
Selvamohan Neethiraj

2004-12-18, 3:56 pm

You can set another security manager as long as your current security
manager allows the following two permissions:

java.lang.RuntimePermission createSecurityManager
java.lang.RuntimePermission setSecurityManager

========================================
=============================
Try the following code ....
========================================
=============================
public class ChangeSecMgr
{

public static void main(String[] args)
{
System.err.println("Current Security Manager: [" +
System.getSecurityManager() + "]") ;
System.setSecurityManager(new RelaxedSecurityManager()) ;
System.err.println("Current Security Manager: [" +
System.getSecurityManager() + "]") ;
System.setSecurityManager(new SecurityManager()) ;
System.err.println("Current Security Manager: [" +
System.getSecurityManager() + "]") ;
}
}

class RelaxedSecurityManager extends SecurityManager
{
public void checkPermission(Permission perm)
{
System.err.println("Permission:[" + perm + "] is ok");
return ;
}
}
========================================
=============================


"Robin Hollenstein" <r.hollenstein@csm.edu.mx> wrote in message
news:621b66f7.0412160916.636963b3@posting.google.com...
> John,
> I think a SecurityManager cannot be removed once its set. Look at this
> code:
>
> public class exps
> {
>
> public static void main(String[] args)
> {
> System.setSecurityManager(new SecurityManager());
> System.out.println("First SecurityManager set");
> System.setSecurityManager(null);
> System.out.println("Second SecurityManager set");
> System.out.println(System.getProperty("user.name"));
> }
>
> }
>
> I ran it and it gave me the following result:
>
> U:\java exps
> First SecurityManager set
> Exception in thread "main" java.security.AccessControlException:
> access denied (
> java.lang.RuntimePermission setSecurityManager)
> at java.security.AccessControlContext.checkPermission(Unknown
> Source)
> at java.security.AccessController.checkPermission(Unknown
> Source)
> at java.lang.SecurityManager.checkPermission(Unknown Source)
> at java.lang.System.setSecurityManager0(Unknown Source)
> at java.lang.System.setSecurityManager(Unknown Source)
> at exps.main(exps.java:8)
>
> U:\
>
> I would also recommend you to check the API-Documentation, specially
> the classes <a
> href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html">System</a>
> and <a
> href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/SecurityManager.html">SecurityManager</a>
>
> Hope I helped,
>
> R. Hollenstein



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com