Tomcat:限制/仅一个/ webapp 访问本地主机
我正在运行 Tomcat 6 来为多个 Web 应用程序提供服务,其中大多数是面向公众的。但我想限制对仅一个网络应用程序的访问,仅允许来自本地主机的连接。
我可以使用 context.xml 中的阀门来限制对所有 Web应用程序的访问,如下所述:
但我不知道如何限制每个应用程序的访问。有没有办法用我的应用程序的 web.xml 来做到这一点?或者通过向 context.xml 添加额外的规则?
谢谢,
-B
回顾解决方案:
$ cp /var/lib/tomcat6/conf/context.xml \
/var/lib/tomcat6/conf/Catalina/localhost/my-app-name.xml
$ cat /var/lib/tomcat6/conf/Catalina/localhost/my-app-name.xml
<Context>
<Valve className="org.apache.catalina.valves.RemoteHostValve" allow="localhost"/>
... {as previously} ...
</Context>
I'm running Tomcat 6 to serve several web apps, most of which are public-facing. But I'd like to restrict access to just one webapp, allowing connections only from localhost.
I can restrict access for all webapps using a valve in context.xml, as described in:
But I can't figure out how to restrict access on a per-app basis. Is there a way to do this with my app's web.xml? Or by adding additional rules to context.xml?
Thanks,
-B
Recapping Solution:
$ cp /var/lib/tomcat6/conf/context.xml \
/var/lib/tomcat6/conf/Catalina/localhost/my-app-name.xml
$ cat /var/lib/tomcat6/conf/Catalina/localhost/my-app-name.xml
<Context>
<Valve className="org.apache.catalina.valves.RemoteHostValve" allow="localhost"/>
... {as previously} ...
</Context>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为您的应用程序创建单独的 context.xml。
这是 Tomcat 文档中有关上下文配置的摘录:
Context 元素可以显式定义:
$CATALINA_HOME/conf/context.xml
文件中:Context 元素信息将由所有 Web 应用程序加载。在 $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default 文件中:Context 元素信息将被该主机的所有 Web 应用程序加载。
$CATALINA_HOME/conf/[enginename]/[hostname]/
目录中的各个文件(具有.xml
扩展名)中。文件的名称(减去.xml
)扩展名将用作上下文路径。可以使用#
定义多级上下文路径,例如foo#bar.xml
用于/foo/bar
的上下文路径。默认 Web 应用程序可以使用名为ROOT.xml
的文件来定义。$CATALINA_HOME/conf/[enginename]/[hostname]/
中不存在应用程序的上下文文件时;在应用程序文件内/META-INF/context.xml
的单个文件中。如果 Web 应用程序打包为 WAR,则/META-INF/context.xml
将被复制到$CATALINA_HOME/conf/[enginename]/[hostname]/
并重命名以匹配应用程序的上下文路径。一旦此文件存在,如果将具有较新/META-INF/context.xml
的新 WAR 放置在主机的 appBase 中,则该文件不会被替换。You can create an individual context.xml for you app.
This is an excerpt from Tomcat doc on context configuraion:
Context elements may be explicitly defined:
$CATALINA_HOME/conf/context.xml
file: the Context element information will be loaded by all webapps.In the
$CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default
file: the Context element information will be loaded by all webapps of that host..xml
extension) in the$CATALINA_HOME/conf/[enginename]/[hostname]/
directory. The name of the file (less the.xml
) extension will be used as the context path. Multi-level context paths may be defined using#
, e.g.foo#bar.xml
for a context path of/foo/bar
. The default web application may be defined by using a file calledROOT.xml
.$CATALINA_HOME/conf/[enginename]/[hostname]/
; in an individual file at/META-INF/context.xml
inside the application files. If the web application is packaged as a WAR then/META-INF/context.xml
will be copied to$CATALINA_HOME/conf/[enginename]/[hostname]/
and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer/META-INF/context.xml
is placed in the host's appBase.允许 localhost 对我来说不起作用。我使用
RemoteAddrValve
代替。请记住,某些系统使用 IPv4 地址(您的过滤器必须匹配127.0.0.1
),而其他系统则使用 IPv6 地址(匹配完整地址,而不是像::1
::1 这样的缩写符号)代码>)。属性
allow
采用正则表达式,因此需要对点进行转义。正如Dmitry Negoda所解释的,这位于/META-INF/context.xml
中。Allowing
localhost
didn't work for me. I useRemoteAddrValve
instead. Keep in mind that some systems use IPv4 addresses (your filter has to match match127.0.0.1
) while others use IPv6 addresses (match the full address, not abbreviated notations like::1
).The attribute
allow
takes a regexp, so dots need to be escaped. As explained by Dmitry Negoda, this goes in/META-INF/context.xml
.转到以下路径:C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf\Catalina\localhost\
在此路径下,您可以找到“ manager.xml ”文件。
编辑“manager.xml”文件,内容如下:
******保存并运行服务器....您知道了。
注意:127.0.0.1 表示您的系统 IP
10.100.1.2 -这是你的朋友
Goto following path: C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf\Catalina\localhost\
Under this path you find " manager.xml " file.
Edit " manager.xml " file,with following content:
****** save and run server....You got it.
NOTE : 127.0.0.1 MEANS YOUR SYSTEM IP
10.100.1.2 -THIS IS YOUR FRIEND