简体   繁体   English

java.security.AccessControlException:运行小程序时发生

[英]java.security.AccessControlException: Occured when Running an applet

I am Learning Java Step by Step From Herbert Schildt Book Java2 Complete Reference Fifth Edition. 我正在从Herbert Schildt的书Java2 Complete Reference Fifth Edition中逐步学习Java。 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. 在创建简单的Banner小程序的过程中,该方法通过创建Thread并调用Applet的Repaint()方法,在Applet Viewer上显示并显示Banner。 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)

I read Other Article saying it need Security Permission but in My applet.policy file there is already all permission allowed 我阅读其他文章说它需要安全权限,但是在我的applet.policy文件中已经允许了所有权限

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? 谁能简单地详细解释为什么它是“引发安全异常”及其解决方案?

Here is My Applet Code. 这是我的小程序代码。

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

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. Applet没有运行时权限来创建或修改线程组,因此您将获得异常。 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. 否则,通过授予运行时创建或修改线程组的权限,覆盖您的安全策略以明确允许您的applet创建一个。 To override default permissions define appropriate policy in your user home's .java.policy file. 要覆盖默认权限,请在用户主目录的.java.policy文件中定义适当的策略。 It is recommended that you edit your user specific policy file and not the global policy file under your JRE security directory. 建议您编辑用户特定的策略文件,而不是JRE安全目录下的全局策略文件。

Use a JDK's policy tool to define the policy or do it manually. 使用JDK的策略工具定义策略或手动执行。 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";
  ...
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.security.AccessControlException:在浏览器上运行小程序时,访问被拒绝 - java.security.AccessControlException: access denied when running applet on browser Java applet java.security.AccessControlException - Java applet java.security.AccessControlException Applet与mysql通信中的java.security.AccessControlException - java.security.AccessControlException in applet communicating with mysql “java.security.AccessControlException:access denied”执行签名的Java Applet - “java.security.AccessControlException: access denied” executing a signed Java Applet 使用java.awt.Robot类在applet中进行屏幕捕获时,java.security.AccessControlException - java.security.AccessControlException when using java.awt.Robot class for screen capture in applet 具有JDBC的Applet-java.security.AccessControlException:访问被拒绝 - Applet with JDBC - java.security.AccessControlException: access denied rmi java.security.AccessControlException - rmi java.security.AccessControlException 运行rmi程序时如何解决java.security.AccessControlException问题? - How do I sort the java.security.AccessControlException issue when running rmi programs? 使用 IntelliJ IDEA 运行 JUnit 测试 - “java.security.AccessControlException” - Running JUnit tests with IntelliJ IDEA - "java.security.AccessControlException" 与Google App Engine开发服务器一起运行最新的Rails App时,java.security.AccessControlException - java.security.AccessControlException when running fresh Rails App with Google App Engine Dev Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM