为什么我的tomcat阀门没有被调用? [英] Why is my tomcat valve not being invoked?

查看:128
本文介绍了为什么我的tomcat阀门没有被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现一个Tomcat阀(当前使用7.0.55),该阀应拦截到达Tomcat服务的每个请求,而不管连接器和其他原因如何,无论是否存在具有匹配名称的主机或Servlet上下文等等.

I try to implement a Tomcat valve (currently using 7.0.55) that should intercept every request that reaches the Tomcat serivce, regardless of Connector and whatnot, regardless of wether there's a Host with matching name or a servlet context or whatever.

阀门的invoke方法如下所示:

public class MyValve extends ValveBase {
    public void invoke(Request request, Response response) throws IOException,
            ServletException {
        LOG.trace("Valve is being invoked");
        getNext().invoke(request, response);
    }
}

在开发系统上,在本地进行测试,一切正常.对我的"localhost" tomcat上任何URI路径的请求均写入该日志行.在sever.xml中,阀配置在任何Host元素之外:

On the dev system, testing locally, everything's working as excepted. A request to any URI path on my "localhost" tomcat writes that log line. In the sever.xml, the valve is configured outside of any Host element:

<Server port="8005" shutdown="SHUTDOWN">
  ...
  <Service name="Catalina">
    ...
    <Engine defaultHost="localhost" name="Catalina">
      ...
      <Realm ... />
      <Valve className="a.b.c.MyValve" />
      ...
      <Host ...>
      </Host>
    </Engine>
  </Service>
</Server>

现在在系统的主机文件中说,域test.domain.com映射到127.0.0.1,并且部署了一个名为some-webapp的上下文.

Now say in my system's hosts file, the domain test.domain.com is mapped to 127.0.0.1, and there's one context deployed named some-webapp.

如上所述,当我调用http://localhost:8080/some-webapp/时,日志行将被打印,这与预期的一样,而当我调用http://localhost:8080/non-existing-webapp/时,日志行也将被打印. 对于域(在server.xml中未配置)test.domain.com也是如此,因此http://test.domain.com/some-webapp/打印日志行以及http://test.domain.com/non-existing-webapp.

As said above the log line gets printet when I call http://localhost:8080/some-webapp/, which is as expected, and it gets also printed when I call http://localhost:8080/non-existing-webapp/, which is also as expected.
The same goes for the domain (that is not configured in server.xml) test.domain.com, thus http://test.domain.com/some-webapp/ prints the log line as well as http://test.domain.com/non-existing-webapp.

但是对于我们正在测试的服务器而言,情况并非如此.在此,只有在tomcat知道URI的上下文名称的情况下才调用Valve,即,对.../some-webapp/的调用将打印日志行,而对.../non-existing-webapp的调用/根本不执行任何操作-根本不调用该阀门.
尽管如此,tomcat仍会处理该请求,因为在这种情况下,发送到客户端的404包含"Apache Coyote something"作为响应标头.

But this is not true for the server we're testing on. Here the Valve is only invoked if the context name of the URI is "known" to tomcat, i.e. a call to .../some-webapp/ would print the log line while a call to .../non-existing-webapp/ would simply do nothing - the valve is not invoked at all.
Still, tomcat handles that request as the 404 that gets sent to the client in this case contains "Apache-Coyote something" as a response header.

我不知道如何进一步调试它,尤其是tomcat对管道的选择"过程或其他任何想法-

I'm out of ideas of how to debug this further, especially the process of tomcat's "selection" of a Pipeline or whatever - any thoughts out there?

谢谢!

推荐答案

原来是由Tomcat的webapps-dir中缺少ROOT目录引起的.我认为Tomcat确实会在很早的时间点严格过滤掉传入的请求,甚至在任何阀门都无法处理并弄乱请求之前.
如果没有默认上下文(即没有ROOT-dir),则Tomcat(认为)知道对non-existing-webapp的请求无法成功,因此甚至不调用阀.在默认上下文中,Tomcat无法知道请求将会发生什么,因此阀门有机会拦截请求.

Turns out it is caused by a missing ROOT-directory in Tomcat's webapps-dir. I think that Tomcat does filter incoming requests rather rigorously at a very early point in time, even before any valves can handle and mess with the request.
And if there's no default context (i.e. no ROOT-dir) then Tomcat (thinks to) know(s) that a request to non-existing-webapp cannot succeed and thus does not even call the valve(s). With a default context Tomcat cannot know what will happen to the request and thus the valve gets its chance to intercept the request.

这篇关于为什么我的tomcat阀门没有被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