|
|
|
|||
|
Installing Your Security ManagerOnce you've completed writing yourSecurityManager subclass,
you can install it as the current security manager for your
Java application. You do this with the setSecurityManager()
method from the System class.
Here's a small test application, SecurityManagerTest, that installs the PasswordSecurityManager class from the previous page as the current security manager. Then to verify that the security manager is in place and operational, the SecurityManagerTest application opens two files--one for reading and one for writing--and copies the contents of the first file into the second.
The
The bold line in the previous code snippet creates a new instance of the
PasswordSecurityManager class with the password "Booga Booga". This instance
is passed to System's setSecurityManager() method, which
installs the object as the current security manager for the running
application. This security manager will remain in effect for the duration
of the execution of this application.
You can set the security manager for your application only once. In other
words, your Java application can invoke
The rest of the program copies the contents of this file
The bold lines in the previous code snippet are restricted file system
accesses. These method calls will result in a call to PasswordSecurityManager's
checkAccess() method.
Running the Test ProgramWhen you run the SecurityManagerTest application, you are prompted twice for a password: once when the application opens the input file and once when the application opens the output file. If you type in the correct password, the access is granted--the file object--and the application proceeds to the next statement. If you type in an incorrect password, | |||
|