java.security.AccessControlException:运行小程序时发生 [英] java.security.AccessControlException: Occured when Running an applet

查看:25
本文介绍了java.security.AccessControlException:运行小程序时发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Herbert Schildt 的书 Java2 Complete Reference 第五版中逐步学习 Java.在我创建简单的 Banner Applet 的过程中,通过创建 Thread 并调用 Applet 的 Repaint() 方法,显示的 Banner 并在 Applet Viewer 上滚动它.但是在创建可运行 Target 的线程对象时,它会抛出这样的异常

I am Learning Java Step by Step From Herbert Schildt Book Java2 Complete Reference Fifth Edition. On my way to creating simple Banner Applet which Display's Banner and Scrolls it on Applet Viewer by creating Thread and calling Repaint() method of Applet. but while crating thread object of runnable Target it throws exception like this

java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:345)
at java.security.AccessController.checkPermission(AccessController.java:555)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at sun.applet.AppletSecurity.checkAccess(AppletSecurity.java:252)
at java.lang.ThreadGroup.checkAccess(ThreadGroup.java:304)
at java.lang.ThreadGroup.<init>(ThreadGroup.java:119)
at java.lang.ThreadGroup.<init>(ThreadGroup.java:95)
at Applet.SimpleBanner.start(SimpleBanner.java:49)
at sun.applet.AppletPanel.run(AppletPanel.java:475)
at java.lang.Thread.run(Thread.java:713)

我读过其他文章说它需要安全权限,但在我的 applet.policy 文件中已经允许所有权限

I read Other Article saying it need Security Permission but in My applet.policy file there is already all permission allowed

grant { permission java.security.AllPermission;};

这只是我的第二个小程序.谁能详细解释一下为什么会抛出安全异常及其解决方案?

This is Only my Second Applet. Can anyone explain in detail why it is Throwing Security Exception and its Solution in simple terms?

这是我的小程序代码.

import java.applet.Applet;
import java.awt.*;

/* A Simple Banner Applet.
 * This Banner Applet Creates a thread that scrolls the message contained
 * in msg right to left across banner's window.
 */

/*
 * <applet code="SimpleBanner" width=300 height=50>
 * </applet>
 */

public class SimpleBanner extends Applet implements Runnable{
private static final long serialVersionUID = 1L;
String msg = "Hello World";
Thread t = null;
ThreadGroup Grp;
int state;
boolean stopflag;

/**
 * Initialization method that will be called after the applet is loaded into
 * the browser.
 */
@Override
public void init() 
    {
    //Set Foreground and background color
    setBackground(Color.cyan);
    setForeground(Color.red);
    }

@Override
public void start()
    {
    //Start Thread
    Grp = new ThreadGroup("Group");
    t = new Thread(Grp, this);
    stopflag = false;
    t.start();
    }
//Entry Point for Thread that Runs The banner
@Override
public void run() 
{
char ch;
//Display Banner
for(;;)
    {
        try {
            repaint();
            Thread.sleep(250);
            ch=msg.charAt(0);
            msg =msg.substring(1,msg.length());
            msg +=ch;
            if (stopflag) 
            {
                break;
            }
        } catch (InterruptedException ex) {

        }
    }
}

@Override
public void stop()
{
// Pause The Banner
stopflag=true;
t=null;
}

@Override
public void paint(Graphics g)
{
//Display The Banner
    g.drawString(msg, 50, 30);
}
}

推荐答案

默认情况下,出于安全原因,小程序在权限受限的沙盒环境中运行.小程序没有运行时权限来创建或修改线程组,因此您会收到异常.不要创建新的线程组.或者通过授予创建或修改线程组的运行时权限来覆盖您的安全策略以明确允许您的小程序创建一个.要覆盖默认权限,请在用户主目录的 .java.policy 文件中定义适当的策略.建议您编辑特定于用户的策略文件,而不是 JRE 安全目录下的全局策略文件.

Applets, by default, run in a sandbox environment with restricted permissions because of security reasons. Applets do not have runtime permissions to create or modify thread groups and hence you are getting the exception. Do not create a new thread group. Or else override your security policy to explicitly allow your applet to create one by granting the runtime permission to create or modify thread group. To override default permissions define appropriate policy in your user home's .java.policy file. It is recommended that you edit your user specific policy file and not the global policy file under your JRE security directory.

使用 JDK 的策略工具来定义策略或手动执行.参考以下模板:

Use a JDK's policy tool to define the policy or do it manually. Refer the template below:

grant codeBase "<code base>" {
  permission <type> "<target>", "<actions>";
  permission <type> "<target>", "<actions>";
  ...
};

For eg. 
grant codeBase "http://geosim.cs.vt.edu/geosim/-" {
  permission java.lang.RuntimePermission "modifyThreadGroup";
  ...
};

这篇关于java.security.AccessControlException:运行小程序时发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