System Properties involving RMI
System.setProperty("java.security.policy","C:\\Program Files\\eclipse\\security\\rmi.policy");
Properties p = System.getProperties();
p.setProperty("java.security.policy","C:\\Program Files\\eclipse\\security\\rmi.policy");
rmi.policy의 내용
grant {
permission java.net.SocketPermission "*:1024-65535",
"connect,accept";
permission java.net.SocketPermission "*:80", "connect";
};
policy 파일을 편집하지 않고 소스코드 상에서 시스템 속성을 설정하는 방법
SecurityManager의 checkPermission()메소드는 디폴트로 예외를 던지도록 되어 있다. 예외(SecurityException)가 발생하면 해당 기능은 보안상 허용되지 않음을 의미한다. 그러므로 특정 기능을 보안해제하여 사용가능하도록 설정하려면 아래처럼 아무것도 처리하지 않고 예외를 던지지 않도록 오버라이드하면 된다.
아래와 같이 작성된 SecurityManager 를 프로그램에 등록할 때는 다음과 같이 하면된다.
System.setSecurityManager(new MySecurityManager() )
SecurityManager 클래스를 오버라이드하여 특정기능의 보안을 해제하는 예
public class MySecurityManager extends SecurityManager {
public void checkPermission(Permission perm) {
if (perm instanceof SocketPermission) return;
if (perm instanceof ReflectPermission) return;
if (perm instanceof SerializablePermission) return;
if (perm instanceof FilePermission) {
FilePermission fileperm=(FilePermission) perm;
String filename=fileperm.getName();
String test = fileperm.getActions();
//don't allow files to be deleted
if (test.indexOf("delete")==-1) return;
}
//throw a security exception for anything you don't want to allow
throw new SecurityException("access?"+perm.getClass().toString()+":"+ perm.getName());
}
public void checkRead(FileDescriptor fd) {
}
public void checkRead(String file, Object context) {
}
}
Permission의 하위 클래스에는 다음과 같은 것들이 있다.
AllPermission , BasicPermission , FilePermission , MBeanPermission , PrivateCredentialPermission , ServicePermission , SocketPermission , UnresolvedPermission
The property java.rmi.server.codebase is used to specify a URL. This URL points to a file:, ftp:, or http: location which supplies classes for objects that are sent from this JRE. If a program running in a JRE sends an object to another JRE (as the return value from a method), that other JRE needs to load the class file for that object.
When RMI sends the object via serialization RMI embeds the URL specified by this parameter into the stream, alongside of the actual object.
It is important to note that RMI does not send class files along with the serialized objects.
If the remote JRE needs to load a class file for an object, it looks for the embedded URL and contacts the server at that location for the file.
RMI 시스템의 클라이언트가 RMI 서버측의 레지스트리에 접속하여 원격객체의 참조를 가져올 때 클라이언트측에 수신되는 것은 원격객체의 Stub 클래스 인스턴스가 직렬화되어 전달된다. 클라이언트가 직렬화되어 있는 인스턴스를 바로 사용할 수는 없고 다시 역직렬화하여 정상적인 인스턴스로 복원해야 사용가능한 상태가 된다. 이 때 직렬화된 인스턴스를 다시 정상적인 인스턴스로 복원하는 과정에서 클래스파일을 참조해야 할 필요가 있다. 그래서 클라이언트는 Stub 클래스를 찾기위해 로컬 시스템의 CLASSPATH를 참조하고 이 곳에 없다면 서버에서 제공한 java.rmi.server.codebase 속성의 값을 참고하여 서버에 Stub 클래스를 요청한다. java.rmi.server.codebase 의 속성 값으로 사용할 수 있는 URL은 file://, ftp://, http:// 등이 있으며, file://은 로컬시스템 내에 클래스 파일이 있을 경우에만 사용할 수 있다. 일반적으로 http:// 가 사용된다.