【问题标题】:Applet + Http Get Requests to other Url'sApplet + Http 获取对其他 Url 的请求
【发布时间】:2011-06-03 17:54:53
【问题描述】:

我有以下课程:

public class TestApplet extends JApplet {

    private static final long serialVersionUID = -2137477433249866949L;

    private JTextArea display;

    @Override
    public void init() {
        //Create the text area and make it uneditable.
        display = new JTextArea( 1, 80 );
        display.setEditable( false );

        //Set the layout manager so that the text area
        //will be as wide as possible.
        setLayout( new GridLayout( 1, 0 ) );

        //Add the text area to the applet.
        add( new JScrollPane( display ) );

        String getResult = getResult();

        display.setText( getResult );

    }

    public static void main( final String[] args ) {
        System.out.println( new TestApplet().getResult() );
    }

    private String getResult() {
        String getResult = "";
        try {
            GetMethod getMethod = new GetMethod( "http://www.google.com" );
            HttpClient httpClient = new HttpClient();

            httpClient.executeMethod( getMethod );

            getResult = getMethod.getResponseBodyAsString();
        }
        catch ( Exception exception ) {
            getResult = ExceptionUtils.getFullStackTrace( exception );
        }
        return getResult;
    }

当我在本地运行它时,它工作正常。但是,当我在应用服务器上运行它时,我得到:

java.security.AccessControlException: access denied (java.net.SocketPermission www.google.com resolve)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkConnect(Unknown Source)
    at sun.plugin2.applet.Applet2SecurityManager.checkConnect(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at java.net.InetSocketAddress.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
    at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
    at com.test.TestApplet.init(TestApplet.java:40)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

jar 是签名的,所以我不确定我在这里做错了什么。有人发现这有什么明显的问题吗?

谢谢。

编辑#1: 这是html源代码:

<html>
<title>
1.1 TestApplet
</title>
<body>
<h1>
TestApplet
</h1>

    <applet 
        code="com.test.TestApplet" 
        archive="testapplet-1.0.0-SNAPSHOT-jar-with-dependencies.jar"
        width=100% 
        height=100%>
    </applet>

</body>
</html>

编辑#2

运行请求命令的输出:

C:\workspaces\workspace-helios-main\testapplet\target>jarsigner -verify testapplet-1.0.0-SNAPSHOT.jar jar 已验证。

警告: 此 jar 包含签名者证书已过期的条目。

使用 -verbose 和 -certs 选项重新运行以获取更多详细信息。

【问题讨论】:

  • @javamonkey79: "jar is 已签名,.." 在加载小程序时,您是否收到信任代码的提示?小程序是否被 JavaScript 调用?
  • 小程序直接在html中调用——我将编辑并发布html源代码。我没有收到提示,但我确实使用了 jarsigner 实用程序对其进行了签名。
  • @javamonkey79: "我没有得到提示.." jarsigner -verify testapplet-1.0.0-SNAPSHOT-jar-with-dependencies.jar 产生了什么输出?
  • 添加了一个编辑来回答你的问题。
  • @javamonkey79: AFAIR 用于的 JRE 允许加载由过期证书签名的 Jars。最近 Sun/Oracle 一直在加强安全性,如果这种可能性被关闭,我不会感到惊讶。尝试使用未过期的证书重新签署小程序。

标签: java applet httpclient


【解决方案1】:

将小程序的init() 包装在AccessController#doPrivileged() 中。

public void init() {
    AccessController.doPrivileged(new PrivilegedAction<Object> {
        @Override 
        public Object run() {
            // Put your original init() here.
            return null;
        }
    });
}

另见:

【讨论】:

  • 我试过这个。我想我可能需要对证书做一些事情以及不签署它以使其工作。嗯...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2011-05-09
  • 1970-01-01
相关资源
最近更新 更多