Kerberos5ShiroRealmModule extends AbstractModule { @Override protected void configure() { if (!serverKeyTab.isPresent()) { addError("No -" + SERVER_KEYTAB_ARGNAME + " specified."); return; } if (!serverPrincipal.isPresent()) { addError("No -" + SERVER_PRINCIPAL_ARGNAME + " specified."); return; } String jaasConf = String.format( JAAS_CONF_TEMPLATE, getClass().getName(), serverKeyTab.get().getAbsolutePath(), serverPrincipal.get().getName(), kerberosDebugEnabled); LOG.debug("Generated jaas.conf: " + jaasConf); File jaasConfFile; try { jaasConfFile = File.createTempFile("jaas", "conf"); jaasConfFile.deleteOnExit(); Files.write(jaasConf, jaasConfFile, StandardCharsets.UTF_8); } catch (IOException e) { addError(e); return; } GSSCredential serverCredential; try { LoginContext loginContext = new LoginContext( getClass().getName(), null , null , new ConfigFile(jaasConfFile.toURI())); loginContext.login(); serverCredential = Subject.doAs( loginContext.getSubject(), (PrivilegedAction<GSSCredential>) () -> { try { return gssManager.createCredential( null , GSSCredential.INDEFINITE_LIFETIME, new Oid[] {new Oid(GSS_SPNEGO_MECH_OID), new Oid(GSS_KRB5_MECH_OID)}, GSSCredential.ACCEPT_ONLY); } catch (GSSException e) { throw Throwables.propagate(e); } }); } catch (LoginException e) { addError(e); return; } install(new PrivateModule() { @Override protected void configure() { bind(GSSManager.class).toInstance(gssManager); bind(GSSCredential.class).toInstance(serverCredential); bind(Kerberos5Realm.class).in(Singleton.class); expose(Kerberos5Realm.class); } }); ShiroUtils.addRealmBinding(binder()).to(Kerberos5Realm.class); } Kerberos5ShiroRealmModule(); @VisibleForTesting  Kerberos5ShiroRealmModule(
      File serverKeyTab,
      KerberosPrincipal serverPrincipal,
      GSSManager gssManager); private  Kerberos5ShiroRealmModule(
      Optional<File> serverKeyTab,
      Optional<KerberosPrincipal> serverPrincipal,
      GSSManager gssManager,
      boolean kerberosDebugEnabled);   }
@Test public void testConfigure() throws Exception { expect(serverKeytab.getAbsolutePath()).andReturn("path.keytab"); expect( gssManager.createCredential( EasyMock.isNull(), eq(GSSCredential.INDEFINITE_LIFETIME), anyObject(Oid[].class), eq(GSSCredential.ACCEPT_ONLY))) .andReturn(gssCredential); control.replay(); Guice.createInjector(module).getInstance(Kerberos5Realm.class); }