cancel
Showing results for 
Search instead for 
Did you mean: 

Alfresco Share Autologin

sergey_khariton
Champ in-the-making
Champ in-the-making
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>
9 REPLIES 9

thomasrjones
Champ in-the-making
Champ in-the-making
What location did you place the filter code? e.g. filename and directory

sergey_khariton
Champ in-the-making
Champ in-the-making
What location did you place the filter code? e.g. filename and directory
Thomas, you need to compile this code, pack into jar and place into /tomcat/webapps/share/WEB-INF/lib/youJarName.jar

novetica
Champ in-the-making
Champ in-the-making
Hi Sergey,
could you confirm that user auth is extracted from http-auth basic in apache/tomcat environment?

Could you provide a .jar already compiled or is alfresco-version dependent?

Thanks,

Marco

vishal3521
Champ in-the-making
Champ in-the-making
Hi Sergey,

I'm using the version 4.0.c. And, I don't find any library jar within the installation to be used for compiling the code given by you. Are these packages (org.alfresco.web.site and org.alfresco.connector) moved to some other distribution? Can you please tell me where can I find these classes/Libraries?

Thanks.

avyaznikov
Champ in-the-making
Champ in-the-making
By the start of 2010, Spring Surf had been contributed as plug-in for Spring Web MVC (<a href="http://wiki.alfresco.com/wiki/Spring_Surf">http://wiki.alfresco.com/wiki/Spring_Surf</a>).
Here is a modified code for 4.0 (4.2) alfresco version. You should create anonymous user manually and correctly setup permissions (according to your demands).


import java.io.IOException;

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.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.extensions.surf.RequestContext;
import org.springframework.extensions.surf.RequestContextUtil;
import org.springframework.extensions.surf.UserFactory;
import org.springframework.extensions.surf.exception.RequestContextException;
import org.springframework.extensions.surf.site.AuthenticationUtil;
import org.springframework.extensions.surf.support.ThreadLocalRequestContext;
import org.springframework.extensions.webscripts.connector.User;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
* Auto login as guest filter
* @author avyaznikov
*
*/
public class AutoLoginFilter implements Filter, ApplicationContextAware {

    private static String username = "anonymous";
    private static String password = "anonymous";

    private ApplicationContext applicationContext;
    private FilterConfig filterConfig;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest sreq, ServletResponse sresp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) sreq;

        HttpServletResponse resp = (HttpServletResponse) sresp;
        HttpSession httpSess = req.getSession(true);

        RequestContext context = ThreadLocalRequestContext.getRequestContext();
        if (context == null) {
            if (applicationContext == null) {
                applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext());
                // applicationContext = RequestContextUtils.getWebApplicationContext(sreq);
            }
            try {
                context = RequestContextUtil.initRequestContext(applicationContext, req);
            } catch (RequestContextException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        User user = context.getUser();
        if (user != null && !user.getId().equals(UserFactory.USER_GUEST)) {
            // already authenticated
            chain.doFilter(sreq, sresp);
            return;
        }

        try {
            // pass the proxy CAS ticket to alfresco to authenticate (and get an alfresco ticket)
            UserFactory userFactory = context.getServiceRegistry().getUserFactory();
            boolean authenticated = userFactory.authenticate(req, username, password);
            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);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

}


hont_vn
Champ in-the-making
Champ in-the-making
Hello,

Do we need to replace the existing Authentication Filter in Alfresco Share, or use both the existing one and the new AutoLoginFilter?

The existing filter:

<filter>
      <description>Share SSO authentication support filter.</description>
      <filter-name>Authentication Filter</filter-name>
      <filter-class>org.alfresco.web.site.servlet.SSOAuthenticationFilter</filter-class>
      <init-param>
         <param-name>endpoint</param-name>
         <param-value>alfresco</param-value>
      </init-param>
   </filter>



I tried to use both of them, but then Alfresco failed to access.

vurquia
Champ in-the-making
Champ in-the-making
I tried to use this class on alfresco 5.0, without success. 

resplin
Elite Collaborator
Elite Collaborator
You are more likely to get a response if you start a new thread, rather than posting to a long quiet thread.