本文介绍了为嵌入在 HTML 中的 Applet 设置策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个 Applet 来截取屏幕截图并使用 java.awt.Robot 类将其保存在用户计算机上.我需要将此小程序嵌入到 html 页面中(使用 object 标签),以便当用户单击网页上的按钮时截取屏幕截图.

I have designed an Applet to take a screenshot and save it on the users computer using the java.awt.Robot class. I need to embedd this applet into an html page (using the object tag) so that when the user clicks a button on the webpage the screenshot is taken.

小程序本身运行良好,我已经通过向它添加一个临时 main 方法并在我的本地机器上将它作为常规 Java 应用程序运行来测试它.

The applet itself works fine, i've tested it by adding a temporary main method to it and running it on my local machine as a regular java app.

我遇到困难的地方是设置权限以允许它从其嵌入位置运行.很明显,robot 类有点危险,所以需要建立 AWTPermission 并需要对小程序本身进行签名.

Where I'm having difficulty is setting up permissions to allow it to run from its embedded location. Obviously the robot class is somewhat hazardous so an AWTPermission needs to be established and the applet itself needs to be signed.

我按照 http://download.oracle 上的教程进行了操作.com/javase/tutorial/security/toolsign/index.html 并成功创建了一个签名的 .jar 文件,然后创建了一个允许该教程中的演示应用程序运行的策略文件.我现在遇到的问题是如何将我学到的知识与我的小程序将要使用的情况相协调.

I followed through the tutorial at http://download.oracle.com/javase/tutorial/security/toolsign/index.html and succeeded in creating a signed .jar file and then a policy file that allowed the demo application in that tutorial to run. Where I am now running into issues is how to reconcile what I've learned with the situation my applet will be used in.

我的目标受众包括大约 100 台机器,我需要它在所有机器上都可以执行.我已经将我的 java .class 文件打包成一个 .jar 并使用 keytool 和 jarsigner 对其进行签名.然后我将 .jar 和 .cer 文件上传到托管相关页面的服务器目录.

My target audience comprises around 100 machines and I need it to be executable on all of them. I have packed my java .class file into a .jar and signed it using keytool and jarsigner. I then uploaded the .jar and .cer files to the server directory where the pages in question are hosted.

然而:当我使用 policytool 在其中一台机器上创建一个新的策略文件来测试设置时,我仍然无法从 HTML 执行小程序.我收到 Java.Security.AccessControlException Acess Denied java.awt.AWTPermission createRobot 错误.

However: When I then used policytool to create a new policy file on one of the machines to test the setup I am still unable to execute the applet from the HTML. I get Java.Security.AccessControlException Acess Denied java.awt.AWTPermission createRobot errors.

我比较怀疑是政策步骤出了问题,所以我将概述我采取的步骤:我将证书下载到本地机器并从中生成一个密钥库,我通过命令行从此目录启动policytool"我在本地机器上添加了生成密钥库和我的证书所在的目录.然后我点击添加策略按钮并输入 SignedBy 别名然后添加权限并选择 AWTPermission目标名称我选择 createRobot功能字段我一直留空,因为我想不出这里有什么适用此窗口中的签名者也留空然后我点击确定"和完成"并收到一条警告,提示我在第一步中输入的别名没有公钥.我执行另存为"并将我的策略文件保存到与我放置证书和从中生成的密钥库相同的目录中.

I rather suspect its the policy step that is going awry, so I'll outline the steps I took:I download the certificate to the local machine and generate a keystore from it, I launch 'policytool' from this directory through the commandlineI add the directory on the local machine where the keystore generated from and my certificate is located.I then hit the add policy button and enter the SignedBy aliasThen Add Permissions and select AWTPermissionTargets name I select createRobotThe function field I have been leaving blank as I cant think what would apply hereSigned By in this window is also left blankI then hit 'OK' and 'Done' and get a warning that there is no public key for the alias I've entered in the first step. I do a 'save as' and save my policyfile to the same directory as I put the certificate and the keystore generated from it.

然而,这不允许我从网页上运行小程序,而且我对编程这方面的有限理解无法提供关于出了什么问题的线索.

This is not allowing me to run the applet from the webpage however and my limited understanding of this aspect of programming offers no clues as to what has gone wrong.

想法、想法、观察?如果我没有明确提到某事,那么我还没有做到.我最大的怀疑是我收到的警告,但我似乎无法找到它出现的原因

