1

我的 Web 配置声明了三个连接器,端口 8080 上的 http 和端口 8081 和 8082 上的 https。

在我的 servlet 中,我想将对某些 url 模式的访问限制到特定端口,例如,除非它在端口 8082 上,否则应该拒绝对 /Admin 的请求。这很简单,我可以在服务方法中检查端口号小服务程序。

但我还需要能够允许客户更改端口。如果客户希望仅在端口 9000(而不是 8083)上允许管理请求,则此策略失败。

我能想到的一种方法是在 server.xml 中向连接器添加一个附加属性,并在 servlet 中访问它。这可能吗?

为了详细说明,我想在server.xml

<Connector port="9000" connectorType="admin"....

然后以某种方式在我的 servlet 中对此进行编程,如下所示。我意识到getConnectorPropertiesis 不存在,这只是一个例子。

 if (request.getRequestURL().startsWith("/admin")) {
   String connectorType = request.getConnectionProperties().get("connectorType");
   if (! "admin".equals(connectorType)) {
     // return unauthorized

关于如何解决这个问题的任何其他建议?

4

1 回答 1

1

似乎您正在为不同的端口使用不同的上下文根 (=apps)。这不应该以编程方式完成。接受不同端口或协议的应用程序配置server.xml有不同的Service组件:

<Server>
    <!-- Define one Service for the open app -->
    <Service name="myOpenApp">
        <Connector port="8080"/>
        <Engine name="myOpenApp" defaultHost="localhost">
            <Host name="localhost"> <!-- default appBase is webapps -->
                <Context docBase="path/to/my/open/app"/>
                <!-- docBase is relative to appBase but may also refer an absolute path -->
            </Host>
        </Engine>
    </Service>

    <!-- and another for the restricted -->
    <Service name="onlyForAdmins">
        <Connector port="8081" SSLEnabled="true" scheme="https"/>
        <Connector port="8082" SSLEnabled="true" scheme="https"/>
        <Engine name="onlyForAdmins" defaultHost="localhost">
            <Host name="localhost"> <!-- default appBase is webapps -->
                <Context docBase="path/to/admins/only"/>
                <!-- docBase is relative to appBase but may also refer an absolute path -->
            </Host>
        </Engine>
    </Service>
</Server>

请注意,这是一个简约的示例。

如果您需要更复杂web.xml的 URL 模式,您可以使用应用程序(servlet-mappings等等)。

基本上,这不是授权错误......这是一个未映射的 URL。您的应用程序只是不支持非 SSL 端口上的管理资源。所以你会得到404 page not found.

于 2013-07-18T01:02:36.390 回答