12-15-2009 04:02 AM
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);
}
}
<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>
01-05-2010 08:58 AM
01-11-2010 11:55 PM
What location did you place the filter code? e.g. filename and directoryThomas, you need to compile this code, pack into jar and place into /tomcat/webapps/share/WEB-INF/lib/youJarName.jar
12-07-2010 05:39 AM
01-09-2011 01:03 PM
01-24-2013 05:54 AM
04-24-2013 11:40 PM
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;
}
}
09-13-2013 12:10 AM
<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>
01-15-2015 07:47 AM
01-15-2015 06:54 PM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.