Ideas, thoughts, observations? If I havent explicitly mentioned something then I havent done it. My biggest suspect is the warning I recieve but I cant seem to find why its appearing

忘了提到一个步骤.我手动将policy.url.3=file:/C:/Testing/debugpolicy"行添加到我的 jre\lib\security\java.security 文件中,因为那是我在上述步骤中创建的路径和策略文件名.我刚刚还设法删除了我之前提到的警告,我一直在混淆我的别名,并在创建策略文件期间为私有密钥库提供了别名而不是公共密钥库,但是我仍然遇到相同的问题

Forgot to mention a step. I manually added to my jre\lib\security\java.security file the line 'policy.url.3=file:/C:/Testing/debugpolicy' since thats the path and policy filename I created during the above steps. I also just now managed to remove the warning I mentioned earlier, I'd been mixing up my alias' and gave the alias for the private keystore rather than the public one during policyfile creation, however I still encounter the same problems

推荐答案

如果小程序签名正确,则不需要策略文件,也不需要单独上传任何证书.一个正确签名的小程序会在它加载之前在小程序被访问时提示用户获得许可.是否出现提示?

If an applet is correctly signed, no policy file is required, nor is it required to separately upload any certificate. A correctly signed applet will prompt the user for permission when the applet is visited, before it loads. Does the prompt appear?

这是一个小演示.我写的演示了可信小程序的防御性加载.这就是我所指的安全提示.

Here is a small demo. I wrote that demonstrates Defensive loading of trusted applets. That is the security prompt I am referring to.

如果小程序经过开发人员的数字签名并得到最终用户的信任,它应该能够截取屏幕截图.

If the applet is both digitally signed by the developer and trusted by the end user, it should be able to take a screen-shot.

如果小程序受信任,您还可以尝试另一件事,就像实验 (1). 在小程序 init() 的早期,调用 System.setSecurityManager(null).这将测试小程序是否具有信任,并清除给予小程序的受信任"安全管理器的最后残余.

There is one other thing you might try if the applet is trusted, just as an experiment (1). Early in the applet init(), call System.setSecurityManager(null). That will both test if the applet has trust, and wipe away the last remnants of the 'trusted' security manager given to applets.

如果可行,并且使屏幕捕获成功,则表明存在错误或 Oracle 改变了对受信任小程序可以执行的默认设置的想法.

And in the case that works, and it makes the screen capture successful, it suggests either a bug or Oracle changed their mind about the defaults of what a trusted applet could do.

1) 不要在现实世界或生产环境中这样做.引用 Tom Hawtin 的话:

1) Don't do this in a real world or production environment. To quote Tom Hawtin:

这个问题似乎给人的印象是调用 System.setSecurityManager(null); 没问题....如果有人有任何疑问,更改小程序中的全局状态将影响同一进程中的所有小程序.清除安全管理器将允许任何未签名的小程序做它喜欢做的事情.请不要使用您希望任何人信任的证书签署使用全局状态的代码.

这是该演示中使用的简单小程序的源代码.出于某种原因,当我最初上传它时,我认为来源不相关.出于某种原因,OTOH 3 人现在要求查看消息来源.当我得到一个圆形的 tuit 时,我会将源上传到我的网站.同时,我会把它放在这里.


Edit 1:Here is the source of the simple applet used in that demo. For some reason when I originally uploaded it, I decided the source was not relevant. OTOH 3 people have now asked to see the source, for one reason or another. When I get a round tuit I'll upload the source to my site. In the mean time, I'll put it here.

package org.pscode.eg.docload;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.security.*;

/** An applet to display documents that are JEditorPane compatible. */
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);
        try {
            // 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.
            File f = new File(".");

            // set up the green 'sandboxed page', as a precaution..
            URL sandboxed = new URL(getDocumentBase(), "sandbox.html");
            document = new JEditorPane(sandboxed);
            main.add( new JScrollPane(document), BorderLayout.CENTER );
            // 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);

            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();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (AccessControlException ace) {
            ace.printStackTrace();
        }
    }

    @Override
    public void start() {
        System.out.println("start()");
    }

    @Override
    public void stop() {
        System.out.println("stop()");
    }

    @Override
    public void destroy() {
        System.out.println("destroy()");
    }
}

这篇关于为嵌入在 HTML 中的 Applet 设置策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:00