Dwarfex
Dwarfex

Reputation: 21

LDAP Auth for Wicket

I'm trying to get an LDAP Login Page working with Wicket.

I have an working LDAP Class which responds with a true / false statement if the given user credentials were confirmed by the LDAP Server.

    package Tools;

import javax.naming.*;
import javax.naming.directory.*;

import java.io.FileNotFoundException;
import java.util.Hashtable;


public class LDAP {
    boolean LDAP_ENABLED;

    String LDAPBaseDirectory;
    String LDAP_SERVER_ADDRESS;
    String LDAP_SERVER_PORT;
    String LDAP_USER_DOMAIN;
    String LDAP_DN;
    String LDAP_StandardUserName;
    String LDAP_StandardUserPassword;
    public LDAP(){ //depends on Config Class
        //Import settings from Config
        try{

            Config config = new Config();

            if(config.getProperty_seLDAP_ENABLED()){
                this.LDAP_ENABLED = true;
            }else{
                this.LDAP_ENABLED = false;
            }
            if(this.LDAP_ENABLED){

                this.LDAPBaseDirectory = config.getProperty_seLDAP_BASE_DIRECTORY();
                this.LDAP_SERVER_ADDRESS = config.getProperty_seLDAP_SERVER_ADDRESS();
                this.LDAP_SERVER_PORT = config.getProperty_seLDAP_SERVER_PORT();
                this.LDAP_USER_DOMAIN = config.getProperty_seLDAP_USER_DOMAIN();
                this.LDAP_DN = config.getProperty_seLDAP_DN();
                this.LDAP_StandardUserName = config.getProperty_seLDAP_StandardUserName();
                this.LDAP_StandardUserPassword = config.getProperty_seLDAP_StandardUserPassword();
            }
        } catch (FileNotFoundException e){
            //todo 
        }


    }

    public boolean authentify(String userName, String userPassword){
        System.out.println(userPassword);
        //LDAP responses with "true" if password == null
        if(userPassword.equals("")){
            return false;
        }

        /**
         * TODO
         * Add availability check for LDAP Server
         * 
         */
        try
        {
            System.out.println("Trying LDAP");
            // Set up the environment for creating the initial context
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            String ldapURL = "ldap://" + this.LDAP_SERVER_ADDRESS + ":" +this.LDAP_SERVER_PORT;
            System.out.println("URL: "+ ldapURL);
            env.put(Context.PROVIDER_URL, ldapURL);
            // 
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, this.LDAP_DN +"\\"+ userName ); //"domain\\user"); 
            System.out.println("Principal: "+ this.LDAP_DN +"\\"+ userName ); //DEBUG
            env.put(Context.SECURITY_CREDENTIALS, userPassword);
            System.out.println("Password: "+ userPassword ); //DEBUG

            // Create the initial context

            DirContext ctx = new InitialDirContext(env);
            boolean result = (ctx != null);

//          if(ctx != null)
                ctx.close();
            System.out.println("Result: " + result);

//          return result;
            if(result){

                return true;
            }else{
                return false;
            }
        }
        catch (Exception e)
        {          
            System.out.println(e.getStackTrace());
            e.printStackTrace();
            return false;
        }
    }




}

The above class works pretty fine when used f.e. via console. Next step was creating a pretty simple login page with wicket:

package haw.Ausleihe;

import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.hibernate.Session;

import Tools.LDAP;
import Database.HibernateHelper;
import Database.Entities.User;


public class Login extends WebPage {
    private static final long serialVersionUID = 1L;

    public Login(final PageParameters parameters) {
        super(parameters);

        final TextField<String> hawKennung = new TextField<String>("hawKennung",
                Model.of(""));
        hawKennung.setRequired(true);
        hawKennung.add(new UserValidator());
        final PasswordTextField passwort = new PasswordTextField("passwort", Model.of(""));
        passwort.setRequired(true);

        Form<?> login = new Form<Void> ("login")        
        {
            @Override
            protected void onSubmit() {
                //HibernateHelper hibernate = new HibernateHelper();
                final String usernameValue = hawKennung.getModelObject();
                final String passwordValue = passwort.getModelObject();
                //hibernate.addUser(usernameValue, passwordValue, "", "", "");
//              User tmpUser = hibernate.getUser("abb123");

//              System.out.println("Database Entry: " +  tmpUser.getKennung() + " ; " + tmpUser.getPassword());

                System.out.println(usernameValue);
                System.out.println(passwordValue);
                System.out.println("NOW TESTING LDAP");
                LDAP ldap = new LDAP();
                if(ldap.authentify(usernameValue, passwordValue)){
                    System.out.println("Success");
                }else{
                    System.out.println("Fail");
                }
                setResponsePage(HomePage.class);
            }
        };
        add(login);
        login.add(hawKennung);
        login.add(passwort);

    }
}

Now to my problem... So this should be pretty simple ... enter username/password click the submit button and ... litterally nothing happens. No System.out.println is shown and i seems as if the code just freezes (debug informations from the LDAP class aren't shown as well)

Do you guys have an idea what i have done wrong?

Greetings, Dwarfex

Upvotes: 0

Views: 741

Answers (1)

bert
bert

Reputation: 7696

Try overriding the onError() function of your form an see if you get there, I suspect that your validator returns an error and you do not get into the onSubmit() because of that.

Than I would suggest to use a proper Model for your input fields. Try adding 2 String properties to your Page (hawKennung and password) and getter/setters for them.

Create the input fields like this:

TextField<String> hawKennung = 
        new TextField<>("hawKennung", new PropertyModel(Login.this, "hawKennung");

and just use the property hawKennung in the onSubmit(). Wicket will take care of assigning the value.

Upvotes: 1

Related Questions