users@glassfish.java.net

Re: login Captcha -> Filter on j_security_check

From: <glassfish_at_javadesktop.org>
Date: Tue, 03 Mar 2009 05:20:21 PST

Hi, I'm experiencing the same problem. I have a filter, but it does not appear to be accessed during the j_security_check action of the login form. Can anyone direct us as to what we GlassFish users may do to overcome this issue?

My web.xml contains the following:
=======================================================
  <filter>
        <description>Performs pre-login and post-login operation</description>
        <filter-name>LoginFilter</filter-name>
        <filter-class>com.myCompany.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
          <filter-name>LoginFilter</filter-name>
        <url-pattern>/j_security_check</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>



My com.myCompany.LoginFilter java class contains the following:
=======================================================
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {

  protected FilterConfig filterConfig;

  // Called once when this filter is instantiated. If this is mapped to
  // j_security_check, called very first time j_security_check is invoked.
  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
  }

  public void destroy() {
    this.filterConfig = null;
  }
         
  // Called for every request that is mapped to this filter. If mapped to
  // j_security_check, called for every j_security_check action
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws java.io.IOException, ServletException {

    // perform pre-login action here
    
    // calls the next filter in chain. j_security_check if
    // this filter is mapped to j_security_check.
    chain.doFilter(request, response);


    // perform post-login action here.
        String fullName = "";
        String departmentNumber = "";
        String businessCategory = "";
        
    // response.setContentType("text/html;charset=UTF-8");
        HttpSession session = ((HttpServletRequest) request).getSession();

        Hashtable env = new Hashtable(10);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=mycompany,dc=com");

        try {
                DirContext ctx = new InitialDirContext(env);
                Attributes ldapAttribute = ctx.getAttributes("uid=" + request.getAttribute("j_username") + ",ou=People");

                /**
                 * Get ldap user's full name
                 */
                try {
                    fullName = ldapAttribute.get("cn").get().toString();
            session.setAttribute("userFullName", fullName);
            System.out.println(fullName);
                } catch (NamingException e) {
                        System.out.println("No department defined.");
                } catch (NullPointerException npe) {
                        System.out.println("No department defined.");
                }

                /**
                 * Get ldap user's business category
                 */
                try {
                    businessCategory = ldapAttribute.get("businesscategory").get().toString();
            session.setAttribute("userBusinessCategory", businessCategory);
            System.out.println(businessCategory);
                } catch (NamingException e) {
                        System.out.println("No business category defined.");
                } catch (NullPointerException npe) {
                        System.out.println("No business category defined.");
                }
                
                /**
                 * Get ldap user's department number
                 */
                try {
                    departmentNumber = ldapAttribute.get("departmentnumber").get().toString();
            session.setAttribute("userDepartment", departmentNumber);
            System.out.println(departmentNumber);
                } catch (NamingException e) {
                        System.out.println("No department defined.");
                } catch (NullPointerException npe) {
                        System.out.println("No department defined.");
                }

                ctx.close();
        } catch (NamingException e) {
                e.printStackTrace();
        }
    
    }
  }
[Message sent by forum member 'wayneredmon' (wayneredmon)]

http://forums.java.net/jive/thread.jspa?messageID=334785