This class represents a high-level security context (low level
security being addressed by the system security manager).
Applications may extend this base class to address specific security
requirements. For example:[code]
// This class defines custom policy with regards to database access.
public abstract class DatabaseAccess extends SecurityContext {
public static boolean isReadAllowed(Table table) {
SecurityContext policy = SecurityContext.current();
return (policy instanceof DatabaseAccess.Permission) ?
((DatabaseAccess.Permission)policy).isReadable(table) : false;
}
public interface Permission {
boolean isReadable(Table table);
boolean isWritable(Table table);
}
}[/code]
The use of interfaces (such as Permission above) makes
it easy for custom policies to support any security actions.
For example:[code]
class Policy extends SecurityContext implements DatabaseAccess.Permission, FileAccess.Permission {
public boolean isReadable(Table table) {
return !table.isPrivate();
}
public boolean isWritable(Table table) {
return Session.getSession().getUser().isAdministrator();
}
public boolean isReadable(File file) {
return true;
}
public boolean isWritable(File file) {
return false;
}
}
...
Policy localPolicy = new Policy();
SecurityContext.enter(localPolicy); // Current thread overrides default policy (configurable)
try { // (if allowed, ref. SecurityContext.isReplaceable())
...
DatabaseAccess.isReadAllowed(table);
...
FileAccess.isWriteAllowed(file);
...
} finally {
SecurityContext.exit();
}[/code]
The default permissions managed by the
SecurityContext.DEFAULT implementation
are the permission to
SecurityContext.isReplaceable replace the current security
context by default) and the permission to
SecurityContext.isModifiable modify
all the application
javolution.lang.Configurable.Logic configuration settings.
author: Jean-Marie Dautelle version: 5.2, August 5, 2007 |