wildfly – 如何让EAR从WAR读取属性文件?

我正在使用Wildfly 11和
Java 8.我正在尝试部署包含多个WAR文件的EAR文件.我的一个WAR文件在其web.xml中包含这个…

<context-param>
    <param-name>Owasp.CsrfGuard.Config</param-name>
    <param-value>csrfguard.properties</param-value>
</context-param>

有问题的文件在我的一个WAR中

myapp.war/WEB-INF/classes/csrfguard.properties

当我自己部署WAR时,一切都很好.但是当我部署包含WAR的EAR时,我得到一个错误,抱怨无法找到属性文件…

Caused by: java.io.IOException: unable to locate resource - csrfguard.properties
    at org.owasp.csrfguard.CsrfGuardServletContextListener.getResourceStream(CsrfGuardServletContextListener.java:85)
    at org.owasp.csrfguard.CsrfGuardServletContextListener.contextInitialized(CsrfGuardServletContextListener.java:36)
    ... 10 more

我觉得有一个类加载器问题正在发生,我不知道如何解决.如何告诉我的EAR文件在哪里找到有问题的属性文件?

最佳答案 我怀疑使用了错误的类加载器来搜索csrfguard.properties的类路径,这会导致
getResourceAsStream失败.在.ear文件中,CSRFGuard库在哪里打包?

您可以通过切换到相对于.war文件的路径来尝试使用context.getRealPath回退:

<context-param>
    <param-name>Owasp.CsrfGuard.Config</param-name>
    <param-value>WEB-INF/classes/csrfguard.properties</param-value>
</context-param>
点赞