Providing Your Own Security Manager |
To write your own security manager, you must create a subclass of the SecurityManager class. Your SecurityManager subclass overrides various methods from SecurityManager to customize the verifications and approvals needed by your Java application.This page will walk through an example security manager subclass that restricts certain Thread accesses. The following is a list of ThreadGroup methods that use the security manager to approve the operation before proceeding.
And the following is a list of Thread methods that use the security manager to approve the operation before proceeding.
ThreadGroup(ThreadGroup parent, String name)
setDaemon()
setMaxPriority()
stop()
suspend()
resume()
destroy()
To get approval from the security manager these methods call the security manager's
- all constructors
stop()
suspend()
resume()
setPriority()
setName()
setDaemon()
checkAccess()
method. If the security manager approves the operation thencheckAccess()
returns, otherwisecheckAccess()
throws a SecurityException.Thus, our example SecurityManager subclass that wishes to impose a stricter policy on Thread accesses, must override SecurityManager's
checkAccess()
method. SecurityManager provides two versions ofcheckAccess()
: one that takes a Thread argument and one that takes a ThreadGroup argument. Each of these methods verifies whether the current thread is allowed to perform one of the restricted methods on the argument. A policy frequently implemented by browsers is that a thread or thread group can only modify another thread or threadgroup if they are in the same group.The policy implemented by our example prompts the user for a password. If the password is correct then the access is allowed. While this is an unlikely policy for a real application concerned with thread accesses, it does provide some visual feedback as to when the SecurityManager is invoked to approve thread accesses and illustrates the point none-the-less.
All security managers must be a subclass of SecurityManager. Thus, our SecretPasswordSMgr class extends SecurityManager.
Next, SecretPasswordSMgr declares a private instance variableclass SecretPasswordSMgr extends SecurityManager { . . . }password
to contain the password that the user must enter in order to allow the restricted thread accesses. The password is set upon construction:The next method in the SecretPasswordSMgr class is a private helper method namedSecretPasswordSMgr(String password) { super(); this.password = password; }accessOK()
. This method prompts the user for a password and verifies it. If the user entered an valid password the method returns true, false otherwise.Finally at the end of the SecretPasswordSMgr class are the two overridenprivate boolean accessOK() { int c; DataInputStream dis = new DataInputStream(System.in); String response; System.out.println("What's the secret password?"); try { response = dis.readLine(); if (response.equals(password)) return true; else return false; } catch (IOException e) { return false; } }checkAccess()
methods:Bothpublic void checkAccess(Thread g) { if (!accessOK()) throw new SecurityException("Not!"); } public void checkAccess(ThreadGroup g) { if (!accessOK()) throw new SecurityException("Not Even!"); }checkAccess()
methods callaccessOK()
to prompt the user for a password. If access is not OK, thencheckAccess()
throws a SecurityException. Otherwise,checkAccess()
returns normally. Note that SecurityException is a runtime exception, and as such does not need to be declared in thethrows
clause of these methods.Note that the
checkAccess()
method does not know which restricted thread accesses is being attempted. It must make the decision to approve or disapprove the access based solely on the current thread and the thread or thread group passed into the method.
checkAccess()
is just one of many of SecurityManager'scheckXXX()
methods that verify various types of accesses. You can override any number ofcheckXXX()
methods to implement your security policy. You do not need to override all of SecurityManager'scheckXXX()
methods, just the ones that you want to customize. The default behaviour for thecheckXXX()
is quite lenient and thus should be sufficient for your needs if you aren't interested in overriding them explicitly.All of SecurityManager's
checkXXX()
methods operate in the same way:Make sure that you implement your overriden
- If access is allowed, the method returns.
- If access is not allowed, the method throws a SecurityException.
checkXXX()
methods in this manner.Well, that's it for our SecurityManager subclass. As you can see implementing a SecurityManager is simple. You just:
The trick is determining which methods to override and implementing your security policy. More About the SecurityManager Class will help you figure out which methods you should override depending on what types of operations you'd like to protect. The next page shows you how to install the SecretPasswordSMgr class as the security manager on-duty for your Java application.
- Create a SecurityManager subclass
- Override a few methods
See also
java.lang.SecurityManager
java.lang.SecurityException
Providing Your Own Security Manager |