KerberosAuthenticationHandler implements AuthenticationHandler { @Override public void init(Properties config) throws ServletException { try { String principal = config.getProperty(PRINCIPAL); if (principal == null || principal.trim().length() == 0) { throw new ServletException("Principal not defined in configuration"); } keytab = config.getProperty(KEYTAB, keytab); if (keytab == null || keytab.trim().length() == 0) { throw new ServletException("Keytab not defined in configuration"); } File keytabFile = new File(keytab); if (!keytabFile.exists()) { throw new ServletException("Keytab does not exist: " + keytab); } final String[] spnegoPrincipals; if (principal.equals("*")) { spnegoPrincipals = KerberosUtil.getPrincipalNames( keytab, Pattern.compile("HTTP/.*")); if (spnegoPrincipals.length == 0) { throw new ServletException("Principals do not exist in the keytab"); } } else { spnegoPrincipals = new String[]{principal}; } KeyTab keytabInstance = KeyTab.getInstance(keytabFile); serverSubject.getPrivateCredentials().add(keytabInstance); for (String spnegoPrincipal : spnegoPrincipals) { Principal krbPrincipal = new KerberosPrincipal(spnegoPrincipal); LOG.info("Using keytab {}, for principal {}", keytab, krbPrincipal); serverSubject.getPrincipals().add(krbPrincipal); } String nameRules = config.getProperty(NAME_RULES, null); if (nameRules != null) { KerberosName.setRules(nameRules); } String ruleMechanism = config.getProperty(RULE_MECHANISM, null); if (ruleMechanism != null) { KerberosName.setRuleMechanism(ruleMechanism); } final String whitelistStr = config.getProperty(ENDPOINT_WHITELIST, null); if (whitelistStr != null) { final String[] strs = whitelistStr.trim().split("\\s*[,\n]\\s*"); for (String s: strs) { if (s.isEmpty()) continue; if (ENDPOINT_PATTERN.matcher(s).matches()) { whitelist.add(s); } else { throw new ServletException( "The element of the whitelist: " + s + " must start with '/'" + " and must not contain special characters afterwards"); } } } try { gssManager = Subject.doAs(serverSubject, new PrivilegedExceptionAction<GSSManager>() { @Override public GSSManager run() throws Exception { return GSSManager.getInstance(); } }); } catch (PrivilegedActionException ex) { throw ex.getException(); } } catch (Exception ex) { throw new ServletException(ex); } } KerberosAuthenticationHandler();  KerberosAuthenticationHandler(String type); @Override void init(Properties config); @Override void destroy(); @Override String getType(); @Override boolean managementOperation(AuthenticationToken token,
                                     HttpServletRequest request,
                                     HttpServletResponse response); @Override AuthenticationToken authenticate(HttpServletRequest request,
      final HttpServletResponse response); static final Logger LOG; static final String TYPE; static final String PRINCIPAL; static final String KEYTAB; static final String NAME_RULES; static final String RULE_MECHANISM; }
@Test public void testInit() throws Exception { Assert.assertEquals(KerberosTestUtils.getKeytabFile(), handler.getKeytab()); Set<KerberosPrincipal> principals = handler.getPrincipals(); Principal expectedPrincipal = new KerberosPrincipal(KerberosTestUtils.getServerPrincipal()); Assert.assertTrue(principals.contains(expectedPrincipal)); Assert.assertEquals(1, principals.size()); }