Tomcat安全的静态内容

7
我正在制作一个服务,其中包括“相册”功能,为用户提供照片。用户必须被“允许”查看相册中的照片。因此直接将链接发送给其他人不应该允许查看照片。
照片存储在上下文之外的文件夹中。
我需要做的是在用户请求照片时执行一些检查,然后如果检查通过,则提供文件。我想避免重复造轮子,只是让Tomcat像静态文件一样提供图像。你能给些建议吗?

这些检查需要哪些信息,这些信息存储在哪里? - BalusC
只有会话。用户必须注册并且必须具有在其配置文件中描述的访问权限,即会话对象。 - Juriy
1个回答

9

好的,大家好。

在经过艰苦的探索后,我认为我终于找到了解决这个问题的方法。首先,看起来这个问题实际上可以分解为两个独立的任务。其中一个是保护对某些资源的访问,另一个是从上下文外部的文件夹中提供资源。

第一个任务很简单,可以通过编写一个简单的过滤器并将其挂载到“/”来解决。

第二个任务要复杂得多,但幸运的是也可以解决。Tomcat使用javax.naming.directory.DirContext的实现来加载给定Web应用程序的所有资源,包括类文件。它还允许您提供此接口的自定义实现并在context.xml文件中进行配置。默认实现是org.apache.naming.resources.FileDirContext。详细信息请参见:http://tomcat.apache.org/tomcat-6.0-doc/config/resources.html

我创建了自己的DirContext实现,只需扩展FileDirContext即可。幸运的是,只需要覆盖一个方法就可以“挂钩”文件发现。该方法称为file()。

我在这里发布我的测试代码。它还远不完美,没有考虑重命名文件等极端情况,但我认为这些在正常运行服务器时不需要。
这段代码的基本思想是检查路径是否以“虚拟目录”前缀开头,如果是,则在文件系统中的其他位置搜索文件(我知道那里有一些重复的代码,但如果您想使用它,希望您不要懒得删除它 :-)。setVirtualName和setVirtualBase会自动调用以注入配置参数。
/**
 * TODO: add javadocs
 *
 * @author Juriy Bura
 */
public class VirtualFolderDirContext extends FileDirContext {
    private String virtualName;
    private String realName;

    private File virtualBase;
    private String absoluteVirtualBase;


    public VirtualFolderDirContext() {
        super();
    }

    public VirtualFolderDirContext(Hashtable env) {
        super(env);
    }

    public void setVirtualName(String path) {
        virtualName = path;
    }

    public void setVirtualBase(String base) {
        this.realName = base;
        virtualBase = new File(realName);
        try {
            virtualBase = virtualBase.getCanonicalFile();
        } catch (IOException e) {
            // Ignore
        }
        this.absoluteVirtualBase = virtualBase.getAbsolutePath();
    }

    protected File file(String name) {
        File file = null;
        boolean virtualFile = name.startsWith(virtualName + "/");
        if (virtualFile) {
            file = new File(virtualBase, name.substring(virtualName.length()));
        } else {
            file = new File(base, name);
        }

        if (file.exists() && file.canRead()) {

            if (allowLinking)
                return file;

            // Check that this file belongs to our root path
            String canPath = null;
            try {
                canPath = file.getCanonicalPath();
            } catch (IOException e) {
            }
            if (canPath == null)
                return null;

            // Check to see if going outside of the web application root
            if (!canPath.startsWith(absoluteBase) && !canPath.startsWith(absoluteVirtualBase)) {
                return null;
            }

            // Case sensitivity check
            if (caseSensitive) {
                String fileAbsPath = file.getAbsolutePath();
                if (fileAbsPath.endsWith("."))
                    fileAbsPath = fileAbsPath + "/";
                String absPath = normalize(fileAbsPath);
                if (canPath != null)
                    canPath = normalize(canPath);
                if (virtualFile) {
                    if ((absoluteVirtualBase.length() < absPath.length())
                        && (absoluteVirtualBase.length() < canPath.length())) {
                        absPath = absPath.substring(absoluteVirtualBase.length() + 1);
                        if ((canPath == null) || (absPath == null))
                            return null;
                        if (absPath.equals(""))
                            absPath = "/";
                        canPath = canPath.substring(absoluteVirtualBase.length() + 1);
                        if (canPath.equals(""))
                            canPath = "/";
                        if (!canPath.equals(absPath))
                            return null;
                    }                   
                } else {
                    if ((absoluteBase.length() < absPath.length())
                        && (absoluteBase.length() < canPath.length())) {
                        absPath = absPath.substring(absoluteBase.length() + 1);
                        if ((canPath == null) || (absPath == null))
                            return null;
                        if (absPath.equals(""))
                            absPath = "/";
                        canPath = canPath.substring(absoluteBase.length() + 1);
                        if (canPath.equals(""))
                            canPath = "/";
                        if (!canPath.equals(absPath))
                            return null;
                    }
                }
            }

        } else {
            return null;
        }
        return file;

    }
}

在你完成这个类之后,你需要将它打包成jar文件,并将该jar文件放置到Tomcat的lib文件夹中。由于明显的原因,它不能与war文件一起使用。在你的context.xml文件中,你应该添加像这样的配置行:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="true" antiJARLocking="true">

    <Resources
            className="com.juriy.tomcat.virtualdir.VirtualFolderDirContext"
            virtualName="/upload"
            virtualBase="c:/temp/up">
    </Resources>
    ...
    ...

现在,任何时候用户请求 /upload/,它将被解析为 c:\temp。使用这种技术,您可以从几乎任何位置加载资源:http,共享文件夹,数据库,甚至版本控制系统。所以这很酷。
附言:我花了一整天的时间让所有东西都能协同工作,所以如果您喜欢这个答案,请不要犹豫给我投票 :-))
干杯,
Juriy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接