AnsweredAssumed Answered

servlet auto-login

Question asked by nyronian on Jul 13, 2007
Latest reply on Jul 17, 2007 by nyronian
I have created a new servlet (within alfresco) that is designed to "auto-login" the user under a specific name (not guest).  I have not figured out how to do this.  The problem I am trying to solve is there are nodes that I need to be able to access when the servlet is called that is NOT accessable by Guest.  So I am going to "auto-login" a user in order to access these nodes.  How do I do this?

Right now I have the following code that doesn't work: :)

NOTE:  The servlet extends Alfresco's BaseServlet.


   protected void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
   {
      String ticket = login(request);
      
      logger.debug("login ticket = "+ticket);
      AuthenticationStatus status =  AuthenticationHelper.authenticate(getServletContext(), request, response, ticket);
      if (status == AuthenticationStatus.Failure)
      {
         return;
      }
      
      processRequest(request, response, false);

   }
   public String login(HttpServletRequest httpRequest)
   {
      String ticket = null;
      
        HttpSession session = httpRequest.getSession();
        ticket = (String)session.getAttribute(SESSION_USER_TICKET);
      
      FacesContext fc = FacesContext.getCurrentInstance();
      ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
      AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
      try
      {

         authenticationService.authenticate("admin", "admin".toCharArray());
         
         ticket = authenticationService.getCurrentTicket();
         session.setAttribute(SESSION_USER_TICKET, ticket);

      }
      catch (AuthenticationException aerr)
      {
         Utils.addErrorMessage(Application.getMessage(fc, MSG_ERROR_UNKNOWN_USER));
      }
      catch (InvalidNodeRefException refErr)
      {
         Utils.addErrorMessage(MessageFormat.format(Application.getMessage(fc,
               Repository.ERROR_NOHOME), refErr.getNodeRef().getId()));
      }

      return ticket;
   }

I get the following Exception when I try to access a Node:


java.lang.NullPointerException
        at org.springframework.web.jsf.FacesContextUtils.getWebApplicationContex
t(FacesContextUtils.java:47)
        at org.springframework.web.jsf.FacesContextUtils.getRequiredWebApplicati
onContext(FacesContextUtils.java:77)
        at org.alfresco.web.bean.repository.Repository.getServiceRegistry(Reposi
tory.java:410)
        at org.alfresco.web.bean.repository.Node.getServiceRegistry(Node.java:50
0)
        at org.alfresco.web.bean.repository.Node.<init>(Node.java:95)
        at com.tlhc.wc.servlet.PageProcessor.processTemplateRequest(PageProcesso
r.java:146)
        at com.tlhc.wc.servlet.PageProcessor.service(PageProcessor.java:99)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

If I log into alfresco and then access the servlet (under the same session) everything works perfectly :)

Thanks in advance.

Outcomes