使用“java.security.krb5.conf”更新 kerberors krb.conf 文件 System.property() 不起作用

问题描述

我想指向一个不同的 krb.conf 文件dynamically,without restarting the JVM。我在 Stackoverflow 上搜索了不同的解决方案,并尝试相应地实施该解决方案。但是有些方法,即使如果我更新 System.property("java.security.krb5.conf",...) 以指向 the new krb.conf file,JAAS 也无法理解这仍然使用较早的 conf 文件。以下是我使用代码解决方案的详细信息:

我的Jaas.conf文件如下:

   Mutual {
      com.sun.security.auth.module.Krb5LoginModule required client=TRUE;
   };
   sp.kerb.sso.KinitExample {
      com.sun.security.auth.module.Krb5LoginModule required 
      client=TRUE 
      refreshKrb5Config=true
      debug=true;
  };

出于显而易见的原因,我设置了 refreshKrb5Config=true,因为我想重新加载 krb 配置文件

这是我要执行的代码: 包 sp.kerb.sso;

import sun.security.krb5.internal.tools.Kinit;

public class KinitExample {

public static void main(String[] args) {

      String kerberosFileName = "C:\\Windows\\krb5.ini";
      String jaas_config_file_name = "C:\\Users\\User1\\temp\\howrah.jaas.conf";

      System.setProperty("java.security.auth.login.config",jaas_config_file_name);  // setting the jaas config file
      System.setProperty("java.security.krb5.conf",kerberosFileName); // setting the kerberos file
      System.setProperty("java.security.krb5.debug","true");

      final String administrator = "admin@exampledomain.lab".toupperCase();
      String cacheFileLoc = "C:\\Users\\User1\\temp\\admin.cache";

      // Perfoming Kinit ...
      Kinit.main(new String[]{"-c",cacheFileLoc,administrator,"Password123" });

      kerberosFileName = "C:\\Users\\User2\\temp\\new.krb.conf";    // Using new KRB configuration file

      System.setProperty("java.security.krb5.debug","true");
      System.setProperty("java.security.auth.login.config",jaas_config_file_name); // setting the property again
      
      System.setProperty("java.security.krb5.conf",kerberosFileName); // setting the property again

      System.out.println(System.getProperty("java.security.krb5.conf")); // Prints the updated conf file location.

      cacheFileLoc = "C:\\Users\\User2\\temp\\newadmin.cache";
      String newAdmin = "administrator@test.lab".toupperCase();
      Kinit.main(new String[]{"-c",newAdmin,"Password123" });
    }
 }

admin 的缓存已创建,但 newAdmin 的缓存未创建,因为相应的 krb.conf 文件具有不同的领域和 JAAS 似乎无法从 new.krb.conf 连接到领域,因此失败并显示 infamour 906 错误代码

我做错了什么?我想要达到的目标是可能的吗?我应该如何解决这个问题?


另外请注意,如果我完全评论管理员缓存创建逻辑并从 new.krb.conf(所有与 newAdmin 相关的代码)开始,它就可以正常工作并为 newAdmin 创建缓存

解决方法

您应该调用 sun.security.krb5.Config.refresh(); 以从新文件重新加载配置。