小程序 +对其他 URL 的 Http Get 请求

发布于 2024-11-11 16:21:11 字数 3764 浏览 5 评论 0原文

我有以下课程:

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)

The jar issigned,所以我不确定我在这里做错了什么。有人看到这有什么明显的问题吗?

谢谢。

编辑#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>

EDIT #2

运行请求的命令的输出:

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

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

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

I have the following class:

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;
    }

When I run it local it works fine. However, when I run it on the app server I get:

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)

The jar is signed, so I'm not sure what I'm doing wrong here. Does anyone see any obvious problems with this?

Thanks.

EDIT #1:
Here is the html source:

<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>

EDIT #2

Output from running requested command:

C:\workspaces\workspace-helios-main\testapplet\target>jarsigner -verify testapplet-1.0.0-SNAPSHOT.jar
jar verified.

Warning:
This jar contains entries whose signer certificate has expired.

Re-run with the -verbose and -certs options for more details.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼藏柔 2024-11-18 16:21:11

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

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

另请参阅:

Wrap the applet's init() in AccessController#doPrivileged().

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

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文