📄 securitymanager.java
字号:
/* SecurityManager.java -- security checks for privileged actions Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.lang;import java.awt.AWTPermission;import java.io.File;import java.io.FileDescriptor;import java.io.FilePermission;import java.lang.reflect.Member;import java.net.InetAddress;import java.net.SocketPermission;import java.security.AllPermission;import java.security.Permission;import java.security.Security;import java.security.SecurityPermission;import java.util.PropertyPermission;/** * SecurityManager is a class you can extend to create your own Java * security policy. By default, there is no SecurityManager installed in * 1.1, which means that all things are permitted to all people. The security * manager, if set, is consulted before doing anything with potentially * dangerous results, and throws a <code>SecurityException</code> if the * action is forbidden. * * <p>A typical check is as follows, just before the dangerous operation:<br> * <pre> * SecurityManager sm = System.getSecurityManager(); * if (sm != null) * sm.checkABC(<em>argument</em>, ...); * </pre> * Note that this is thread-safe, by caching the security manager in a local * variable rather than risking a NullPointerException if the mangager is * changed between the check for null and before the permission check. * * <p>The special method <code>checkPermission</code> is a catchall, and * the default implementation calls * <code>AccessController.checkPermission</code>. In fact, all the other * methods default to calling checkPermission. * * <p>Sometimes, the security check needs to happen from a different context, * such as when called from a worker thread. In such cases, use * <code>getSecurityContext</code> to take a snapshot that can be passed * to the worker thread:<br> * <pre> * Object context = null; * SecurityManager sm = System.getSecurityManager(); * if (sm != null) * context = sm.getSecurityContext(); // defaults to an AccessControlContext * // now, in worker thread * if (sm != null) * sm.checkPermission(permission, context); * </pre> * * <p>Permissions fall into these categories: File, Socket, Net, Security, * Runtime, Property, AWT, Reflect, and Serializable. Each of these * permissions have a property naming convention, that follows a hierarchical * naming convention, to make it easy to grant or deny several permissions * at once. Some permissions also take a list of permitted actions, such * as "read" or "write", to fine-tune control even more. The permission * <code>java.security.AllPermission</code> grants all permissions. * * <p>The default methods in this class deny all things to all people. You * must explicitly grant permission for anything you want to be legal when * subclassing this class. * * @author John Keiser * @author Eric Blake (ebb9@email.byu.edu) * @see ClassLoader * @see SecurityException * @see #checkTopLevelWindow(Object) * @see System#getSecurityManager() * @see System#setSecurityManager(SecurityManager) * @see AccessController * @see AccessControlContext * @see AccessControlException * @see Permission * @see BasicPermission * @see java.io.FilePermission * @see java.net.SocketPermission * @see java.util.PropertyPermission * @see RuntimePermission * @see java.awt.AWTPermission * @see Policy * @see SecurityPermission * @see ProtectionDomain * @since 1.0 * @status still missing 1.4 functionality */public class SecurityManager{ /** * The current security manager. This is located here instead of in * System, to avoid security problems, as well as bootstrap issues. * Make sure to access it in a thread-safe manner; it is package visible * to avoid overhead in java.lang. */ static volatile SecurityManager current; /** * Tells whether or not the SecurityManager is currently performing a * security check. * @deprecated Use {@link #checkPermission(Permission)} instead. */ protected boolean inCheck; /** * Construct a new security manager. There may be a security check, of * <code>RuntimePermission("createSecurityManager")</code>. * * @throws SecurityException if permission is denied */ public SecurityManager() { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkPermission(new RuntimePermission("createSecurityManager")); } /** * Tells whether or not the SecurityManager is currently performing a * security check. * * @return true if the SecurityManager is in a security check * @see #inCheck * @deprecated use {@link #checkPermission(Permission)} instead */ public boolean getInCheck() { return inCheck; } /** * Get a list of all the classes currently executing methods on the Java * stack. getClassContext()[0] is the currently executing method (ie. the * class that CALLED getClassContext, not SecurityManager). * * @return an array of classes on the Java execution stack */ protected Class[] getClassContext() { return VMSecurityManager.getClassContext(SecurityManager.class); } /** * Find the ClassLoader of the first non-system class on the execution * stack. A non-system class is one whose ClassLoader is not equal to * {@link ClassLoader#getSystemClassLoader()} or its ancestors. This * will return null in three cases: * * <ul> * <li>All methods on the stack are from system classes</li> * <li>All methods on the stack up to the first "privileged" caller, as * created by {@link AccessController.doPrivileged(PrivilegedAction)}, * are from system classes</li> * <li>A check of <code>java.security.AllPermission</code> succeeds.</li> * </ul> * * @return the most recent non-system ClassLoader on the execution stack * @deprecated use {@link #checkPermission(Permission)} instead */ protected ClassLoader currentClassLoader() { return VMSecurityManager.currentClassLoader(SecurityManager.class); } /** * Find the first non-system class on the execution stack. A non-system * class is one whose ClassLoader is not equal to * {@link ClassLoader#getSystemClassLoader()} or its ancestors. This * will return null in three cases: * * <ul> * <li>All methods on the stack are from system classes</li> * <li>All methods on the stack up to the first "privileged" caller, as * created by {@link AccessController.doPrivileged(PrivilegedAction)}, * are from system classes</li> * <li>A check of <code>java.security.AllPermission</code> succeeds.</li> * </ul> * * @return the most recent non-system Class on the execution stack * @deprecated use {@link #checkPermission(Permission)} instead */ protected Class currentLoadedClass() { int i = classLoaderDepth(); return i >= 0 ? getClassContext()[i] : null; } /** * Get the depth of a particular class on the execution stack. * * @param className the fully-qualified name to search for * @return the index of the class on the stack, or -1 * @deprecated use {@link #checkPermission(Permission)} instead */ protected int classDepth(String className) { Class[] c = getClassContext(); for (int i = 0; i < c.length; i++) if (className.equals(c[i].getName())) return i; return -1; } /** * Get the depth on the execution stack of the most recent non-system class. * A non-system class is one whose ClassLoader is not equal to * {@link ClassLoader#getSystemClassLoader()} or its ancestors. This * will return -1 in three cases: * * <ul> * <li>All methods on the stack are from system classes</li> * <li>All methods on the stack up to the first "privileged" caller, as * created by {@link AccessController.doPrivileged(PrivilegedAction)}, * are from system classes</li> * <li>A check of <code>java.security.AllPermission</code> succeeds.</li> * </ul> * * @return the index of the most recent non-system Class on the stack * @deprecated use {@link #checkPermission(Permission)} instead */ protected int classLoaderDepth() { try { checkPermission(new AllPermission()); } catch (SecurityException e) { Class[] c = getClassContext(); for (int i = 0; i < c.length; i++) if (c[i].getClassLoader() != null) // XXX Check if c[i] is AccessController, or a system class. return i; } return -1; } /** * Tell whether the specified class is on the execution stack. * * @param className the fully-qualified name of the class to find * @return whether the specified class is on the execution stack * @deprecated use {@link #checkPermission(Permission)} instead */ protected boolean inClass(String className) { return classDepth(className) != -1; } /** * Tell whether there is a class loaded with an explicit ClassLoader on * the stack. * * @return whether a class with an explicit ClassLoader is on the stack * @deprecated use {@link #checkPermission(Permission)} instead */ protected boolean inClassLoader() { return classLoaderDepth() != -1; } /** * Get an implementation-dependent Object that contains enough information * about the current environment to be able to perform standard security * checks later. This is used by trusted methods that need to verify that * their callers have sufficient access to perform certain operations. * * <p>Currently the only methods that use this are checkRead() and * checkConnect(). The default implementation returns an * <code>AccessControlContext</code>. * * @return a security context * @see #checkConnect(String, int, Object) * @see #checkRead(String, Object) * @see AccessControlContext * @see AccessController#getContext() */ public Object getSecurityContext() { // XXX Should be: return AccessController.getContext(); return new SecurityContext(getClassContext()); } /** * Check if the current thread is allowed to perform an operation that * requires the specified <code>Permission</code>. This defaults to * <code>AccessController.checkPermission</code>. * * @param perm the <code>Permission</code> required * @throws SecurityException if permission is denied * @throws NullPointerException if perm is null * @since 1.2 */ public void checkPermission(Permission perm) { // XXX Should be: AccessController.checkPermission(perm); //.throw new SecurityException("Operation not allowed"); } /** * Check if the current thread is allowed to perform an operation that * requires the specified <code>Permission</code>. This is done in a * context previously returned by <code>getSecurityContext()</code>. The * default implementation expects context to be an AccessControlContext, * and it calls <code>AccessControlContext.checkPermission(perm)</code>. * * @param perm the <code>Permission</code> required * @param context a security context * @throws SecurityException if permission is denied, or if context is * not an AccessControlContext * @throws NullPointerException if perm is null * @see #getSecurityContext() * @see AccessControlContext#checkPermission(Permission) * @since 1.2 */ public void checkPermission(Permission perm, Object context) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -