Permission相关详解

1,Permission类介绍

java.security.Permission

This abstract class is the ancestor of all permissions.

Permission类的子类:
  • AllPermission
  • BasicPermission
  • FilePermission
  • PrivateCredentialPermission
  • ServicePermission
  • SocketPermission
  • UnresolvedPermission

BasicPermission类的子类
  • AudioPermission
  • AuthPermission
  • AWTPermission
  • DelegationPermission
  • LoggingPermission
  • NetPermission
  • PropertyPermission
  • ReflectPermission
  • RuntimePermission
  • SecurityPermission
  • SerializablePermission
  • SQLPermission
  • SSLPermission


Permission类的主要方法:
//Constructs a permission with the specified name.
public Permission(String name)
//Implements the guard interface for a permission. 
void checkGuard(Object object) 
//Returns the actions as a String. 
abstract  String getActions() 
String getName() //Returns the name of this Permission.
//Checks if the specified permission's actions are "implied by" this object's actions.  
abstract  boolean implies(Permission permission) 


2,Permission类相关类介绍

java.security.PermissionCollection
This class holds a homogeneous collection of permissions. In other words, each instance of the class holds only permissions of the same type.

java.security.Permissions
This class is designed to hold a heterogeneous collection of permissions. Basically, it is a collection of java.security.PermissionCollection objects.

3,Permission类的子类用法说明

java.io.FilePermission

