Java 24:SecurityManager将被永久禁用

JEP 486:Permanently Disable the Security Manager

JEP486:该特性为一个正式特性,不论客户端还是服务端,SecurityManager已经不再是安全方面的首选,在Java17的JEP411被标注为Deprecated,在Java24将其禁用。

SecurityManager是Java的一种安全机制,用于控制应用程序的权限,限制对系统资源(文件、网络、运行时、属性、反射等)的访问。

试用一下SecurityManager,先写一段代码打印SecurityManager,并读取文件,打印其内容,最后打印一下os.name属性。


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class SecurityManagerTest {

    public static void main(String[] args)throws FileNotFoundException {

        System.out.println("SecurityManager: " + System.getSecurityManager());

        try (FileInputStream fis = new FileInputStream("/yourpath/to/file")) {
            int data;
            while ((data = fis.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("OS name: " + System.getProperty("os.name"));

    }

}

在Java17环境运行:

 

 此时没有添加SecurityManager直接运行,SecurityManager打印为null。

添加启动参数-Djava.security.manager运行:

 因为在Java17特性中被标注为Deprecated,因此打印了WARNING,并打印了SecurityManager,但读文件没有做授权,抛出了AccessControlException异常。

添加policy文件授权文件的读取权限

 添加参数-Djava.security.policy运行:

这里发现os.name也读取出来了,这是因为Java的安全策略默认包含了一些基础权限,如果换成file.encoding,则需要添加额外的读取授权

换成Java24

 再次执行:

 SecurityManager已经掉队了。

 

2025-06-28 11:42:45 25/06/28 03:42:45 INFO Worker: Executor app-20250628034119-0000/32 finished with state EXITED message Command exited with code 1 exitStatus 1 2025-06-28 11:42:45 25/06/28 03:42:45 INFO ExternalShuffleBlockResolver: Clean up non-shuffle and non-RDD files associated with the finished executor 32 2025-06-28 11:42:45 25/06/28 03:42:45 INFO ExternalShuffleBlockResolver: Executor is not registered (appId=app-20250628034119-0000, execId=32) 2025-06-28 11:42:45 25/06/28 03:42:45 INFO Worker: Asked to launch executor app-20250628034119-0000/33 for KafkaNumberAdder 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: Changing view acls to: spark 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: Changing modify acls to: spark 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: Changing view acls groups to: 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: Changing modify acls groups to: 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: spark; groups with view permissions: EMPTY; users with modify permissions: spark; groups with modify permissions: EMPTY 2025-06-28 11:42:45 25/06/28 03:42:45 INFO ExecutorRunner: Launch command: "/opt/bitnami/java/bin/java" "-cp" "/opt/bitnami/spark/conf/:/opt/bitnami/spark/jars/*" "-Xmx1024M" "-Dspark.driver.port=36357" "-Dspark.ui.port=4041" "-Djava.net.preferIPv6Addresses=false" "-XX:+IgnoreUnrecognizedVMOptions" "--add-opens=java.base/java.lang=ALL-UNNAMED" "--add-opens=java.base/java.lang.invoke=ALL-UNNAMED" "--add-opens=java.base/java.lang.reflect=ALL-UNNAMED" "--add-opens=java.base/java.io=ALL-UNNAMED" "--add-opens=java.base/java.net=ALL-UNNAMED" "--add-opens=java.base/java.nio=ALL-UNNAMED" "--add-opens=java.base/java.util=ALL-UNNAMED" "--add-opens=java.base/java.util.concurrent=ALL-UNNAMED" "--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED" "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED" "--add-opens=java.base/sun.nio.cs=ALL-UNNAMED" "--add-opens=java.base/sun.security.action=ALL-UNNAMED" "--add-opens=java.base/sun.util.calendar=ALL-UNNAMED" "--add-opens=java.security.jgss/sun.security.krb5=ALL-UNNAMED" "-Djdk.reflect.useDirectMethodHandle=false" "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED" "--add-opens=java.base/java.lang=ALL-UNNAMED" "org.apache.spark.executor.CoarseGrainedExecutorBackend" "--driver-url" "spark://CoarseGrainedScheduler@spark-master:36357" "--executor-id" "33" "--hostname" "172.20.0.4" "--cores" "2" "--app-id" "app-20250628034119-0000" "--worker-url" "spark://Worker@172.20.0.4:43009" "--resourceProfileId" "0" worker节点的日志
06-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值