String SECURITY_DOMAIN_NAME = "ssd"; // the security domain's name from standalone.xml String username = "user"; String password = "password"; LoginContext lc = null; try { lc = new LoginContext(SECURITY_DOMAIN_NAME, new UsernamePasswordHandler(username, password.toCharArray())); lc.login(); // successful login } catch (LoginException loginException) { // failed login }
private void _checkAuthentication(String username, String password) throws LoginException { try { UsernamePasswordHandler handler = new UsernamePasswordHandler(username, password.toCharArray()); LoginContext loginContext; loginContext = new LoginContext(CustomJaasDeploymentServiceMBean.RHQ_USER_SECURITY_DOMAIN, handler); loginContext.login(); loginContext.getSubject().getPrincipals().iterator().next(); loginContext.logout(); } catch (javax.security.auth.login.LoginException e) { throw new LoginException(e.getMessage()); } }
/** * Creates login context for given {@link Krb5LoginConfiguration} and credentials and calls the {@link LoginContext#login()} * method on it. This method contains workaround for IBM JDK issue described in bugzilla <a * href="https://bugzilla.redhat.com/show_bug.cgi?id=1206177">https://bugzilla.redhat.com/show_bug.cgi?id=1206177</a>. * * @param krb5Configuration * @param user * @param pass * @return * @throws LoginException */ public static LoginContext loginWithKerberos(final Krb5LoginConfiguration krb5Configuration, final String user, final String pass) throws LoginException { LoginContext lc = new LoginContext(krb5Configuration.getName(), new UsernamePasswordHandler(user, pass)); if (IBM_JDK) { // workaround for IBM JDK on RHEL5 issue described in https://bugzilla.redhat.com/show_bug.cgi?id=1206177 // The first negotiation always fail, so let's do a dummy login/logout round. lc.login(); lc.logout(); lc = new LoginContext(krb5Configuration.getName(), new UsernamePasswordHandler(user, pass)); } lc.login(); return lc; }
UsernamePasswordHandler handler = new UsernamePasswordHandler(username, credentials); Configuration conf = getConfiguration();
UsernamePasswordHandler handler = new UsernamePasswordHandler("your-uid", "your-uid-password".toCharArray()); LoginContext lc = new LoginContext("testLdapExample1", handler); lc.login();