2013-03-26 97 views
0

不止一個問題,我需要使用jsf執行身份驗證。我開發了一個登錄,它接收一個存儲在MySQL中的用戶名和密碼。從Active Directory登錄後,這應該採用AD的用戶名和密碼,我想這應該與MySQL的用戶名和密碼相同。使用JSF 2,1和Apache Tomcat登錄LDAP

然後,要進入系統,您不再看到登錄,而是直接看到主頁或主頁。

我希望你的幫助和提前致謝。

問候。

+0

恐怕你需要做的很多,更精確的是你有什麼工作,什麼不是。我曾與MySQL,AD和LDAP合作過,你的問題對我來說絕對沒有意義。 – Hazzit 2013-03-26 23:25:41

回答

0

這是我的解決方案,它爲我工作: 編輯faces-config.xml中:

<lifecycle> 
     <phase-listener> 
      com.xxx.admin.security.Login 
     </phase-listener> 
    </lifecycle> 

類登錄:

public class Login implements PhaseListener { 
    private static final String USER_LOGIN_OUTCOME = "login"; 
    @Override 
     public void afterPhase(PhaseEvent event) { 
      FacesContext context = event.getFacesContext(); 
      if (userExists(context)) { 
       // 1. Update last login 
       // 2. may be expired ??? 
       ExternalContext extContext = context.getExternalContext(); 
       try { 
        ETT_UserDTL tmpUser = (ETT_UserDTL) extContext.getSessionMap().get(User.USER_SESSION_KEY); 
        if (!Authenticator.authenticateUser(tmpUser, context)) { 
         // send the user to the login view 
         reLogin(context); 
        } else { 
         ; 
        } 
        // allow processing of the requested view 
       } catch (Exception ex) { 
        SystemLogger.getLogger().error(ex); 
        ClientMessage.logErr(ex.toString()); 
        reLogin(context); 
       } 
      } else { 
       // send the user to the login view 
       reLogin(context); 
      } 
     } 
    private boolean userExists(FacesContext context) { 
    // Need re-check authenticator here. 
    // Check user exist 
    ExternalContext extContext = context.getExternalContext(); 
    return (extContext.getSessionMap().containsKey(User.USER_SESSION_KEY)); 
} 
private void reLogin(FacesContext context) { 
     // send the user to the login view 
     if (requestingSecureView(context)) { 
      context.responseComplete(); 
      context.getApplication(). 
        getNavigationHandler().handleNavigation(context, 
        null, 
        USER_LOGIN_OUTCOME); 
     } else { 
      ; 
     } 
    } 
    } 

LDAPAuthentication:

public class LDAPAuthentication { 

    static String ATTRIBUTE_FOR_USER = "sAMAccountName"; 

    @SuppressWarnings("unchecked") 
    public Attributes authenticateUser(String username, String password, String strDomain, String strHost, String dn) throws NamingException { 

     String searchFilter = "(&(objectClass=user)(" + ATTRIBUTE_FOR_USER + "=" + username + "))"; 
     // Create the search controls 

     SearchControls searchCtls = new SearchControls(); 
     // searchCtls.setReturningAttributes(returnedAtts); 
     // Specify the search scope 
     searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE); 
     String searchBase = dn; 
     Hashtable environment = new Hashtable(); 
     environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     // Using starndard Port, check your instalation 
     environment.put(Context.PROVIDER_URL, "ldap://" + strHost); 
     environment.put(Context.SECURITY_AUTHENTICATION, "simple"); 

     environment.put(Context.SECURITY_PRINCIPAL, username + "@" + strDomain); 
     environment.put(Context.SECURITY_CREDENTIALS, password); 

     LdapContext ctxGC = null; 
     try { 
      ctxGC = new InitialLdapContext(environment, null); 
      // Search for objects in the GC using the filter 
      NamingEnumeration answer = ctxGC.search(searchBase, searchFilter, searchCtls); 
      while (answer.hasMoreElements()) { 
       SearchResult sr = (SearchResult) answer.next(); 
       Attributes attrs = sr.getAttributes(); 
       if (attrs != null) { 
        return attrs; 
       } 
      } 
     } catch (Exception e) { 
      SystemLogger.getLogger().error(e); 
     } 
     return null; 
    } 
} 

認證:

public static boolean authenticateLDAPUser(String strUser, String strPass, String strDomain, String strHost) throws NamingException, Exception { 
     LDAPAuthentication ldap = new LDAPAuthentication(); 
     Attributes att = ldap.authenticateUser(strUser, strPass, strDomain, strHost, ""); 
     if (att != null) { 
      try { 
       ETT_UserDTL tmpUser = (ETT_UserDTL) DataUtil.performAction(DATA_UserGUI.class, "getInfByUserName", strUser); 
       tmpUser.setPassword(strPass); 
       if (!otherAuthenticate(tmpUser)) { 
        Authenticator.removeUser(); 
        return false; 
       } else { 
        ; 
       } 
       pushUser(tmpUser); 
       return true; 
      } catch (TelsoftException ex) { 
       SystemLogger.getLogger().error(ex); 
       return false; 
      } 
     } else { 
      updateLoginFail(); 
      return false; 
     } 
    } 
+0

感謝您的回覆,但我仍然有疑問。當我驗證AD憑據與存儲在數據庫中的憑據相同時?我有一個方法login()在我的JSF支持bean中。省略它並使用前面提到的「authenticateLDAPUser」方法?問候和感謝。 – 2013-04-04 15:20:05

+0

忽略com.xxx.admin.security.Login? – 2013-04-04 15:29:46

+0

這是我已經添加的行,它檢查用戶是否通過身份驗證。我的問題是,如果AD用戶和密碼必須與數據庫的用戶名和密碼相同,並且用戶有權限,但是這些位於數據庫中。 – 2013-04-04 15:36:18