符号链接Tomcat 8目录资源

17

我想在Tomcat 8下创建JSP目录的符号链接。
在Tomcat 7中可以按照以下方式进行操作:

    <Context allowLinking="true">


但是看起来Tomcat 8放弃了这个功能,开始使用资源

(http://tomcat.apache.org/migration-8.html#Web_application_resources)。

我的示例用法:

ROOT/jspdirectory -->链接到--> /var/tmp/realplaceofjspfiles/

一个不良配置:

ROOT/META-INF/context.xml:

<Context>
   <Resources allowLinking="true">
         <PreResources className="org.apache.catalina.webresources.DirResourceSet"   base="/var/tmp/realplaceofjspfiles" internalPath="jspdirectory"/>
    </Resources>
</Context>

对我来说,它出现了这个异常:

    07-Mar-2014 04:09:12.113 WARNING [localhost-startStop-1] org.apache.tomcat.util.digester.SetPropertiesRule.begin [SetPropertiesRule]{Context/Resources/PreResources} Setting property 'internalPath' to 'jspdirectory' did not find a matching
     property.
    07-Mar-2014 04:09:12.114 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
     org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697)
            at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1134)
            at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1780)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            at java.lang.Thread.run(Thread.java:744)
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@4756d5a0]
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
            at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4841)
            at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4966)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
            ... 10 more
    Caused by: java.lang.NullPointerException
            at java.lang.String.startsWith(String.java:1392)
            at java.lang.String.startsWith(String.java:1421)
            at org.apache.catalina.webresources.DirResourceSet.list(DirResourceSet.java:115)
            at org.apache.catalina.webresources.StandardRoot.list(StandardRoot.java:129)
            at org.apache.catalina.webresources.StandardRoot.listResources(StandardRoot.java:313)
            at org.apache.catalina.webresources.StandardRoot.processWebInfLib(StandardRoot.java:523)
            at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:643)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
            ... 13 more

    07-Mar-2014 04:09:12.115 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Error deploying web application directory /PATH/apache-tomcat-8.0.3/webapps/ROOT
     java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697)
            at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1134)
            at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1780)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            at java.lang.Thread.run(Thread.java:744)



我在这里看到了"internalPath":
http://tomcat.apache.org/tomcat-8.0-doc/config/resources.html

2个回答

30

为了使Tomcat能够访问符号链接,请执行以下操作:

$CATALINA_HOME/conf/context.xml

Tomcat 7:

<Context allowLinking="true">
...

Tomcat 8:

->

Tomcat 8:

<Context>
    <Resources allowLinking="true" />
...

不要在Windows(或任何其他大小写不敏感的文件系统)上执行此操作,因为它将禁用大小写敏感性检查,从而可能导致JSP源代码泄露等安全问题。

http://tomcat.apache.org/tomcat-8.0-doc/config/resources.html


当我尝试使用您提供的Tomcat 8解决方案时,我的数据源停止工作。 - Dave
@Dave 尝试使用自动关闭节点 <Resources allowLinking="true" /> 而不是 <Resources allowLinking="true"></Resources>。我刚刚更新了我的答案,说明了我现在的做法。话虽如此,我没有使用任何我知道的 Tomcat 数据源,可能无法帮助你。 - GlenPeterson
太好了!这让我能够像在Tomcat 7中一样使用WEB-INF/lib的符号链接。我的数据源没有任何问题。我已经将<Resources allowLinking="true" />作为现有<Resource ... />同级元素,而不是其内部或周围的元素。 - Tobia

2
似乎您需要一个单独的“webAppMount”属性。
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/">
    <Resources>
        <PreResources
            className="org.apache.catalina.webresources.DirResourceSet"
            base="/FilePath/.../Resources"
            webAppMount="/resources"
            internalPath="/"/>
    </Resources>
 </Context>

这对我很有帮助。它在我的Web应用程序的META-INF/context.xml文件中。


1
然后,您可以通过getContext().getResource("/resources/home.jsp").getPath();访问/FilePath/.../Resources/home.jsp - Richard Osseweyer

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