AnsweredAssumed Answered

Alfresco Share Autologin

Question asked by sergey_kharitonov on Dec 15, 2009
Latest reply on Jan 15, 2015 by vurquia
Hi, Alfrescans!

I investigate this subject and develop a simple autologin filter

This code is not ideal, but I think, it helps somebody.

Before apply filter, create new user in the Alfresco anonymous:anonymous


package ru.eurekabpo.alfresco.autologin;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.alfresco.connector.User;
import org.alfresco.web.site.AuthenticationUtil;
import org.alfresco.web.site.FrameworkHelper;
import org.alfresco.web.site.RequestContext;
import org.alfresco.web.site.RequestUtil;
import org.alfresco.web.site.UserFactory;
import org.alfresco.web.site.exception.RequestContextException;
/**
* Simple Autologin filter for Alfresco Share
* @author skharitonov
*
*/
public class AutoLoginFilter implements Filter {

   public void destroy() {
   }

   public void init(FilterConfig config) throws ServletException {
   }

   /**
    * Run the filter
    *
    * @param sreq
    *            ServletRequest
    * @param sresp
    *            ServletResponse
    * @param chain
    *            FilterChain
    * @exception IOException
    * @exception ServletException
    * @throws
    */
   public void doFilter(ServletRequest sreq, ServletResponse sresp,
         FilterChain chain) throws IOException, ServletException {
      // Get the HTTP request/response/session
      HttpServletRequest req = (HttpServletRequest) sreq;
      HttpServletResponse resp = (HttpServletResponse) sresp;
      HttpSession httpSess = req.getSession(true);

      // check if user is already authenticated
      try {
         RequestContext context = RequestUtil.getRequestContext(req);
         User user = context.getUser();
         if (user != null && !user.getId().equals(UserFactory.USER_GUEST)) {
            // already authenticated
            chain.doFilter(sreq, sresp);
            return;
         }
      } catch (RequestContextException e) {
         e.printStackTrace();
      }
      String ref = req.getHeader("referer");
      /*
      Enumeration en = req.getHeaderNames() ;
      while(en.hasMoreElements()){
      String name = (String) en.nextElement()   ;
      System.out.println(name+":"+req.getHeader(name));
      }
      System.out.println("query:"+req.getQueryString());
      System.out.println("ref:"+ref);
      */
      if (ref == null || ref.length() == 0 || !ref.endsWith("share/page/user/anonymous/dashboard")) {
         String username = "anonymous";
         String proxyticket = "anonymous";

         try {
            // pass the proxy CAS ticket to alfresco to authenticate (and
            // get an alfresco ticket)
            UserFactory userFactory = FrameworkHelper.getUserFactory();
            boolean authenticated = userFactory.authenticate(req, username,
                  proxyticket);
            if (authenticated) {
               // this will fully reset all connector sessions
               AuthenticationUtil.login(req, resp, username);
            }
         } catch (Throwable err) {
            throw new ServletException(err);
         }
      }
      chain.doFilter(sreq, sresp);
   }

}



Modify webapps/share/WEB-INF/web.xml

<filter>
      <filter-name>Authentication Filter</filter-name>
      <filter-class>ru.eurekabpo.alfresco.autologin.AutoLoginFilter</filter-class>
  
   </filter>
     
   <!– For NTLM authentication support enable the following mappings –>
   <!– after enabling the NTLMAuthenticationFilter filter class above –>

   <filter-mapping>
      <filter-name>Authentication Filter</filter-name>
      <url-pattern>/page/*</url-pattern>
   </filter-mapping>
  
   <filter-mapping>
      <filter-name>Authentication Filter</filter-name>
      <url-pattern>/p/*</url-pattern>
   </filter-mapping>
  
   <filter-mapping>
      <filter-name>Authentication Filter</filter-name>
      <url-pattern>/s/*</url-pattern>
   </filter-mapping>

Outcomes