tomcat - 设置在 Grails 中嵌入 Tomcat 以遵循符号链接(symbolic link)

标签 tomcat grails

这篇文章的简化问题是:

如何访问 Grails 中 WEB-APP 目录之外的文件?

我有一个 grails 应用程序,它与另一个系统共享一些资源(图像)。所以我创建了一个指向新文件的符号链接(symbolic link)。这些文件是由用户上传的,因此它们被有意放置在网络根目录之外。

例如:

/images/country_flags/ --> /some/directory/with/images/country_flags/

所以当tomcat请求图片时:

/images/country_flags/flag1.png

它实际上是:

/some/directory/with/images/country_flags/flag1.png

据我了解,Tomcat 默认情况下不支持符号链接(symbolic link),但可以通过在 META-INF 目录中创建包含以下信息的 context.xml 文件来启用它:

<?xml version="1.0" encoding="UTF-8"?>

<Context path="/images" allowLinking="true"></Context>

Grails 允许通过在脚本目录中创建一个名为“_Events.groovy”的新文件来配置 Tomcat。

从互联网上看,这应该可以解决问题:

eventConfigureTomcat = {tomcat ->
    println "Changing the configuration for tomcat"
    println serverContextPath

    def ctx=tomcat.host.findChild(serverContextPath) 
    ctx.allowLinking = true 
    println "Configuration changed"
} 

但是,我在控制台中得到以下输出:

Changing the configuration for tomcat
/
| Error Exception occurred trigger event [ConfigureTomcat]: Cannot set property 'allowLinking' on null object (Use --stacktrace to see the full trace)

我的application.properties文件如下(注意上下文改为“/”):

#Grails Metadata file
#Wed Sep 26 09:56:39 BST 2012
app.context=/
app.grails.version=2.1.1
app.name=SomeCoolApp
app.version=0.1
plugins.google-visualization=0.5.3
plugins.mail=1.0
plugins.quartz=1.0-RC2
plugins.searchable=0.6.3
plugins.spring-security-core=1.2.7.3

这两种方法都不起作用。有人可以向我指出有关如何使用 _Events.groovy 方法配置 Tomcat 的文档吗?除了尝试打印到控制台之外,还有其他方法可以解决上下文为空的问题吗?

最佳答案

我找到了答案,但我不太明白它为什么有效。 我将上下文从 serverContextPath (返回/)更改为“”并且它有效。

eventConfigureTomcat = {tomcat ->
    println "Changing the configuration for tomcat"
    println serverContextPath

    def ctx=tomcat.host.findChild(serverContextPath) // doesn't work?
    def ctx=tomcat.host.findChild("") // works
    ctx.allowLinking = true 
    println "Configuration changed"
} 

关于tomcat - 设置在 Grails 中嵌入 Tomcat 以遵循符号链接(symbolic link),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12619169/

相关文章:

maven-2 - 在多个服务器上使用 Tomcat Maven 插件

grails - groovy.lang.MissingPropertyException : No such property: configClass for class: java. lang.String

linux - Apache 背后的 Jira、Tomcat 和 JBoss

grails - 从当前语言环境获取Grails中的所有国际字符串?

grails - 如何在 Grails 中外部化自定义约束?

grails - log4j:参数附加程序文件名

unit-testing - 如何在Grails 2单元测试中获取Spring Security提供的currentUser

java - 如何在rest服务中编写web应用初始化代码?

java - 在 eclipse kepler 中安装 Apache Tomcat 8.0.9 时遇到问题

java - 调用 WebService 并遇到 SSL/cert 问题