|
|
The custom security manager for this program prompts the
end user to enter a password before it allows public static void main(String[] args){ BufferedReader buffy = new BufferedReader( new InputStreamReader(System.in)); try { System.setSecurityManager( new PasswordSecurityManager("pwd", buffy)); } catch (SecurityException se) { System.err.println("SecurityManager already set!"); } The PasswordSecurityManager ClassThe PasswordSecurityManager class declares two private instance variables, which are initialized by the constructor when the custom security manager is installed. Thepassword instance variable contains the
actual password, and the buffy instance variable
is an input buffer that stores the end user's password input.
public class PasswordSecurityManager extends SecurityManager{ private String password; private BufferedReader buffy; public PasswordSecurityManager(String p, BufferedReader b){ super(); this.password = p; this.buffy = b; }The accessOK method prompts
the end user for a password, verifies the password, and returns
true if the password is correct and false
if it is not.
private boolean accessOK() { int c; String response; System.out.println("Password, please:"); try { response = buffy.readLine(); if (response.equals(password)) return true; else return false; } catch (IOException e) { return false; } } Verify AccessTheSecurityManager parent class provides methods
to verify file system read and write access. The checkRead
and checkWrite methods each have a version that accepts
a String and another verion that accepts a file descriptor.
This example overrides only the public void checkRead(String filename) { if((filename.equals(File.separatorChar + "home" + File.separatorChar + "monicap" + File.separatorChar + "text2.txt"))){ if(!accessOK()){ super.checkRead(filename); throw new SecurityException("No Way!"); } else { FilePermission perm = new FilePermission( File.separatorChar + "home" + File.separatorChar + "monicap" + File.separatorChar + "text2.txt", "read"); checkPermission(perm); } } } public void checkWrite(String filename) { if((filename.equals(File.separatorChar + "home" + File.separatorChar + "monicap" + File.separatorChar + "text.txt"))){ if(!accessOK()){ super.checkWrite(filename); throw new SecurityException("No Way!"); } else { FilePermission perm = new FilePermission( File.separatorChar + "home" + File.separatorChar + "monicap" + File.separatorChar + "text.txt" , "write"); checkPermission(perm); } } } }The checkWrite method is called before the end user
input is written to the output file. This is because
the FileOutputStream class calls
SecurityManager.checkWrite first.
The custom implementation for Policy FileHere is the policy file theFileIO program
needs for its read and write operations. It also grants
permission to the custom security manager to access the event
queue on behalf of the application and show the application window
without the warning banner.
grant { permission java.io.FilePermission "${user.home}/text.txt", "write"; permission java.util.PropertyPermission "user.home", "read"; permission java.io.FilePermission "${user.home}/text2.txt", "read"; permission java.awt.AWTPermission "accessEventQueue"; permission java.awt.AWTPermission "showWindowWithoutWarningBanner"; }; Run the FileIO ProgramHere is how to run theFileIO
program with the policy file:
java -Djava.security.policy=polfile FileIO Reference InformationAppendix A: Security and Permissions describes the available permissions and explains the consequences of granting permissions. One way to use this information is to help you limit what permissions a given applet or application might need to successfully execute. Another way to use this information is to educate yourself on the ways in which a particular permission can be exploited by malicious code.
Appendix B: Classes, Methods, and Permissions
provides lists of Java 2 platform software methods that are implemented to
perform security access checks, the permission each requires, and
the You can use this reference to write your own security manager implementations or when you implement abstract methods that perform security-related tasks.
Appendix C: SecurityManager Methods lists
the permissions checked for by the [TOP] |