Java 小程序权限 [英] Java Applet Permissions

查看:30
本文介绍了Java 小程序权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经把一个基本的小程序放在一起,用户从他们的硬盘驱动器中选择一个文件,它读取这个文件的第一行并将其传递给 JavaScript 进行一些额外的预处理,然后当你点击一个按钮时它会尝试通过 HTTP POST 请求上传该文件.我找到了一个非常基本的开源小程序,用于上传我为最后一点复制和修改的文件.

I've put together a basic applet where the user selects a file from their hard drive, it reads the first line of this file and passes that off to JavaScript for some additional preprocessing, and then when you click a button it tries to upload that file through an HTTP POST request. I found a very basic open source applet for uploading files that I copied and modified for this last bit.

但问题是,它并不完全有效.看起来它运行良好,但后来我遇到了两个与权限相关的问题.Java 控制台中的消息表明小程序在以下两个权限上存在访问被拒绝错误:

The trouble is, though, it doesn't quite work. It seems like it's running fine, but then I run into two snags related to permissions. The messages in the Java Console say that the applet had access denied errors on the following two permissions:

java.lang.RuntimePermission setFactory
java.io.FilePermission read

我觉得这很奇怪,因为当我在 NetBeans 中选中自签名"选项的情况下构建小程序时,我以为我已经授予了该小程序的权限,然后单击以确认浏览器中的小安全弹出窗口.

I find this strange, because I thought I had granted permission to the applet already when I built it with the "self-signed" option checked in NetBeans, and then clicked to confirm the little security pop-up in the browser.

另外,我自己编码的部分,它读取文件并将第一行传递给 JavaScript 工作正常.这是一个非常明确的指示,表明小程序能够从本地文件系统读取!直到我真正尝试开始上传时,麻烦才开始.我想有一点需要注意的是,上传过程似乎在一个新线程中运行,而其余部分都在主类中运行,而没有创建线程.

Also, the part that I coded myself, where it reads the file and passes the first line on to JavaScript works fine. This is a pretty clear indicator that the applet is able to read from the local file system! The trouble doesn't start until I actually try to start the upload. One thing to note, I suppose, is that the upload process seems to run in a new thread, whereas the rest of it all runs in the main class without creating threads.

我是 Java 的新手,对 Java 中的线程知之甚少;我需要以某种方式将权限传递给这个新线程吗?或者有什么效果?提前致谢.

I am a total novice to Java and know very little about threads in Java; do I need to pass the permissions onto this new thread somehow? Or something to that effect? Thanks in advance.

推荐答案

您可能需要向安全管理员(代码,而不是管理员)请求权限才能执行特权操作.由于各种原因,小程序能够打开本地文件通常不是一件好事,所以它受到了很大的保护.

You probably need to ask the security manager (code, not administrator) for permission to do a privileged operation. For various reasons, it's not generally a good thing for an applet to be able to open a local file, so it's guarded pretty heavily.

基本的关键是调用 AccessController.doPrivileged() 并且有一个 不错的小教程,请参阅 Java Ranch 常见问题解答.

The basic key is to call AccessController.doPrivileged() and there's a good little tutorial on it at the Java Ranch FAQ.

这篇关于Java 小程序权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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