📄 accesscontroller.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:35:54 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class AccessController</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/AccessController.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../java/security/AccessControlContext.html"><B>PREV CLASS</B></A> <A HREF="../../java/security/AlgorithmParameterGenerator.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A> <A HREF="AccessController.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | FIELD | CONSTR | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: FIELD | CONSTR | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.security</FONT><BR>Class AccessController</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A> | +--<B>java.security.AccessController</B></PRE><HR><DL><DT>public final class <B>AccessController</B><DT>extends <A HREF="../../java/lang/Object.html">Object</A></DL><P><p> The AccessController class is used for three purposes: <ul> <li> to decide whether an access to a critical system resource is to be allowed or denied, based on the security policy currently in effect,<p> <li>to mark code as being "privileged", thus affecting subsequent access determinations, and<p> <li>to obtain a "snapshot" of the current calling context so access-control decisions from a different context can be made with respect to the saved context. </ul> <p> The <A HREF="../../java/security/AccessController.html#checkPermission(java.security.Permission)"><CODE>checkPermission</CODE></A> method determines whether the access request indicated by a specified permission should be granted or denied. A sample call appears below. In this example, <code>checkPermission</code> will determine whether or not to grant "read" access to the file named "testFile" in the "/temp" directory. <pre> FilePermission perm = new FilePermission("/temp/testFile", "read"); AccessController.checkPermission(perm); </pre> <p> If a requested access is allowed, <code>checkPermission</code> returns quietly. If denied, an AccessControlException is thrown. AccessControlException can also be thrown if the requested permission is of an incorrect type or contains an invalid value. Such information is given whenever possible. Suppose the current thread traversed m callers, in the order of caller 1 to caller 2 to caller m. Then caller m invoked the <code>checkPermission</code> method. The <code>checkPermission </code>method determines whether access is granted or denied based on the following algorithm: <pre> i = m; while (i > 0) { if (caller i's domain does not have the permission) throw AccessControlException else if (caller i is marked as privileged) { if (a context was specified in the call to doPrivileged) context.checkPermission(permission) return; } i = i - 1; }; // Next, check the context inherited when // the thread was created. Whenever a new thread is created, the // AccessControlContext at that time is // stored and associated with the new thread, as the "inherited" // context. inheritedContext.checkPermission(permission); </pre> <p> A caller can be marked as being "privileged" (see <A HREF="../../java/security/AccessController.html#doPrivileged(java.security.PrivilegedAction)"><CODE>doPrivileged</CODE></A> and below). When making access control decisions, the <code>checkPermission</code> method stops checking if it reaches a caller that was marked as "privileged" via a <code>doPrivileged</code> call without a context argument (see below for information about a context argument). If that caller's domain has the specified permission, no further checking is done and <code>checkPermission</code> returns quietly, indicating that the requested access is allowed. If that domain does not have the specified permission, an exception is thrown, as usual. <p> The normal use of the "privileged" feature is as follows. If you don't need to return a value from within the "privileged" block, do the following: <pre> somemethod() { ...normal code here... AccessController.doPrivileged(new PrivilegedAction() { public Object run() { // privileged code goes here, for example: System.loadLibrary("awt"); return null; // nothing to return } }); ...normal code here... } </pre> <p> PrivilegedAction is an interface with a single method, named <code>run</code>, that returns an Object. The above example shows creation of an implementation of that interface; a concrete implementation of the <code>run</code> method is supplied. When the call to <code>doPrivileged</code> is made, an instance of the PrivilegedAction implementation is passed to it. The <code>doPrivileged</code> method calls the <code>run</code> method from the PrivilegedAction implementation after enabling privileges, and returns the <code>run</code> method's return value as the <code>doPrivileged</code> return value (which is ignored in this example). <p> If you need to return a value, you can do something like the following: <pre> somemethod() { ...normal code here... String user = (String) AccessController.doPrivileged( new PrivilegedAction() { public Object run() { return System.getProperty("user.name"); } } ); ...normal code here... } </pre> <p>If the action performed in your <code>run</code> method could throw a "checked" exception (those listed in the <code>throws</code> clause of a method), then you need to use the <code>PrivilegedExceptionAction</code> interface instead of the <code>PrivilegedAction</code> interface: <pre> somemethod() throws FileNotFoundException { ...normal code here... try { FileInputStream fis = (FileInputStream) AccessController.doPrivileged( new PrivilegedExceptionAction() { public Object run() throws FileNotFoundException { return new FileInputStream("someFile"); } } ); } catch (PrivilegedActionException e) { // e.getException() should be an instance of FileNotFoundException, // as only "checked" exceptions will be "wrapped" in a // <code>PrivilegedActionException</code>. throw (FileNotFoundException) e.getException(); } ...normal code here... } </pre> <p> Be *very* careful in your use of the "privileged" construct, and always remember to make the privileged code section as small as possible. <p> Note that <code>checkPermission</code> always performs security checks within the context of the currently executing thread. Sometimes a security check that should be made within a given context will actually need to be done from within a <i>different</i> context (for example, from within a worker thread). The <A HREF="../../java/security/AccessController.html#getContext()"><CODE>getContext</CODE></A> method and AccessControlContext class are provided for this situation. The <code>getContext</code> method takes a "snapshot" of the current calling context, and places it in an AccessControlContext object, which it returns. A sample call is the following: <pre> AccessControlContext acc = AccessController.getContext() </pre> <p> AccessControlContext itself has a <code>checkPermission</code> method that makes access decisions based on the context <i>it</i> encapsulates, rather than that of the current execution thread. Code within a different context can thus call that method on the previously-saved AccessControlContext object. A sample call is the following: <pre> acc.checkPermission(permission) </pre> <p> There are also times where you don't know a priori which permissions to check the context against. In these cases you can use the doPrivileged method that takes a context: <pre> somemethod() { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { // Code goes here. Any permission checks from this // point forward require both the current context and // the snapshot's context to have the desired permission. } }, acc); ...normal code here... } </pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -