Write your own logon and access control modules

Source: Internet
Author: User

TIPS: I wrote my notes for the first time, and my hands were shaking a little bit. I got an axe from the class ~~~ Welcome!

Security is one of the features that Java advocates most. Indeed, Java's security features cover from the application level to the language level, and even the JVM itself. In the past, we all knew that there was a Sandbox, but only the Sandbox was not enough, or we could not easily meet all the security requirements we needed. For example, a system needs at least one login function, further, we also need to restrict the user's access to resources. I 'd like to give a general idea of how Java is doing these things, it is basically a summary or a "post-reading" nature. At the same time, it provides a simple implementation example. In fact, this example still imitates people's homes ......

1. Java access control mechanism

When it comes to access control or "Authorization", there are two meanings: first, from the perspective of resources, is this socket port allowed to operate? Is this file readable? Writable? Or executable? Or above? This is how we use "ls? L "command to list the"-rwx-"and other meanings of files in the current directory; second, from the visitor's point of view, I want to view the Sina European Cup news on the Web through port 80, is this qualification in this system? I want to play a video file named "friends. rm" on drive D. Have I obtained the permission to access this file? Do I have the permission to run the player?
Java considers both aspects of the access control policy at the same time. You said, "No, I use FileOutputStream to write files and use the Socket class to connect to the remote host, there is no limit. "First, let's talk about what is called"Security Manager(SecurityManger ). The security manager has been available since JDK 1.0. How old is it! Java has considered the security factor since the day of design. Security Manager is the most important part of Sandbox and the overall coordination of access control, we can normally use the network and files normally, because when the application is started (Note that it is application, not applet!), If you do not add"-Djava. security. manager"Option, the JVM will not start Sandbox, then you can" Do whatever you want ", instead of encountering exceptions such as SecurityException; once added"-Djava. security. manager "option, you will find a series of exceptions!

  1. ExceptionIn thread "main" java. security.AccessControlException: Access denied (......)
  2. ......


Java has a built-in default security policy. In this case, the security manager first loads the Default policy. Do not believe it. Do not believe it. Check your "% JAVA_HOME % jrelibsecurity" directory, is there a Java. policy"File? Use notepad to open it and see:

  1. // Standard extensions get all permissions by default
  2. Grant codeBase "file: $ {java. home}/lib/ext /*"{
  3. Permission java. security.AllPermission;
  4. };
  5. // Default permissions granted to all domains
  6. Grant {
  7. // Allows any thread to stop itself using the java. lang. Thread. stop ()
  8. // Method that takes no argument.
  9. // Note that this permission is granted by default only to remain
  10. // Backwards compatible.
  11. // It is stronugly recommended that you either remove this permission
  12. // From this policy file or further restrict it to code sources
  13. // That you specify, because Thread. stop () is potentially unsafe.
  14. // See "http://java.sun.com/notes" for more information.
  15. Permission java. lang.RuntimePermission"StopThread ";
  16. // Allows anyone to listen on un-privileged ports
  17. Permission java.net.SocketPermission"Localhost: 1024-", "listen ";
  18. // "Standard" properies that can be read by anyone
  19. Permission java. util.PropertyPermission"Java. version", "read ";
  20. Permission java. util.PropertyPermission"Java. vendor", "read ";
  21. Permission java. util.PropertyPermission"Java. vendor. url", "read ";
  22. Permission java. util.PropertyPermission"Java. class. version", "read ";
  23. Permission java. util.PropertyPermission"OS. name", "read ";
  24. Permission java. util.PropertyPermission"OS. version", "read ";
  25. Permission java. util.PropertyPermission"OS. arch" <

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.