The targets for this class can be specified in the following ways, where directory and file names are strings that cannot contain white spaces.

  • file
  • directory (same as directory/)
  • directory/file
  • directory/* (all files in this directory)
  • * (all files in the current directory)
  • directory/- (all files in the file system under this directory)
  • - (all files in the file system under the current directory)
  • "<<ALL FILES>>" (all files in the file system)

Note that "<<ALL FILES>>" is a special string denoting all files in the system. On a Unix system, this includes all files under the root directory. On an MS-DOS system, this includes all files on all drives.
The actions are: read, write, delete, and execute. Therefore, the following are valid code samples for creating file permissions:

import java.io.FilePermission;

FilePermission p = new FilePermission("myfile", "read,write");
FilePermission p = new FilePermission("/home/gong/", "read");
FilePermission p = new FilePermission("/tmp/mytmp", "read,delete");
FilePermission p = new FilePermission("/bin/*", "execute");
FilePermission p = new FilePermission("*", "read");
FilePermission p = new FilePermission("/-", "read,execute");
FilePermission p = new FilePermission("-", "read,execute");
FilePermission p = new FilePermission("<<ALL FILES>>", "read");


The implies method in this class correctly interprets the file system. For example, FilePermission("/-", "read,execute") implies FilePermission("/home/gong/public_html/index.html", "read"), and FilePermission("bin/*", "execute") implies FilePermission("bin/emacs19.31", "execute").
Note: Most of these strings are given in platform-dependent format. For example, to represent read access to the file named "foo" in the "temp" directory on the C drive of a Windows system, you would use

FilePermission p = new FilePermission("c:\\temp\\foo", "read");


The double backslashes are necessary to represent a single backslash because the strings are processed by a tokenizer (java.io.StreamTokenizer), which allows "\" to be used as an escape string (e.g., "\n" to indicate a new line) and which thus requires two backslashes to indicate a single backslash. After the tokenizer has processed the above FilePermission target string, converting double backslashes to single backslashes, the end result is the actual path

"c:\temp\foo"

It is necessary that the strings be given in platform-dependent format until there is a universal file description language. Note also that the use of meta symbols such as "*" and "-" prevents the use of specific file names. We think this is a small limitation that can be tolerated for the moment. Finally, note that "/-" and "<<ALL FILES>>" are the same target on Unix systems in that they both refer to the entire file system. (They can refer to multiple file systems if they are all available). The two targets are potentially different on other operating systems, such as MS Windows and MacOS.
Also note that a target name that specifies just a directory, with a "read" action, as in

FilePermission p = new FilePermission("/home/gong/", "read");


means you are only giving permission to list the files in that directory, not read any of them. To allow read access to files, you must specify either an explicit file name, or an "*" or "-", as in

FilePermission p = new FilePermission("/home/gong/myfile", "read");
FilePermission p = new FilePermission("/home/gong/*", "read");
FilePermission p = new FilePermission("/home/gong/-", "read");


And finally, note that code always automatically has permission to read files from its same (URL) location, and subdirectories of that location; it does not need explicit permission to do so.

java.net.SocketPermission

This class represents access to a network via sockets. The target for this class can be given as "hostname:port_range", where hostname can be given in the following ways:

  • hostname (a single host)
  • IP address (a single host)
  • localhost (the local machine)
  • "" (equivalent to "localhost")
  • hostname.domain (a single host within the domain)
  • hostname.subdomain.domain
  • *.domain (all hosts in the domain)
  • *.subdomain.domain
  • * (all hosts)


That is, the host is expressed as a DNS name, as a numerical IP address, as "localhost" (for the local machine) or as "" (which is equivalent to specifying "localhost").
The wildcard "*" may be included once in a DNS name host specification. If it is included, it must be in the leftmost position, as in "*.sun.com".

The port_range can be given as follows:

  • N (a single port)
  • N- (all ports numbered N and above)
  • -N (all ports numbered N and below)
  • N1-N2 (all ports between N1 and N2, inclusive)


Here N, N1, and N2 are non-negative integers ranging from 0 to 65535 (2^16-1).
The actions on sockets are accept, connect, listen, and resolve (which is basically DNS lookup). Note that implicitly, the action "resolve" is implied by "accept", "connect", and "listen" -- i.e., those who can listen or accept incoming connections from or initiate out-going connections to a host should be able to look up the name of the remote host.

Below are some examples of socket permissions.

import java.net.SocketPermission;

SocketPermission p = new SocketPermission("java.sun.com","accept");
p = new SocketPermission("204.160.241.99","accept");
p = new SocketPermission("*.com","connect");
p = new SocketPermission("*.sun.com:80","accept");
p = new SocketPermission("*.sun.com:-1023","accept");
p = new SocketPermission("*.sun.com:1024-","connect");
p = new SocketPermission("java.sun.com:8000-9000",
         "connect,accept");
p = new SocketPermission("localhost:1024-",
          "accept,connect,listen");


Note that SocketPermission("java.sun.com:80,8080","accept") and SocketPermission("java.sun.com,javasun.sun.com","accept") are not valid socket permissions.
Moreover, because listen is an action that applies only to ports on the local host, whereas accept is an action that applies to ports on both the local and remote host, both actions are necessary.

java.security.BasicPermission

The BasicPermission class extends the Permission class. It can be used as the base class for permissions that want to follow the same naming convention as BasicPermission (see below).
The name for a BasicPermission is the name of the given permission (for example, "exitVM", "setFactory", "queuePrintJob", etc). The naming convention follows the hierarchical property naming convention. An asterisk may appear at the end of the name, following a ".", or by itself, to signify a wildcard match. For example: "java.*" or "*" is valid, "*java" or "a*b" is not valid.

The action string (inherited from Permission) is unused.Thus, BasicPermission is commonly used as the base class for "named" permissions (ones that contain a name but no actions list; you either have the named permission or you don't.) Subclasses may implement actions on top of BasicPermission, if desired.

Some of the BasicPermission subclasses are java.lang.RuntimePermission, java.security.SecurityPermission, java.util.PropertyPermission, and java.net.NetPermission.

java.util.PropertyPermission

The targets for this class are basically the names of Java properties as set in various property files. Examples are the "java.home" and "os.name" properties. Targets can be specified as "*" (any property), "a.*" (any property whose name has a prefix "a."), "a.b.*", and so on. Note that the wildcard can occur only once and can only be at the rightmost position.
This is one of the BasicPermission subclasses that implements actions on top of BasicPermission. The actions are read and write. Their meaning is defined as follows: "read" permission allows the getProperty method in java.lang.System to be called to get the property value, and "write" permission allows the setProperty method to be called to set the property value.

java.lang.RuntimePermission

The target for a RuntimePermission can be represented by any string, and there is no action associated with the targets. For example, RuntimePermission("exitVM") denotes the permission to exit the Java Virtual Machine.
The target names are:

  • createClassLoader
  • getClassLoader
  • setContextClassLoader
  • setSecurityManager
  • createSecurityManager
  • exitVM
  • setFactory
  • setIO
  • modifyThread
  • stopThread
  • modifyThreadGroup
  • getProtectionDomain
  • readFileDescriptor
  • writeFileDescriptor
  • loadLibrary.{library name}
  • accessClassInPackage.{package name}
  • defineClassInPackage.{package name}
  • accessDeclaredMembers.{class name}
  • queuePrintJob


java.awt.AWTPermission

This is in the same spirit as the RuntimePermission; it's a permission without actions. The targets for this class are:

  • accessClipboard
  • accessEventQueue
  • listenToAllAWTEvents
  • showWindowWithoutWarningBanner


java.net.NetPermission

This class contains the following targets and no actions:

  • requestPasswordAuthentication
  • setDefaultAuthenticator
  • specifyStreamHandler


java.lang.reflect.ReflectPermission

This is the Permission class for reflective operations. A ReflectPermission is a named permission (like RuntimePermission) and has no actions. The only name currently defined is

  • suppressAccessChecks


which allows suppressing the standard Java programming language access checks -- for public, default (package) access, protected, and private members -- performed by reflected objects at their point of use.

java.io.SerializablePermission

This class contains the following targets and no actions:

  • enableSubclassImplementation
  • enableSubstitution


java.security.SecurityPermission

SecurityPermissions control access to security-related objects, such as Security, Policy, Provider, Signer, and Identity objects. This class contains the following targets and no actions:

  • getPolicy
  • setPolicy
  • getProperty.{key}
  • setProperty.{key}
  • insertProvider.{provider name}
  • removeProvider.{provider name}
  • setSystemScope
  • setIdentityPublicKey
  • setIdentityInfo
  • printIdentity
  • addIdentityCertificate
  • removeIdentityCertificate
  • clearProviderProperties.{provider name}
  • putProviderProperty.{provider name}
  • removeProviderProperty.{provider name}
  • getSignerPrivateKey
  • setSignerKeyPair


java.security.AllPermission

This permission implies all permissions. It is introduced to simplify the work of system administrators who might need to perform multiple tasks that require all (or numerous) permissions. It would be inconvenient to require the security policy to iterate through all permissions. Note that AllPermission also implies new permissions that are defined in the future.
Clearly much caution is necessary when considering granting this permission.

javax.security.auth.AuthPermsision

AuthPermission handles authentication permissions and authentication-related object such as Subject, SubjectDomainCombiner, LoginContext, and Configuration. This class contains the following targets and no actions:

  • doAs
  • doAsPrivileged
  • getSubject
  • getSubjectFromDomainCombiner
  • setReadOnly
  • modifyPrincipals
  • modifyPublicCredentials
  • modifyPrivateCredentials
  • refreshCredential
  • destroyCredential
  • createLoginContext.{name}
  • getLoginConfiguration
  • setLoginConfiguration
  • refreshLoginConfiguration

你可能感兴趣的:(java,socket,Security,Access,sun)