简体   繁体   English

如何在安全管理器策略文件中为可变父目录授予文件读/写权限

[英]How to grant file read/write permission in a Security Manager policy file for variable parent directories

I'm learning to use Security Manager, and I'm getting this error when I run my unit tests: 我正在学习使用安全管理器,并且在运行单元测试时遇到此错误:

Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Users\[username]\AppData\Local\NetBeans\Cache\7.4\executor-snippets\junitvmwatcher1469887727677239882.properties" "write")

It's simple enough grant permission to that directory. 授予该目录足够简单的权限。 The problem is I run this code on different computers. 问题是我在不同的计算机上运行此代码。 Ideally, I'd like to do something like this: 理想情况下,我想做这样的事情:

permission java.io.FilePermission "*/NetBeans/Cache/7.4/-", "write";

But apparently SecurityManager doesn't recognize wildcard characters at the beginning of the path. 但是显然SecurityManager不能在路径的开头识别通配符。 I've tried using both an asterisk and a dash. 我尝试过同时使用星号和破折号。 Neither works. 都不行。

Basically, I'd like to get my tests to run without needing to hard-code an absolute path. 基本上,我想让我的测试能够运行而无需对绝对路径进行硬编码。 Is there another way to achieve this? 还有另一种方法可以做到这一点吗?

You can use property expansion in policy files : 您可以在策略文件中使用属性扩展

For example, permission java.io.FilePermission "${user.home}", "read"; 例如,权限java.io.FilePermission“ $ {user.home}”,“ read”; will expand "${user.home}" to use the value of the "user.home" system property. 将展开“ $ {user.home}”以使用“ user.home”系统属性的值。

Could you set: 您可以设定:

permission java.io.FilePermission "${user.home}/AppData/Local/NetBeans/-", "write";

to get what you wanted? 得到你想要的?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM