在 tomcat valve class 中获取 webapp 名称

Get the webapp name in the tomcat valve class

我有我的自定义 tomcat Valve 扩展 FormAuthenticator,并且在每个 webapp 的 context.xml 中定义相同。我需要根据 webapp 名称读取 Valve 'initInternal' 中的一些 属性 文件。即对于每个 webapp 不同的 属性 文件。由于 Valve 很常见,我需要一种方法来找出加载我的 Valve 的上下文。有没有办法在 Valve class.

中获取上下文

我试图在 'ServletContextListener' 中设置系统 属性,但是在初始化 ServletContextListener 之前加载了 Valve class。

这是我的 context.xml 样子。

<Context className="org.apache.catalina.core.StandardContext" debug="0" allowLinking="true" docBase="auth" path="/auth" privileged="true" reloadable="false">
  <Realm className="org.apache.catalina.realm.LockOutRealm" failureCount="5" lockOutTime="300" >
  <Realm className="com.foo.realm.FooRealm" /></Realm>
  <Valve className="com.foo.valves.FooRESTValve" />
</Context>

只有 'docbase' 和 'path' 每个 webapp 不同。

您可以在 context.xml:

中将参数传递给您的阀门
 <Valve className="com.foo.valves.FooRESTValve" myParam="bar" />

Tomcat 将使用参数名称尝试通过调用它的 JavaBean setter:

在阀实现上设置它的值
public class FooRESTValve extends ValveBase {

    ...

    private String myParam;

    ...

    public void setMyParam(String myParam) {
        this.myParam = myParam;
    }

    ...


}

您可以像这样配置所有属性,或者只提供属性文件的名称或路径。