gpt4 book ai didi

java - 异常: access denied ("java.net.SocketPermission" "localhost:80" "listen,resolve")

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:42 26 4
gpt4 key购买 nike

将 Eclipse Kepler (Windows 7) 用于在 localhost 端口 80 上打开 ServerSocket 的项目。

我使用的安全管理器的策略文件位于:

C:\Users\John\Developpement\workspace\security\my.policy

在 Eclipse 中,对于项目启动配置属性,对于 VM 参数:

-Djava.security.manager
-Djava.security.policy=${workspace_loc}/security/my.policy

执行的 bin 文件是(我在 Eclipse 中使用单独的源文件夹和输出文件夹):

C:\Users\John\Developpement\workspace\SocketApps\bin\TinyHttpd.class

my.policy中:

grant codeBase "file:\C:\Users\John\Developpement\workspace\SocketApps\bin\-" {
permission java.net.SocketPermission "localhost:80", "listen,resolve";
};

从 Eclipse 运行时:

Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:80" "listen,resolve")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
at java.security.AccessController.checkPermission(AccessController.java:559)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.SecurityManager.checkListen(SecurityManager.java:1134)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at TinyHttpd.main(TinyHttpd.java:35)

到达代码时:

ServerSocket ss = new ServerSocket(80));

如果我删除 codeBase 过滤器:

grant {
permission java.net.SocketPermission "localhost:80", "listen,resolve";
};

问题消失了,所以我想这是 codeBase 的表达方式是错误的。

我已经尝试了针对 this question 提出的解决方案,但它不起作用。你能帮我吗?

最佳答案

自从我发现问题所在以来,就回答我自己的问题。不确定删除这个问题是否更好,在我看来,保留它会对其他人有所帮助。版主说一下。

取自 Oracle documentation :

Note: a codeBase value is a URL and thus should always utilize slashes (never backslashes) as the directory separator, even when the code source is actually on a Win32 system. Thus, if the source location for code on a Win32 system is actually C:\somepath\api\, then the policy codeBase entry should look like:

grant codeBase "file:/C:/somepath/api/" {
...
}

这是初学者的错误。

关于java - 异常: access denied ("java.net.SocketPermission" "localhost:80" "listen,resolve"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24722885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com