安全管理器与Java 17和Sping Boot Runnable jar有问题

vc9ivgsu  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(192)

我正在将一个项目从Java-8和Sping Boot 2.x迁移到Java 17和Spring Boot 3.x。该项目使用Security Manager满足一些内部需求,目前无法删除。当项目开始使用intellij时,即
java -classpath <all local classes>;<all dependencies> <main class>
它工作得很好,但如果使用最终的可执行jar,那么会出现很多找不到类的异常,比如logger、ignite等。此外,如果安全管理器关闭,则不会出现错误
java -jar <executable jar>
用于启用自定义安全管理器的代码

log.warn("Enabling PlatformSecurityManager");
    Policy policy = Policy.getPolicy();
    PermissionCollection perm = policy.getPermissions(new CodeSource(null, (Certificate[]) null));
    boolean canReplaceSecurityManager = perm.implies(new RuntimePermission("setSecurityManager"));
    if (System.getSecurityManager() != null && !canReplaceSecurityManager) {
      //we have to do the check like this because it will be from a different classloader
      if (!(System.getSecurityManager().getClass().getName().equals(PlatformSecurityManager.class.getName()))) {
        throw new RuntimeException(
            "You have started the JVM with a security manager that doesn't allow me to set a new one!");
      }
      //else another platform application has specified the security manager... so we should be ok
    } else {
      if (System.getProperty("java.security.policy") == null) {
        log.warn("java.security.policy Not found");
        //load up allow all policy
        log.info("Enabling AllPermission for the platform security manager");
        PlatformSecurityPolicy platformPolicy = new PlatformSecurityPolicy();
        platformPolicy.addPermission(new AllPermission());
        Policy.setPolicy(platformPolicy);
      }

      log.warn("Enabled PlatformSecurityManager");
      System.setSecurityManager(new PlatformSecurityManager(false));
    }

如果需要任何其他信息,请添加注解。任何领导/帮助都非常感谢。

fzsnzjdm

fzsnzjdm1#

根据下面的文章,这是Java 11 +的Sping Boot 中的一个错误,虽然他们已经修复了它是Spring 2.3+,但同样没有前端移植到3.x,所以在应用下面的解决方案后,它对我有效
-Dsun.misc.URLClassPath.disableJarChecking=true
参考文献
https://github.com/spring-projects/spring-boot/issues/25538

相关问题