我提到了一个小演示。在 为嵌入 HTML 的 Applet 设置策略中,一位冰茶 JRE 用户评论了该演示。对他们来说失败了。他们拒绝了对该小程序的许可(从而将其限制在沙盒中),并且应该看到绿色的“这个小程序是沙盒”页面。相反,小程序完全失败了,他们看到了小程序本应位于的“灰色空间”。
我在 WAGing 它试图实例化一个File
不同的对象。IE Sun/Oracle JRE 将毫无问题地允许它,仅当小程序尝试创建JFileChooser
. OTOH 冰茶 JRE 不允许
File
创建。
因此,这段代码应该可以解决这个问题。它将
JEditorPane
第一个“所有其他失败”消息的创建/添加和安装,然后是绿色的“沙盒”页面移动到new File(..)
调用之前。
我的问题是。对于拥有冰茶 JRE 的用户,此代码是否“像广告宣传的那样工作”?
要测试它:
- 访问 pscode.org/test/docload/applet-latest.html上的小程序
- 拒绝数字签名的代码。 这对于创建合适的条件来测试小程序非常重要。
- 观察/报告小程序是否加载了绿色的 sandbox.html。沙盒文档将代表修复错误的“成功”。
同样令人感兴趣(可能很少)是 Demo of Defensive Loading of Trusted Applets的主页,它链接到 applet 页面、applet 中显示的每个 HTML 文件,以及包含源代码的 ZIP 存档的代码和 HTML,以及 Ant build.xml,这样您就可以“在家做这件事,孩子们”。
这是新代码。
package org.pscode.eg.docload;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JFileChooser;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.File;
import java.io.IOException;
import java.security.AccessControlException;
/** An applet to display documents that are JEditorPane compatible.
This applet loads in a defensive way in terms of the security environment,
in case the user has refused to accept the digitally signed code. */
public class DocumentLoader extends JApplet {
JEditorPane document;
@Override
public void init() {
System.out.println("init()");
JPanel main = new JPanel();
main.setLayout( new BorderLayout() );
getContentPane().add(main);
document = new JEditorPane("text/html",
"<html><body><h1>Testing</h1><p>Testing security environment..");
main.add( new JScrollPane(document), BorderLayout.CENTER );
System.out.println("init(): entering 'try'");
try {
// set up the green 'sandboxed URL', as a precaution..
URL sandboxed = new URL(getDocumentBase(), "sandbox.html");
document.setPage( sandboxed );
// It might seem odd that a sandboxed applet can /instantiate/
// a File object, but until it goes to do anything with it, the
// JVM considers it 'OK'. Until we go to do anything with a
// 'File' object, it is really just a filename.
System.out.println("init(): instantiate file");
File f = new File(".");
System.out.println("init(): file instantiated, create file chooser");
// Everything above here is possible for a sandboxed applet
// *test* if this applet is sandboxed
final JFileChooser jfc =
new JFileChooser(f); // invokes security check
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setMultiSelectionEnabled(false);
System.out.println(
"init(): file chooser created, " +
"create/add 'Load Document' button");
JButton button = new JButton("Load Document");
button.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int result = jfc.showOpenDialog(
DocumentLoader.this);
if ( result==JFileChooser.APPROVE_OPTION ) {
File temp = jfc.getSelectedFile();
try {
URL page = temp.toURI().toURL();
document.setPage( page );
} catch(Exception e) {
e.printStackTrace();
}
}
}
} );
main.add( button, BorderLayout.SOUTH );
// the applet is trusted, change to the red 'welcome page'
URL trusted = new URL(getDocumentBase(), "trusted.html");
document.setPage(trusted);
} catch (MalformedURLException murle) {
murle.printStackTrace();
document.setText( murle.toString() );
} catch (IOException ioe) {
ioe.printStackTrace();
document.setText( ioe.toString() );
} catch (AccessControlException ace) {
ace.printStackTrace();
// document should already be showing sandbox.html
}
}
@Override
public void start() {
System.out.println("start()");
}
@Override
public void stop() {
System.out.println("stop()");
}
@Override
public void destroy() {
System.out.println("destroy()");
}
}