Java程序访问权限控制(policy文件)

当你想对Java程序进行权限控制时,可以考虑启用SecurityManager安全管理器,并配置XX.policy文件来达到你想的效果。(想对安全管理器进一步了解,可以阅读《深入Java虚拟机》)
1.启动SecurityManager开关
默认情况下,JVM是不启动安全检查的,所以要想让程序在沙箱中运行,必须将开关打开。打开的方式有两种,一种是在启动运行中追加JVM参数 -Djava.security.manager,还有一种方式是在程序中直接设置:System.setSecurityManager(new SecurityManager());,这两种方式是等价的。
2.JVM自带的java.policy文件

java.policy文件位于%JAVA_HOME%/ jre/lib/security/下,默认内容如下:

// Standard extensions get all permissions by default
 
grant codeBase "file<img src="%5C%22static/image/smiley/default/shy.gif%5C%22" smilieid="\"8\"" alt="\"\"" border="\"0\"">{
  {java.ext.dirs}}/*" {
        permission java.security.AllPermission;
};
 
// default permissions granted to all domains
 
grant { 
        // Allows any thread to stop itself using the java.lang.Thread.stop()
        // method that takes no argument.
        // Note that this permission is granted by default only to remain
        // backwards compatible.
        // It is strongly recommended that you either remove this permission
        // from this policy file or further restrict it to code sources
        // that you specify, because Thread.stop() is potentially unsafe.
        // See "http://java.sun.com/notes" for more information.
        permission java.lang.RuntimePermission "stopThread";
 
        // allows anyone to listen on un-privileged ports
        permission java.net.SocketPermission "localhost:1024-", "listen
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值