Monday 15 June 2015

mysql - Login with LDAP using JSF 2,1 & Apache Tomcat -


More than a problem, I need to authenticate using JSF. I have developed a login, which is a user Receives names and passwords that are stored in MySQL. After logging in from Active Directory, the username and password of E. should be taken, which, I think, should be the same as MySQL.

Then, to enter the system, you should now see

Greetings.

This is my solution, it worked for me: edit faces-config.xml: < / P>

  & lt; Lifecycle & gt; & Lt; Step Listener & gt; Com.xxx.admin.security.login & lt; / Phase-listener> & Lt; / Life cycle & gt;   

Category login:

  applies the public class login PhaseListener {Private Static Last String USER_LOGIN_OUTCOME = "Login to"; Override public zero after @files (Festive Event) {FacesContext Reference = event.getFacesContext (); If (userExists (reference)) // // 1. Last login // update 2. can end ??? ExternalContext extContext = context.getExternalContext (); . {ETT_UserDTL tmpUser = (ETT_UserDTL) try extContext.getSessionMap () find (User.USER_SESSION_KEY); If (! Authenticator.authenticateUser (tmpUser, context)) {// Login view to the user in re-entry (reference); } other { ; } // Allow the process of the requested view} Catch (Exception Pre) {SystemLogger.getLogger (). Error (ex); ClientMessage.logErr (ex.toString ()); Re-login (context); }} And {// send the user login view in re-entry (reference); }} Private Boolean UserExists (Facessex Context) {// Researcher Authenticator is required. // Check User ExternalContext ExtContext = context.getExternalContext (); Return (extContext.getSessionMap (). Contains KEY (User.USER_SESSION_KEY)); } Private Zero Rewrite (FacesContext Reference) {// login to user in login view if (requestingSecureView (reference)) {context.responseComplete (); Context.getApplication (). GetNavigationHandler (). HandleNavigation (Reference, Blank, USER_LOGIN_OUTCOME); } other { ; }}}   

LDAPAuthentication:

  public class LDAPAuthentication {static string ATTRIBUTE_FOR_USER = "sAMAccountName"; @SuppressWarnings throws public property authenticateUser (string user name, string password, string strDomain, string strhost, string dn) NamingException {string searchFilter = "(& objectClass = user) (" + ATTRIBUTE_FOR_USER + "= "+ Username" +)); // Search Control Search Search Search Search Certificate = New Search Search (); // searchCtls.setReturningAttributes; // Specify search scope searchCtls.setSearchScope (SearchControls.OBJECT_SCOPE); String Searchbase = DN; Hashtable Environment = New HashTable (); Environment.put (reference. INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // Using StarDard Port, check your installation environment. Input (context.PROVIDER_URL, "ldap: //" + strHost); Environment.put (reference. SECURITY_AUTHICICATION, "SIMPLE"); Environment.put (reference. SECURITY_PRINCIPAL, user name + "@" + strDomain); environment. Input (reference. SECURITY_CREDENTIALS, password); LdapContext ctxGC = Faucet; {CtxGC = Try New InitialLdapContext (Environment, Blank); // search for objects in GC using filter naming Answer = ctxGC.search (searchBase, searchFilter, searchCtls); While (Answer hasMoreElements ()) {SearchResult SR = (SearchResult) answer.next (); Attribute = sr.getAttributes (); If (attrs! = Null) {return attrs; }}} Hold (exception e) {SystemLogger.getLogger (). Error (e); } Return tap; Authentication:   

Authentication:

  Public Stabilized Bullion Certified DripUuser (String strUser, String Straps, String Strodman, String Strhost) Naming Exceptions, Exceptions {LDAPAuthentication ldap = New LDAP authentication (); Att 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 (! Other System (tmpUser)) {Authenticator.removeUser (); return false; } other { ; } PushUser (tmpUser); Back true; } Hold (TelsoftException East) {SystemLogger.getLogger (). Error (ex); return false; }} And {updateLoginFail (); return false; }}    

No comments:

Post a Comment