Class SecurityManager
- java.lang.Object
-
- java.lang.SecurityManager
-
public class SecurityManager extends Object
The security manager is a class that allows applications to implement a security policy. It allows an application to determine, before performing a possibly unsafe or sensitive operation, what the operation is and whether it is being attempted in a security context that allows the operation to be performed. The application can allow or disallow the operation.The
SecurityManagerclass contains many methods with names that begin with the wordcheck. These methods are called by various methods in the Java libraries before those methods perform certain potentially sensitive operations. The invocation of such acheckmethod typically looks like this:SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkXXX(argument, . . . ); }The security manager is thereby given an opportunity to prevent completion of the operation by throwing an exception. A security manager routine simply returns if the operation is permitted, but throws a
SecurityExceptionif the operation is not permitted. The only exception to this convention ischeckTopLevelWindow, which returns abooleanvalue.The current security manager is set by the
setSecurityManagermethod in classSystem. The current security manager is obtained by thegetSecurityManagermethod.The special method
checkPermission(java.security.Permission)determines whether an access request indicated by a specified permission should be granted or denied.If a requested access is allowed,
checkPermissionreturns quietly. If denied, aSecurityExceptionis thrown.As of Java 2 SDK v1.2, the default implementation of each of the other
checkmethods inSecurityManageris to call theSecurityManager checkPermissionmethod to determine if the calling thread has permission to perform the requested operation.Note that the
checkPermissionmethod with just a single permission argument always performs security checks within the context of the currently executing thread.Permissions fall into these categories: Runtime and Property. The classes managing these various permission categories are
java.lang.RuntimePermission,java.util.PropertyPermission,Some of the permission classes have an "actions" list that tells the actions that are permitted for the object.
Other permission classes are for "named" permissions - ones that contain a name but no actions list; you either have the named permission or you don't.
-
-
Constructor Summary
Constructors Constructor Description SecurityManager()Constructs a newSecurityManager.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheckAccess(Thread t)Throws aSecurityExceptionif the calling thread is not allowed to modify the thread argument.voidcheckPermission(Permission perm)Throws aSecurityExceptionif the requested access, specified by the given permission, is not permitted based on the security policy currently in effect.
-
-
-
Constructor Detail
-
SecurityManager
public SecurityManager()
Constructs a newSecurityManager.If there is a security manager already installed, this method first calls the security manager's
checkPermissionmethod with theRuntimePermission("createSecurityManager")permission to ensure the calling thread has permission to create a new security manager. This may result in throwing aSecurityException.- Throws:
SecurityException- if a security manager already exists and itscheckPermissionmethod doesn't allow creation of a new security manager.- See Also:
System.getSecurityManager(),checkPermission,RuntimePermission
-
-
Method Detail
-
checkAccess
public void checkAccess(Thread t)
Throws aSecurityExceptionif the calling thread is not allowed to modify the thread argument.This method is invoked for the current security manager by the
setPriorityandsetNamemethods of classThread.If the thread argument is a system thread then this method calls
checkPermissionwith theRuntimePermission("modifyThread")permission. If the thread argument is not a system thread, this method just returns silently.Applications that want a stricter policy should override this method. If this method is overridden, the method that overrides it should additionally check to see if the calling thread has the
RuntimePermission("modifyThread")permission, and if so, return silently. This is to ensure that code granted that permission is allowed to manipulate any thread.If this method is overridden, then
super.checkAccessshould be called by the first statement in the overridden method, or the equivalent security check should be placed in the overridden method.- Parameters:
t- the thread to be checked.- Throws:
SecurityException- if the calling thread does not have permission to modify the thread.NullPointerException- if the thread argument isnull.- See Also:
setName,setPriority,checkPermission
-
checkPermission
public void checkPermission(Permission perm)
Throws aSecurityExceptionif the requested access, specified by the given permission, is not permitted based on the security policy currently in effect.- Parameters:
perm- the requested permission.- Throws:
SecurityException- if access is not permitted based on the current security policy.NullPointerException- if the permission argument isnull.
-
-