Enabling the Security Manager : SecurityManager « Security « Java






Enabling the Security Manager

   


public class Main {
  public static void main(String[] argv) throws Exception {
    System.setProperty("java.version", "data");

    try {
      SecurityManager sm = new SecurityManager();
      System.setSecurityManager(sm);
    } catch (SecurityException se) {
      se.printStackTrace();
    }

    // no longer possible; an AccessControlException is thrown
    System.setProperty("java.version", "malicious data");
  }
}
/*Exception in thread "main" java.security.AccessControlException: access denied (java.util.PropertyPermission java.version write)
  at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
  at java.security.AccessController.checkPermission(AccessController.java:546)
  at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
  at java.lang.System.setProperty(System.java:727)
  at Main.main(Main.java:13)
*/

   
    
    
  








Related examples in the same category

1.Use SecurityManager to check AWT permission and file permission
2.Listing All Permissions Granted to Classes Loaded from a URL or Directory
3.extends SecurityManager
4.Security Manager TestSecurity Manager Test
5.The security manager can be installed from the command line:
6.Security Support