📄 javalang.doc16.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.lang </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javalang.doc15.html">Prev</a> | <a href="javalang.doc17.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="46274"></a>
<center><h1>20.17 The Class <code>java.lang.SecurityManager</code></h1></center>
<pre><a name="46275"></a>public abstract class <code><b>SecurityManager</b></code> {
<a name="3125"></a> protected boolean <code><b>inCheck</b></code>;
<a name="3126"></a> protected <code><b>SecurityManager</b></code>()<br>
throws SecurityException;
<a name="3127"></a> protected Class[] <code><b>getClassContext</b></code>();
<a name="3129"></a> protected int <code><b>classDepth</b></code>(String name);
<a name="30021"></a> protected boolean <code><b>inClass</b></code>(String name);
<a name="30015"></a> protected ClassLoader <code><b>currentClassLoader</b></code>();
<a name="3130"></a> protected int <code><b>classLoaderDepth</b></code>();
<a name="3132"></a> protected boolean <code><b>inClassLoader</b></code>();
<a name="29969"></a> public boolean <code><b>getInCheck</b></code>();
<a name="3134"></a> public void <code><b>checkCreateClassLoader</b></code>()<br>
throws SecurityException;
<a name="3135"></a> public void <code><b>checkAccess</b></code>(Thread t)<br>
throws SecurityException;
<a name="3136"></a> public void <code><b>checkAccess</b></code>(ThreadGroup g)<br>
throws SecurityException;
<a name="3137"></a> public void <code><b>checkExit</b></code>(int status)<br>
throws SecurityException;
<a name="3138"></a> public void <code><b>checkExec</b></code>(String cmd)<br>
throws SecurityException;
<a name="29903"></a> public void <code><b>checkPropertiesAccess</b></code>()<br>
throws SecurityException;
<a name="29904"></a> public void <code><b>checkPropertyAccess</b></code>(String key)<br>
throws SecurityException;
<a name="3139"></a> public void <code><b>checkLink</b></code>(String libname)<br>
throws SecurityException;
<a name="3140"></a> public void <code><b>checkRead</b></code>(int fd)<br>
throws SecurityException;
<a name="3141"></a> public void <code><b>checkRead</b></code>(String file)<br>
throws SecurityException;
<a name="3142"></a> public void <code><b>checkWrite</b></code>(int fd)<br>
throws SecurityException;
<a name="3143"></a> public void <code><b>checkWrite</b></code>(String file)<br>
throws SecurityException;
<a name="3144"></a> public void <code><b>checkDelete</b></code>(String file)<br>
throws SecurityException;
<a name="3145"></a> public void <code><b>checkConnect</b></code>(String host, int port)<br>
throws SecurityException;
<a name="3146"></a> public void <code><b>checkListen</b></code>(int port)<br>
throws SecurityException;
<a name="3147"></a> public void <code><b>checkAccept</b></code>(String host, int port)<br>
throws SecurityException;
<a name="3154"></a> public void <code><b>checkSetFactory</b></code>()<br>
throws SecurityException;
<a name="29910"></a> public boolean <code><b>checkTopLevelWindow</b></code>()<br>
throws SecurityException;
<a name="30111"></a> public void <code><b>checkPackageAccess</b></code>(String packageName)<br>
throws SecurityException;
<a name="30115"></a> public void <code><b>checkPackageDefinition</b></code>(String packageName)<br>
throws SecurityException;
<a name="3160"></a>}
</pre><a name="5903"></a>
A running Java program may have a security manager, which is an instance of class <code>SecurityManager</code>. The current security manager is the one returned by the method invocation <code>System.getSecurityManager()</code> <a href="javalang.doc17.html#3205">(§20.18.4)</a>.<p>
<a name="5917"></a>
The <code>SecurityManager</code> class contains a large number of methods whose names begin with "<code>check</code>". They are called by various methods throughout the Java libraries before those methods perform certain sensitive operations. The invocation of such a check method typically looks like this:<p>
<pre><a name="5921"></a>
SecurityManager security = System.getSecurityManager();
<a name="5922"></a>if (security != null) {
<a name="5933"></a> security.check<i>XXX</i>(<i>arguments</i>);
<a name="5924"></a>}
</pre><a name="5918"></a>
The security manager is thereby given an opportunity to prevent completion of the operation by throwing an exception. The usual convention is that a security manager checking routine simply returns if the operation is permitted, or throws a <code>SecurityException</code> if the operation is not permitted. In one case, namely <code>checkTopLevelWindow</code> <a href="javalang.doc16.html#29924">(§20.17.27)</a>, the checking routine must return a <code>boolean</code> value to indicate one of two levels of permission.<p>
<a name="5906"></a>
<p><font size=+1><strong>20.17.1 </strong> <code>protected boolean <code><b>inCheck</b></code> = false;</code></font>
<p>
<a name="29935"></a>
By convention, this field should be assigned the value <code>true</code> whenever a security
check is in progress. This matters when one of the checking routines needs to call
outside code to do its work. Outside code can then use the method <code>getInCheck</code>
<a href="javalang.doc16.html#29978">(§20.17.9)</a> to test the status of this flag.
<p><a name="14101"></a>
<p><font size=+1><strong>20.17.2 </strong> <code>protected <code><b>SecurityManager</b></code>()<br>throws SecurityException</code></font>
<p>
<a name="29985"></a>
This constructor checks to see whether a security manager has already been
installed <a href="javalang.doc17.html#14133">(§20.18.5)</a>; if so, creation of another security manager is not permitted,
and so a <code>SecurityException</code> is thrown.
<p><a name="14102"></a>
<p><font size=+1><strong>20.17.3 </strong> <code>protected Class[] <code><b>getClassContext</b></code>()</code></font>
<p>
<a name="30083"></a>
This utility method for security managers scans the execution stack for the current
thread and returns an array with one component for each stack frame. The component
at position <code>0</code> corresponds to the top of the stack. If a component is a <code>Class</code>
object, then the corresponding stack frame is for an invocation of a method of the
class represented by that <code>Class</code> object.
<p><a name="30009"></a>
<p><font size=+1><strong>20.17.4 </strong> <code>protected int <code><b>classDepth</b></code>(String name)</code></font>
<p>
<a name="30063"></a>
This utility method for security managers searches the execution stack for the current
thread to find the most recently invoked method whose execution has not yet
completed and whose class has <code>name</code> as its fully qualified name. If such a method
is found, its distance from the top of the stack is returned as a nonnegative integer;
otherwise, <code>-1</code> is returned.
<p><a name="30027"></a>
<p><font size=+1><strong>20.17.5 </strong> <code>protected boolean <code><b>inClass</b></code>(String name)</code></font>
<p>
<a name="30075"></a>
This utility method for security managers searches the execution stack for the current
thread to find the most recently invoked method whose execution has not yet
completed and whose class has <code>name</code> as its fully qualified name. If such a method
is found, <code>true</code> is returned; otherwise, <code>false</code> is returned.
<p><a name="14103"></a>
<p><font size=+1><strong>20.17.6 </strong> <code>protected ClassLoader <code><b>currentClassLoader</b></code>()</code></font>
<p>
<a name="29991"></a>
This utility method for security managers searches the execution stack for the current
thread to find the most recently invoked method whose execution has not yet
completed and whose class was created by a class loader <a href="javalang.doc13.html#14462">(§20.14)</a>. If such a
method is found, a reference to the <code>ClassLoader</code> object for its class is returned;
otherwise, <code>null</code> is returned.
<p><a name="14105"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -