4 Replies Latest reply on Jun 4, 2012 9:30 AM by Andy Peer

    A filter for the login servlet

    Andy Peer Newbie

      Hi, I need to implement some logic before and after the login servlet invoked by my login.jsp.

      So I wrote a filter for the url /login to do that. I need to get the user profile for some operations, so I created this LoginFilter class:

      [code]

      public class LoginFilter implements Filter {

          private static Logger logger = Logger.getLogger(LoginFilter.class);

       

          @Override

          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

              HttpServletRequest httpRequest = (HttpServletRequest) request;

              String username = httpRequest.getParameter("username");

              String password = httpRequest.getParameter("password");

             

              chain.doFilter(request, response);

             

              PortalRequestContext context = PortalRequestContext.getCurrentInstance();

              if (context == null)

                  logger.info("PortalRequestContext is NULL");

              else {

                  String userId = context.getRemoteUser();

                  if (userId == null || userId.equals(""))

                      logger.info("Login failed, IP:" + httpRequest.getRemoteAddr());

                  else

                      logger.info("Login executed, username:" + userId);

              }

       

          }

      [/code]

       

      The problem is that "context" (PortalRequestContext) is always null. What ma I doing wrong? Is this the right approach?

       

      Thanks in advance.

        • 1. Re: A filter for the login servlet
          Minh Hoang TO Novice

          PortalRequestContext is single-request object that exists as the code handling HTTP request enters WebUI layer. As the code of your filter is executed, the PortalRequestContext has not been created on request handling thread yet. So it is always null there!

           

          To get the username, you could simply use the method getRemoteUser from HttpServletRequest.

          • 2. Re: A filter for the login servlet
            Andy Peer Newbie

            Thanks for the reply.

            Well, actually I need the user profile and I solved by calling a static method within the portlets war.

             

            Now I want to do some operations if the login is successful or not. How could I realize it from my filter?

            • 3. Re: A filter for the login servlet
              Minh Hoang TO Novice

              Well, actually I need the user profile and I solved by calling a static method within the portlets war.

               

              To get the user profile, you could go through OrganizationService component. The service components are available even before Tomcat 's Http connector!

               

               

              Now I want to do some operations if the login is successful or not. How could I realize it from my filter?

               

              I don't think a filter is suitable for that. You should implement a JAAS login module, and put your operations in the commit() method. Then configure your login module in gatein-domain JAAS entry.

              1 of 1 people found this helpful
              • 4. Re: A filter for the login servlet
                Andy Peer Newbie

                I don't think a filter is suitable for that. You should implement a JAAS login module, and put your operations in the commit() method. Then configure your login module in gatein-domain JAAS entry.

                That's exactly what I wanted to know

                Could you report some good online resources?