JaasIdentityStore extends AbstractIdentityStore { @Override protected CredentialValidationResult validateUsernamePassword(UsernamePasswordCredential usernamePasswordCredential) { final CallbackHandler callbackHandler = callbacks -> { for (final Callback callback : callbacks) { if (callback instanceof NameCallback) { ((NameCallback) callback).setName(usernamePasswordCredential.getCaller()); } else if (callback instanceof PasswordCallback) { ((PasswordCallback) callback).setPassword(usernamePasswordCredential.getPassword().getValue()); } else { throw new UnsupportedCallbackException(callback); } } }; LoginContext loginContext = null; Subject subject = null; String caller = null; List<String> groups = null; List<String> roles = null; try { loginContext = new LoginContext(appConfigurationEntryName, new Subject(), callbackHandler, configuration); loginContext.login(); subject = loginContext.getSubject(); if (null == subject) throw new IllegalStateException("Null Subject"); caller = jaasSubjectPrincipalResolver.getCaller(subject); groups = jaasSubjectPrincipalResolver.getCallerGroups(subject, caller); roles = jaasSubjectPrincipalResolver.getCallerRoles(subject, caller); } catch (LoginException e) { e.printStackTrace(); System.out.println("Login invalid: " + e.getMessage()); return CredentialValidationResult.INVALID_RESULT; } catch (Exception e) { System.out.println("Login not validated: " + e.getMessage()); return CredentialValidationResult.NOT_VALIDATED_RESULT; } finally { try { if (null != loginContext) { loginContext.logout(); } } catch (Exception e) { System.out.println("Logout exception: " + e.getMessage()); } } return new CredentialValidationResult(CredentialValidationResult.Status.VALID, caller, groups, roles); } JaasIdentityStore(
        String appConfigurationEntryName,
        Configuration configuration,
        JaasSubjectPrincipalResolver jaasSubjectPrincipalResolver); @Override List<String> getCallerGroups(String callerName); @Override List<String> getCallerRoles(String callerName);  }
@Test public void validateUsernamePassword() throws IOException { JaasTestArtifacts.TestJaasSubjectPrincipalResolver resolver = new JaasTestArtifacts.TestJaasSubjectPrincipalResolver(); Configuration config = new JaasTestArtifacts.TestConfiguration(); JaasIdentityStore store = new JaasIdentityStore(JaasTestArtifacts.CONFIG_ENTRY_NAME, config, resolver ); assertEquals("validate", CredentialValidationResult.Status.VALID, store.validateUsernamePassword(new UsernamePasswordCredential("jsmith", new Password("welcome1"))).getStatus()); assertEquals("validate", CredentialValidationResult.Status.INVALID, store.validateUsernamePassword(new UsernamePasswordCredential("jsmith", new Password("badPassword"))).getStatus()); }