Define your own security manager : SecurityManager « Security « Java Tutorial






class CustomSecurityManager extends SecurityManager {
  public CustomSecurityManager() {
    super();
  }

  public void checkRead(String fileName) {
    if (fileName != null && fileName.endsWith(".java")) {
      throw new SecurityException(" You are not allowed to read " + " file names ending with .java");
    }
    super.checkRead(fileName);
  }

  public void checkWrite(String fileName) {
    if (fileName != null && fileName.endsWith(".java")) {
      throw new SecurityException(" You are not allowed to write "
          + " file names ending with .java");
    }
    super.checkWrite(fileName);
  }

  public void checkDelete(String fileName) {
    if (fileName != null && fileName.endsWith(".java")) {
      throw new SecurityException(" You are not allowed to delete "
          + " file names ending with .java");
    }
    super.checkDelete(fileName);
  }
}

public class MainClass {
  public static void main() {
    System.setSecurityManager(new CustomSecurityManager());
    SecurityManager secMgr = System.getSecurityManager();
    if (secMgr != null) {
      secMgr.checkRead("fileName");
    }
  }
}








36.41.SecurityManager
36.41.1.extends SecurityManager
36.41.2.Use SecurityManager to check AWT permission and file permission
36.41.3.Listing All Permissions Granted to Classes Loaded from a URL or Directory
36.41.4.Define your own security manager
36.41.5.Enabling the Security Manager
36.41.6.To specify an additional policy file, set the java.security.policy system property at the command line:
36.41.7.To ignore the policies in the java.security file, and use the specified policy, use '==' instead of '='