Hallo Alle
Ich habe eine user_login_002.java Datei wo es ein Login gibt. Das sieht wie folgt aus:
Momentan wird überprüft ob es den User "müllerp" mit dem Passwort "pmüller" gibt.
Das funktioniert auch super. Auch die ausgabe geht.
Nun will ich aber, dass es nicht einen Definierten User sucht. Sondern, dass es nach dem User sucht, welcher eingegeben wird. Ich mache die eingabe über die Datei: login_seite.jsp
Kann mir jemand bei der Verknüpfung helfen? Ich habe schon vieles im Internet geschaut, aber bekomme es nicht hin. Hat jemand eine Ahnung, was ich am Code verändern muss um das Login erfolgreich zu gestallten?
LG
Ich habe eine user_login_002.java Datei wo es ein Login gibt. Das sieht wie folgt aus:
Java:
public class user_login_002 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws NamingException {
final String ldapAdServer = "ldap://XXXXXXXXX";
final String ldapSearchBase = "dc=identity-directory,dc=users,dc=ID";
final String ldapUsername = "cn=Directory Manager";
final String ldapPassword = "secret";
final String ldapAccountToLookup = "müllerp";
Hashtable<String, Object> env = new Hashtable<String, Object>();
// Specify SSL
env.put(Context.SECURITY_AUTHENTICATION, "simple");
if(ldapUsername != null) {
env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
}
if(ldapPassword != null) {
env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
}
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapAdServer);
//ensures that objectSID attribute values
//will be returned as a byte[] instead of a String
env.put("java.naming.ldap.attributes.binary", "objectSID");
// the following is helpful in debugging errors
//env.put("com.sun.jndi.ldap.trace.ber", System.err);
LdapContext ctx = new InitialLdapContext(env, null);
user_login_002 ldap = new user_login_002();
//1) lookup the ldap account
SearchResult srLdapUser = ldap.findAccountByAccountName(ctx, ldapSearchBase, ldapAccountToLookup);
Hashtable<String, Object> newEnv=(Hashtable<String, Object>)env.clone() ;
if (srLdapUser != null) {
String dn=srLdapUser.getNameInNamespace();
if (ldap.testLogin(newEnv, dn, "pmüller")) {
System.out.println("alles ok!");
} else {
System.out.println("leider falsches Passwort!");
}
}
}
public SearchResult findAccountByAccountName(DirContext ctx, String ldapSearchBase, String accountName) throws NamingException {
String searchFilter = "(&(objectClass=person)(uid=" + accountName + "))";
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ctx.search(ldapSearchBase, searchFilter, searchControls);
SearchResult searchResult = null;
if(results.hasMoreElements()) {
searchResult = (SearchResult) results.nextElement();
//make sure there is not another item available, there should be only 1 match
if(results.hasMoreElements()) {
System.err.println("Matched multiple users for the accountName: " + accountName);
return null;
}
}
return searchResult;
}
public boolean testLogin(Hashtable<String, Object> env ,String dc, String userPassword) {
LdapContext ctx=null;
env.put(Context.SECURITY_PRINCIPAL, dc);
env.put(Context.SECURITY_CREDENTIALS, userPassword);
try {
ctx = new InitialLdapContext(env, null);
return true;
} catch (AuthenticationException e) { // Invalid Login
return false;
} catch (NamingException e) { // The base context{
return false;
}
finally {
try {
if (ctx != null) {
ctx.close();
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Momentan wird überprüft ob es den User "müllerp" mit dem Passwort "pmüller" gibt.
Das funktioniert auch super. Auch die ausgabe geht.
Nun will ich aber, dass es nicht einen Definierten User sucht. Sondern, dass es nach dem User sucht, welcher eingegeben wird. Ich mache die eingabe über die Datei: login_seite.jsp
Code:
<form method="get" name="login" action="user_login.java">
Benutzername<br />
<input type="text" name="loginName" /><br />
Passwort<br />
<input type="password" name="password" /><br />
<br />
<br />
<input type="submit" value="Login" />
</form>
Kann mir jemand bei der Verknüpfung helfen? Ich habe schon vieles im Internet geschaut, aber bekomme es nicht hin. Hat jemand eine Ahnung, was ich am Code verändern muss um das Login erfolgreich zu gestallten?
LG