2013-07-17 102 views
1

我的Web配置声明端口8080上三个连接器,http和https的端口8081和8082如何限制访问特定端口的某些URL?

在我的servlet我想限制访问某些网址模式,以一个特定的端口,如/ Admin的请求应该被拒绝,除非它在端口8082上。这很简单,我可以检查servlet服务方法中的端口号。

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

一种方法我能想到的是附加属性在server.xml中增加了连接器和访问它在servlet。这可能吗?

为了详细说明,我想在我的servlet中加入类似下面的server.xml

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

然后以某种方式得到纲领性这个如下。我知道getConnectorProperties不存在,这只是一个例子。

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

有关我如何解决这个问题的其他建议?

回答

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> 

注意,这是一个简约例子。

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

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

+0

背后发生了什么?我们在这种情况下创建了两个实例吗? –

+1

实例是什么?仍然只有一个过程。但是两个...服务:)。服务将连接器绑定到引擎。你可以进一步阅读[他们的文档](https://tomcat.apache.org/tomcat-7.0-doc/config/service.html) – yair

相关问题