Every Java application can have its own security manager, which is the main security guard against malicious attacks. The security manager performs runtime checks and access authorization to implement the security policies required by the application to protect resources from malicious operations. In fact, the security manager decides which group of permissions to grant to the class based on the Java security policy file. However, when untrusted classes and third-party applications use JVM, Java security manager uses JVM-related security policies to identify malicious operations. In many cases, the threat model does not contain malicious code running in JVM, so the Java security manager is not necessary. When the security manager detects a violation of the security policy, the JVM will cause AccessControlException or SecurityException.
In Java applications, the security manager is set by the setSecurityManager method in the System class. To obtain the current security manager, you can use getSecurityManager.
The java. lang. SecurityManager class contains many checkXXXX methods, such as the checkRead (String file) method used to determine the file access permission. These check methods call the SecurityManager. checkPermission method, which determines whether the called application has the operation permission to execute the request based on the security policy file. If no, SecurityException is thrown.
To enable the application to use the security manager and security policy, you can set the-Djava. security. manager option when starting JVM, and specify the security policy file at the same time. If Java security manager is enabled in the application, but no security policy file is specified, Java security manager uses the default security policy, they are written by java in the $ JAVA_HOME/jre/lib/security Directory. policy.
1. Specify-Djava. security. manager
When we run a program, we can specify the JVM command-Djava. security. manager to run SecurityManager.
Java-Djava. security. manager <class_name>
This is the most common way to open SecurityManager. Java. security. manager is a System attribute. You can use System. getProperty ("java. security. manager") to check whether the System attribute is set.
Here, you may think that we can use System. setProperty ("java. security. manager") to open SecurityManager, but this cannot be set. As we mentioned earlier, this system attribute is checked when JVM is started. If we use a program to manually set this attribute, it does not work because the JVM has been started and the system attribute check procedure has passed.
2. Open SecurityManager through a program
Now, if we really want to open Se through our program
CurityManager, which we can also do. The System class has a method called setSecurityManager () to do this. The parameter of this method is a SecurityManager instance.
SecurityManager sm = new SecurityManager ();
System. setSecurityManager (sm );
With this, we can open SecurityManager .. What should we do if we want to disable SecurityManager later? Can the following code be implemented?
SecurityManager sm = System. getSecurityManager ();
If (sm! = Null ){
System. setSecurityManager (null );
}
The above code works only when a permission is specified in the $ {JAVA_HOME}/lib/security directory or in the java. policy file of another specified directory. This permission is:
Permission java. lang. RuntimePermission "setSecurityManager ";
The above line is used to allow the code to set SecurityManager.
3. In the build file
We want to open SecurityManager. If we use Ant to build an application, we can add:
<Sysproperty key = "java. security. manager" value = ""/>
We use Ant to Create unit tests.
Create a subclass of SecurityManager;
(2) override some methods.
Import java. io .*;
Public class TestSecurity
{
Public static void main (String args [])
{
Try {
System. setSecurityManager (new PasswordSecurityManager ("123456 "));
} Catch (SecurityException se ){
System. out. println ("SecurityManager already set! ");
}
Try {
// DataInputStream FD = new DataInputStream (new FileInputStream ("input.txt "));
BufferedReader FCM = new BufferedReader (new FileReader ("input.txt "));
// DataOutputStream fos = new DataOutputStream (new FileOutputStream ("output.txt "));
BufferedWriter fos = new BufferedWriter (new FileWriter ("output.txt "));
String inputString;
While (inputString = FI. readLine ())! = Null ){
// Fos. writeBytes (inputString );
// Fos. writeByte ('\ n ');
Fos. write (inputString );
Fos. write ('\ n ');
}
FCM. close ();
Fos. close ();
} Catch (IOException ioe ){
System. out. println ("I/O failed for SecurityManagerTest .");
} Catch (Exception e)
{
System. out. println (e. toString ());
}
}
}
Import java. io .*;
Class PasswordSecurityManager extends SecurityManager {
Private String password;
PasswordSecurityManager (String password ){
Super ();
This. password = password;
}
Private boolean accessOK (){
Int c;
// DataInputStream dis = new DataInputStream (System. in );
BufferedReader dis = new BufferedReader (new InputStreamReader (System. in ));
String response;
System. out. println ("What's the secret password? ");
Try {
Response = dis. readLine ();
If (response. equals (password ))
Return true;
Else
Return false;
} Catch (IOException e ){
Return false;
}
}
Public void checkRead (FileDescriptor filedescriptor ){
If (! AccessOK ())
Throw new SecurityException ("Not a Chance! ");
}
Public void checkRead (String filename ){
If (! AccessOK ())
Throw new SecurityException ("No Way! ");
}
Public void checkRead (String filename, Object executionContext ){
If (! AccessOK ())
Throw new SecurityException ("Forget It! ");
}
Public void checkWrite (FileDescriptor filedescriptor ){
If (! AccessOK ())
Throw new SecurityException ("Not! ");
}
Public void checkWrite (String filename ){
If (! AccessOK ())
Throw new SecurityException ("Not Even! ");
}
}