JBoss 7.1.1.Final - Log4j - Modules - Logging configuration
sanssan Jan 22, 2013 5:23 AMHi Team,
I do have a LDAP Custom Login module which has been deployed as a module in JBoss AS 7.1.1.Final.
Is there any way, I can use configure that logging to go on a log file.
Java class:
package com.xyz.ldaplogin.module; import java.security.acl.Group; import java.util.Hashtable; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import javax.naming.ldap.InitialLdapContext; import javax.security.auth.login.LoginException; import org.apache.log4j.Logger; import org.jboss.security.SimpleGroup; import org.jboss.security.SimplePrincipal; import org.jboss.security.auth.spi.LdapExtLoginModule; import com.xyz.ldaplogin.util.GenericLDAPLoginUtil; /** * @author SanthoshK * */ public class GenericLVLDAPLoginModule extends LdapExtLoginModule { private static Logger LOGGER = Logger .getLogger(GenericLVLDAPLoginModule.class); private String xyzUserName; private String xyzUserName; private void assignUserCredentials() throws LoginException { LOGGER.info("assignUserCredentials - START"); String[] loginCredentials = null; try { loginCredentials = getUsernameAndPassword(); this.xyzUserName = loginCredentials[0]; this.xyzUserName = loginCredentials[1]; } catch (Exception exception) { LOGGER.error("assignUserCredentials Exception : " + exception); throw new LoginException("Invalid Login Credentials : " + loginCredentials); } finally { LOGGER.info("assignUserCredentials - END"); } } @Override protected String bindDNAuthentication(InitialLdapContext ctx, String user, Object credential, String baseDN, String filter) throws NamingException { return super.bindDNAuthentication(ctx, user, credential, baseDN, filter); } @Override protected void rolesSearch(InitialLdapContext ctx, SearchControls constraints, String user, String userDN, int recursionMax, int nesting) throws NamingException { super.rolesSearch(ctx, constraints, user, userDN, recursionMax, nesting); } @Override protected boolean validatePassword(String inputPassword, String expectedPassword) { return validation(); } @Override protected String getUsersPassword() throws LoginException { return ""; } private transient SimpleGroup userRoles = new SimpleGroup("Roles"); @Override protected Group[] getRoleSets() throws LoginException { // Add each role to the "Roles" principal (Ref web.xml) userRoles.addMember(new SimplePrincipal("Authenticated_users")); Group[] roleSets = { userRoles }; return roleSets; } private boolean validation() { try { LOGGER.info("authenticate - START"); this.assignUserCredentials(); LOGGER.info("Attempting to validate user : [" + this.xyzUserName + "]"); GenericLDAPLoginUtil genericLDAPLoginUtil = new GenericLDAPLoginUtil(); Hashtable<String, String> envHTable = genericLDAPLoginUtil.getEnvironmentTable(); DirContext ctx = new InitialLdapContext(envHTable, null); SearchControls searchCtls = new SearchControls(); String returnedAtts[] = { "cn", "givenName" }; searchCtls.setReturningAttributes(returnedAtts); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); String searchFilter = "(&(sAMAccountName=" + this.xyzUserName + ")(objectCategory=user))"; String searchBase = "DC=group,DC=net"; int totalResults = 0; NamingEnumeration<SearchResult> answer = ctx.search(searchBase, searchFilter, searchCtls); String ouName = null; while (answer.hasMoreElements()) { SearchResult searchResult = answer.next(); totalResults++; ouName = searchResult.getName(); Attributes attrs = searchResult.getAttributes(); if (attrs != null) { try { LOGGER.info(" surname: " + attrs.get("cn").get()); LOGGER.info(" firstname: " + attrs.get("givenName").get()); } catch (NullPointerException e) { LOGGER.info("Errors listing attributes: " + e); } } } LOGGER.info("Total results: " + totalResults); ctx.close(); if (totalResults > 0) { String adminName = ouName + ",dc=group,dc=net"; envHTable = genericLDAPLoginUtil.getEnvironmentTable(adminName, this.xyzUserName); DirContext ctx1 = new InitialLdapContext(envHTable, null); ctx1.close(); LOGGER.info("AUTHENTICATION ["+this.xyzUserName+"] : PASS"); return true; } else { LOGGER.info("AUTHENTICATION ["+this.xyzUserName+"] : FAILED"); return false; } } catch (NamingException exception) { LOGGER.error("Problem searching directory: ", exception); return false; } catch (Exception exception) { LOGGER.error("Unhandled Exception: ", exception); return false; } finally { LOGGER.info("authenticate - END"); } } }
module.xml
<?xml version="1.0" encoding="UTF-8"?> <module xmlns="urn:jboss:module:1.1" name="com.xyz.ldap"> <resources> <resource-root path=""/> <resource-root path="ldap_auth.jar"/> </resources> <dependencies> <module name="javax.api"/> <module name="org.apache.log4j"/> <module name="org.picketbox"/> </dependencies> </module>
And placed log4j.xml next to module.xml...
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="LDAP" class="org.apache.log4j.DailyRollingFileAppender"> <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/> <param name="Append" value="true"/> <param name="File" value="${jboss.server.log.dir}/ldap.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss.SSS} [%C{1}] - %m%n"/> </layout> </appender> <category name="com.xyz" additivity="false"> <priority value="DEBUG"/> <appender-ref ref="LDAP"/> </category> </log4j:configuration>
But, logging goes to server.log. Is there any way, I can configure the logging to go to ldap.log?
Has anybody tried the logging with user modules?