2014-05-24 46 views
0

我正試圖獲得使用Wicket的LDAP登錄頁面。針對Wicket的LDAP身份驗證

我有一個正在工作的LDAP類,如果給定的用戶憑證由LDAP服務器確認,那麼它將使用true/false語句做出響應。

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; 
     } 
    } 




} 

上面的類在使用f.e.時非常好用。通過控制檯。 下一步是創造一個非常簡單的登錄頁面,檢票口:

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); 

    } 
} 

現在我的問題...... 所以這應該是非常簡單的...輸入用戶名/密碼,點擊提交按鈕和...... litterally什麼都沒發生。沒有System.out.println顯示,我似乎代碼只是凍結(從LDAP類的調試信息也沒有顯示)

你們有一個想法,我做錯了什麼?

問候, Dwarfex

+0

提供的答案是正確的,我會建議使用'wicket-auth-roles'。您可以免費獲得一個登錄頁面,並且可以使用一個'AuthenticatedWebSession'來處理應用程序中的許多認證邏輯。 – RobAu

回答

1

嘗試重寫表單的onerror的()函數看看你那裏,我懷疑你的驗證器返回一個錯誤,你就進不了因認爲的onsubmit() 。

比我會建議使用適當的模型爲您的輸入字段。嘗試添加2個字符串屬性到你的頁面(hawKennung和密碼)和getter/setters爲他們。

像這樣創建輸入字段:

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

,只是使用在的onsubmit屬性hawKennung()。 Wicket將負責分配值。