면책 조항 : 이전 버전의 질문은 SecurityManager
과 AccessController
을 혼란스럽게합니다. 하지만 지금은 실수를 한 것이고 질문은 세련됩니다.AccessController가 권한이없는 액세스를 차단하지 않는 이유
줄기는 꽤 똑바르다. 일부 ScriptEngine에서 스크립트가 수행 할 수있는 작업을 제한하는 방법을 찾고 있습니다.
몇 가지 비슷한 질문을 읽었습니다. ClassFilter
클래스를 사용하는 NashornScriptEngine
에 대한 해결책이있는 것 같습니다. 하지만 스크립팅 엔진 구현에 관계없이 일반적인 방법을 찾고 있습니다. 어떤 이들은 자바의 AccessController
이 길임을 암시합니다.
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
Permissions perms = new Permissions();
ProtectionDomain domain = new ProtectionDomain(new CodeSource(null, (Certificate[]) null), perms);
AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { domain });
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
try {
//I want the following line to throw a SecurityException
return engine.eval("var System = Java.type('java.lang.System'); print(System.getProperty('java.home'));");
}
catch (ScriptException e) {
e.printStackTrace();
}
return null;
}},
acc
);
//At the same time I want the following line to work
System.out.println(System.getProperty("java.home"));
그리고 반군 AccessController에 없다 것처럼 스크립트 실행 : 그래서, 읽고 AccessController
함께 플레이하기 시작했습니다 지금까지 나는이있어!
제 질문은; 이 작업을 수행하는 방법은 AccessController
입니까? 그리고 만약 그렇다면 어떻게해야합니까?
확실히 할 수있는 방법이지만 코드에 SecurityManager가 등록되어 있지 않습니다. –
이 코드를 수정하는 방법을 보여 주시겠습니까? 나는 그것이 무엇이 잘못되었는지를 모른다. 그리고 한 가지 더, 나머지 응용 프로그램은 그대로입니다. 즉, SecurityManager를 적용하고자하는 것은 응용 프로그램의 나머지 부분이 아닌'engine.eval' 행입니다. 감사. – Mehran