Tomcat filters - authentication for supporting static content

First post! Woohoo!

Recently, I've been struggling with the following question:

"We have a lot of static HTML pages and binary files that support (are linked to by) our channel content. How do I make sure these files are only available to users who are logged in to the portal?"

I saw a few solutions like this one around here that revolved around injecting a bit of JSP at the top of each file that verifies the user's session. Unfortunately, since I want to protect binary files as well, this wouldn't work for me. After much pondering, I came up with a hack that seems worth posting:

The idea here is to use a tomcat filter to verify a user's session for every file in a certain directory. This works particularly well with a push-CMS, which we happen to have. Here's the filter code:

package edu.tamhsc.my.filters;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.pipeline.web.WebUtil;

public final class IntranetAuthenticationFilter implements Filter {
    private FilterConfig filterConfig = null;

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

    public void destroy() {
	this.filterConfig = null;
    }

    public void doFilter(ServletRequest request,
			 ServletResponse response, FilterChain chain) 
	throws IOException, ServletException {
	
	
	PrintWriter out = response.getWriter();
	if (WebUtil.getUserSession((HttpServletRequest)request, (HttpServletResponse)response) == null) {
	    out.println("<h2>You are not authorized to view this resource.  You must <a href='/cp/home/loginf'>log in to myHSC</a>.</h2>");
	    out.flush();
	    return;
	}
	
	chain.doFilter(request, response);
    }
}

Compile and jar this, and place the jar in $CP_ROOT/webapps/luminis/WEB-INF/lib/

Then, in $CP_ROOT/webapps/luminis/WEB-INF/web.xml, add something like this:

<filter>
  <filter-name>intranet_auth_filter</filter-name>
  <filter-class>edu.tamhsc.my.filters.IntranetAuthenticationFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>intranet_auth_filter</filter-name>
  <url-pattern> /intranet/* </url-pattern>
</filter-mapping>

That's more or less it. Restart 70-webserver, and place your authenticated content in the appropriate folder (here it's CP_ROOT/webapps/luminis/intranet). That's all there is to it =)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Nice article

Very useful information.
Another way: create the folder 'intranet' under
$CP_DOC_ROOT/ipx
and the URL to access is: http://lum-server/cp/ips/intranet
will have the same affect.

--
Thai Nguyen