Java's JVM Learning note four (security Manager)

Source: Internet
Author: User

Java's JVM Learning note four (security Manager)

The two components of the Java security Model (the class loader, type Checker) are described earlier, and the next step in the Java security model is to learn about the security Manager, which is another important component.

The security Manager is a separate object in the Java virtual machine, which has a central role in access control-access control for external resources

If the light-looking concept may not be well understood, or rather abstract, here is a constructor for ClassLoader, which is a simple look at what it does before initializing ClassLoader.

[Java]View Plaincopy
    1. Protected ClassLoader (ClassLoader parent) {
    2. SecurityManager security = System.getsecuritymanager ();
    3. if (Security! = null) {
    4. Security.checkcreateclassloader ();
    5. }
    6. this.parent = parent;
    7. initialized = true;
    8. }



The first word of this constructor (and, of course, the implicit invocation) is System.getsecuritymanager (); This line of code returns a security Manager object, The directory to which this object belongs is Java.lang.SecurityManager.

This constructor first determines if the security manager (in the previous class loader section, we mentioned that the class loader and security Manager can be customized by the user, here is the embodiment!! Since there is System.getsecuritymanager (), you should certainly guess that there is a system.setsecuritymanager ();), that is, the security manager is not empty, then perform the checksum, Jump to Checkcreateclassloader (); see what he's doing.

[Java]View Plaincopy
    1. Public void Checkcreateclassloader () {
    2. Heckpermission (securityconstants.create_classloader_permission);
    3. }


Here again called another method, from the method name, it can be guessed that this method is used to verify the permissions, check whether there is permission to create ClassLoader, and then jump to the Checkpermisson method

[Java]View Plaincopy
  1. public static void Checkpermission (Permission perm)
  2. throws Accesscontrolexception
  3. {
  4. //system.err.println ("checkpermission" +perm);
  5. //thread.currentthread (). DumpStack (); if (perm = = null) {
  6. throw New NullPointerException ("permission can ' t be null");
  7. } AccessControlContext stack = Getstackaccesscontrolcontext ();
  8. //If context is null and we had privileged system code on the stack.
  9. if (stack = = null) {
  10. Debug debug = Accesscontrolcontext.getdebug ();
  11. Boolean dumpdebug = false;
  12. if (debug! = null) {
  13. Dumpdebug =!  Debug.ison ("codebase=");
  14. Dumpdebug &=!  Debug.ison ("permission=") | |
  15. Debug.ison ("permission=" + perm.getclass (). Getcanonicalname ());
  16. } if (Dumpdebug && Debug.ison ("stack")) {
  17. Thread.CurrentThread (). DumpStack ();
  18. } if (Dumpdebug && debug.ison ("domain")) {
  19. Debug.println ("domain (context is null)");
  20. } if (dumpdebug) {
  21. Debug.println ("Access allowed" +perm);
  22. }
  23. return;
  24. } AccessControlContext acc = Stack.optimize ();
  25. Acc.checkpermission (perm);
  26. }
  27. }



The above method some code is difficult to understand, we do not have to read every line ( This method involves more things, it involves code signing authentication, policy and protection domain, these we will explain in detail in the latter section, do not understand the first skip), see its annotations/if context is NULL, we had privileged system code on the stack. This means that if the current Access controller context is empty, the systems codes on the stack will be privileged to find acc.checkpermission (perm); Jump in and find the code below .

[Java]View Plaincopy
  1. /*
  2. * Iterate through the protectiondomains in the context.
  3. * Stop at the first one, doesn ' t allow the
  4. * Requested permission (throwing an exception).
  5. *
  6. */ * If Ctxt is null, all we had on the stack were system domains,
  7. Or the first domain was a privileged system domain. This
  8. is to make the common case for system code very FAST * /if (context = = null)
  9. return; For (int i=0; i< context.length; i++) {
  10. if (context[i]! = null &&!context[i].implies (perm)) {
  11. if (dumpdebug) {
  12. Debug.println ("Access denied" + perm);
  13. } if (Debug.ison ("failure") && Debug! = null) {
  14. //Want to make sure this is the displayed for failure,
  15. want to display again if already displayed
  16. //above.
  17. if (!dumpdebug) {
  18. Debug.println ("Access denied" + perm);
  19. }
  20. Thread.CurrentThread (). DumpStack ();
  21. final Protectiondomain pd = Context[i];
  22. Final Debug db = Debug;
  23. Accesscontroller.doprivileged (new Privilegedaction () {
  24. Public Object Run () {
  25. Db.println ("domain that Failed" +pd);
  26. return null;
  27. }
  28. });
  29. }
  30. throw New Accesscontrolexception ("Access Denied" +perm, Perm);
  31. }
  32. }



Nothing to look at, just look at the top of that paragraph of the note, meaning traversal context in the protection domain, once found that the requested permission is not allowed, stop, throw an exception, here we have a relatively clear concept, the security Manager is used to control the execution of permissions, and the above code has a very important class Accesscontroller, Access controller, there is also a very important term protection domain ( protection domain We also have a simple in the previous section, is not a bit of impression), these may now listen a little vague, do not worry, temporarily do not tube, They will be explained slowly in a later chapter.


Well, after you understand what the security manager is doing, next, do a next experiment, first to verify that the default security management is not installed, and then try to install it. In my environment I did not install the default security manager, and did not write my own security manager based on the default security manager, if need to open, can be displayed in the program to install the Security manager, the same can let it automatically install the default security manager ( Add a-djava.security.manager to the JVM.

Below we use familiar ecplise to write a simple demo to see before and after the installation of the difference, in the next section, will be detailed to learn code signing authentication and policy, and write a security manager of their own.

[Java]View Plaincopy
    1. <span style="FONT-SIZE:14PX;" > public static void Main (string[] args) {
    2. System.out.println (System.getsecuritymanager ());
    3. }</span>

Run this main function and output what? Yes output NULL, this time we did not install the default security Manager

Re-run, in ecplise right-click--run as--run configuration--arguments, in the VM Arguments column input

-djava.security.manager. When you click Run, what do you see at this time?

Output: The name of the SecurityManager object. This is when the default security manager is installed.

Summarize:

In a Java virtual machine, it plays a central role in access control-access control for external resources

Java's JVM Learning note four (security Manager)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.