use of com.sun.security.auth.LdapPrincipal in project jdk8u_jdk by JetBrains.
the class LdapLoginModule method attemptAuthentication.
/**
* Attempt authentication
*
* @param getPasswdFromSharedState boolean that tells this method whether
* to retrieve the password from the sharedState.
* @exception LoginException if the authentication attempt fails.
*/
private void attemptAuthentication(boolean getPasswdFromSharedState) throws LoginException {
// first get the username and password
getUsernamePassword(getPasswdFromSharedState);
if (password == null || password.length == 0) {
throw (LoginException) new FailedLoginException("No password was supplied");
}
String dn = "";
if (authFirst || authOnly) {
String id = replaceUsernameToken(identityMatcher, authcIdentity);
// Prepare to bind using user's username and password
ldapEnvironment.put(Context.SECURITY_CREDENTIALS, password);
ldapEnvironment.put(Context.SECURITY_PRINCIPAL, id);
if (debug) {
System.out.println("\t\t[LdapLoginModule] " + "attempting to authenticate user: " + username);
}
try {
// Connect to the LDAP server (using simple bind)
ctx = new InitialLdapContext(ldapEnvironment, null);
} catch (NamingException e) {
throw (LoginException) new FailedLoginException("Cannot bind to LDAP server").initCause(e);
}
// Locate the user's distinguished name
if (userFilter != null) {
dn = findUserDN(ctx);
} else {
dn = id;
}
} else {
try {
// Connect to the LDAP server (using anonymous bind)
ctx = new InitialLdapContext(ldapEnvironment, null);
} catch (NamingException e) {
throw (LoginException) new FailedLoginException("Cannot connect to LDAP server").initCause(e);
}
// Locate the user's distinguished name
dn = findUserDN(ctx);
try {
// Prepare to bind using user's distinguished name and password
ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
if (debug) {
System.out.println("\t\t[LdapLoginModule] " + "attempting to authenticate user: " + username);
}
// Connect to the LDAP server (using simple bind)
ctx.reconnect(null);
// Authentication has succeeded
} catch (NamingException e) {
throw (LoginException) new FailedLoginException("Cannot bind to LDAP server").initCause(e);
}
}
// Save input as shared state only if authentication succeeded
if (storePass && !sharedState.containsKey(USERNAME_KEY) && !sharedState.containsKey(PASSWORD_KEY)) {
sharedState.put(USERNAME_KEY, username);
sharedState.put(PASSWORD_KEY, password);
}
// Create the user principals
userPrincipal = new UserPrincipal(username);
if (authzIdentity != null) {
authzPrincipal = new UserPrincipal(authzIdentity);
}
try {
ldapPrincipal = new LdapPrincipal(dn);
} catch (InvalidNameException e) {
if (debug) {
System.out.println("\t\t[LdapLoginModule] " + "cannot create LdapPrincipal: bad DN");
}
throw (LoginException) new FailedLoginException("Cannot create LdapPrincipal").initCause(e);
}
}
use of com.sun.security.auth.LdapPrincipal in project jdk8u_jdk by JetBrains.
the class CreateLdapPrincipals method main.
public static void main(String[] args) throws Exception {
Set<Principal> principals = new Subject().getPrincipals();
principals.add(new LdapPrincipal("x=y"));
principals.add(new LdapPrincipal("x=#04024869"));
principals.add(new LdapPrincipal("1.2.3=x"));
principals.add(new LdapPrincipal("A=B"));
principals.add(new LdapPrincipal("a=b+c=d"));
principals.add(new LdapPrincipal("a=b,c=d,e=f"));
principals.add(new LdapPrincipal("f=g, h=i, j=k"));
System.out.println("Successfully created " + principals.size() + " LDAP principals:");
System.out.println(principals);
}
Aggregations