Welcome loading Please indicate Source: http://blog.csdn.net/yfqnihao/article/details/8271407
Course Source: http://download.csdn.net/detail/yfqnihao/4866503
What is a Java policy and what is a policy file.
Today, I change the way of notes, not directly to the concept, but first to do a small example, I believe you finish this example and then read my explanation of the example, you on the strategy, strategy files, will be enlightened.
The example is simple, simple is everybody's , following me below (you can copy my code completely).
The first step is to define a simple class.
Packagecom.yfq.test;ImportJava.io.FileWriter;Importjava.io.IOException; Public classTestpolicy { Public Static voidMain (string[] args) {FileWriter writer; Try{writer=NewFileWriter ("D:/testpolicy.txt"); Writer.write ("Hello1"); Writer.close (); } Catch(IOException e) {e.printstacktrace (); } }}
The second step, define a simple policy file, we put in the project Classpath (src folder), named MyPolicy.txt
Grant CodeBase "file:d:/workspace/testpolicy/bin/*" { "d:/testpolicy.txt", "read";
I'm simply speaking about the role of this file.
The first line: Grant CodeBase "file:d:/workspace/testpolicy/bin/*" means to give d:/workspace/testpolicy/bin/* permission to all the files under this path, the asterisk is the wildcard character, All the meaning
The second line: Permission Java.io.FilePermission "D:/testpolicy.txt", "read" means d:/testpolicy.txt This file only assigns Read permissions.
The third step, run, in the cmd window input (does not run up, indicating that the JDK environment variable is not configured, go to match)
Java-classpath D:/workspace/testpolicy/bin-djava.security.manager-djava.security.policy=d:/workspace/testpolicy /src/mypolicy.txt Com.yfq.test.TestPolicy
Specify the current classpath as D:/workspace/testpolicy/bin, start the default security manager (as you should guess here, the policy must work with the security Manager), set the location of the security policy file ( There are several ways to install a policy file, and here we are under Windows, if you are interested enough to explore it yourself.
Fourth step, view the output
The exception is reported here, indicating that the application does not have permission to write to the D:/testpolicy.txt file.
Modify the above MyPolicy.txt file as follows
Grant CodeBase "file:d:/workspace/testpolicy/bin/*" { "d:/testpolicy.txt", "Read,write";
Run again, no error.
Well the experiment success, perhaps you will doubt, this has the bird to use Ah, do not hurry, in the next section, we will say in detail, now I do a brief introduction, this policy file (MyPolicy.txt in this article) corresponds to a class in Java, called Java.security.Policy ( Strategy), this is a magical class, with it, you can define your own code permissions, of course it can also be combined with the security manager we mentioned in note four. And now you just have to remember one thing:
Java's access control policy for an application is represented by a singleton of a subclass of the abstract class Java.security.Policy, and at any time, each application actually has only one policy object, which corresponds to the policy file . The class loader uses this policy object to help them decide what permissions should be granted when importing a piece of code into a virtual machine.
If you have listened to the concept of strategy a little bit before, I hope to read this article will give you a sense of the enlightened
Disclaimer: The above experiments have reference to the content of other authors on the web, in this only to do learning and exchange!
Java JVM Learning Note nine (policy file)