Giriş
Şu satırı dahil ederiz.
doPrivileged metodu - PrivilegedExceptionAction
PrivilegedActionException fırlatıyor. Açıklaması şöyle.
Şu satırı dahil ederiz.
import java.security.AccessController;
doPrivileged Neden Lazım?
Yani Security Manager yoksa doPrivileged() yapmaya gerek yok.
Without an installed Security Manager you don't need a privileged block. However, if you are writing fully general library code, which may be executed with a security manager in place, and the caller of the library may not have the needed permissions, then without a PrivilegedAction your code will be denied access as well, ...
Açıklaması şöyle
There are lot of operations in Java that require the caller domain to have certain permissions for successful execution of those operations. System.getProperty is one of those operations. All file related operations also need special permissions. When you use AccessController.doPrivileged to invoke those operations, the operation is executed with all the rights(permissions) of your protection domain.
doPrivileged metodu - PrivilegedAction
Exception fırlatmıyor
Örnek
Şöyle yaparız
class PasswordManager {
public static void changePassword() {
FileInputStream fin = openPasswordFile();
if (fin == null) {
// No password file; handle error
}
}
private static FileInputStream openPasswordFile() {
final String password_file = "password";
final FileInputStream fin[] = { null };
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
try {
// Sensitive action; can't be done outside
// doPrivileged() block
fin[0] = new FileInputStream(password_file);
} catch (FileNotFoundException x) {
// Report to handler
}
return null;
}
});
return fin[0];
}
}
PrivilegedActionException fırlatıyor. Açıklaması şöyle.
Örnek - File
Belli bir dizine erişmek için şöyle yaparız.
Reader reader;
try {
reader = AccessController.doPrivileged(new PrivilegedExceptionAction<Reader>() {
public Reader run() throws IOException {
return new FileReader(file);
}
});
} catch (PrivilegedActionException e) {
throw (IOException) e.getCause();
}
Örnek - FileŞöyle yaparız
String jarPath = ...;
URL jarUrl = new File(jarPath).toURI().toURL();
try (URLClassLoader classLoader = AccessController.doPrivileged(
(PrivilegedAction<URLClassLoader>) () ->
new URLClassLoader(new URL[]{jarUrl}, MyMain.class.getClassLoader())
)) {
Class<?> clazz = loadMainClass(classLoader, mainClass);
Method main = getMainMethod(clazz, calledByMember);
String[] args = new String[] {""}
main.invoke(null, args);
}
Örnek - File
Şöyle yaparız
private static void loadLibrary(final String libName) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary(libName);
return null;
}
});
}
private static void loadLibraryFile(final String libFileName) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
System.load(libFileName);
return null;
}
});
}
Örnek - Hiding Exceptions
Şöyle yaparız
public static void changePassword() throws FileNotFoundException {
//Use own privilege to open the sensitive password file
final String password_file = "password";
try {
final InputStream in = AccessController.doPrivileged(
new PrivilegedExceptionAction<InputStream>() {
@Override
public InputStream run() throws FileNotFoundException {
return openPasswordFile(password_file); //call the privileged method here
}
}
);
//Perform other operations such as password verification
} catch (PrivilegedActionException exc) {
Exception cause = exc.getException();
if (cause instanceof FileNotFoundException) {
throw (FileNotFoundException)cause;
} else {
throw new Error("Unexpected exception type",cause);
}
}
}