限制Jetty和Solr的IP地址



我正在使用Jetty设置Solr。我想将访问权限限制在少数几个IP地址。使用Jetty似乎并不能立即明显地做到这一点。有可能吗?如果有,怎么做?

Solr 4.2.1使用Jetty 8.1.8。Jetty 8(正如jonas789所指出的)不支持.htaccess。相反,它使用IPAccessHandler,因为它没有很好的文档。为了让它发挥作用,我不得不花了很多时间,所以我在这里发布了一个更新的解决方案。

IPAccessHandler管理黑名单和白名单,接受任意范围的IP,并支持为每个白/黑名单条目附加特定的URI路径。IPAccessHandler还对HandlerWrapper进行了子类化,这一点非常重要。

solr应用程序仍然存在于WebAppContext中(如Lyndsay的解决方案中),但WebAppContext现在由ContextHandler管理,该ContextHandler驻留在占用服务器中第一个处理程序插槽的ContextHandlerCollection中。为了阻止来自错误IP的请求进入应用程序,我们需要将其封装在该路径上的某个IPAccessHandler中。如果IPAccessHandler位于错误的位置,它的行为会很奇怪:我尝试在上下文处理程序之前插入它,结果它给了错误的机器403 Forbidden,引发了NullPointerException发脾气,没有其他错误消息,诸如此类。通过在服务器级别包装ContextHandlerCollection本身,我终于让它工作起来了。

转到etc/jetty.xml并滚动到handlers部分。然后将现有的ContextHandlerCollection项包装如下:

<!-- =========================================================== -->
<!-- Set handler Collection Structure                            --> 
<!-- =========================================================== -->
<Set name="handler">
  <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
    <Set name="handlers">
     <Array type="org.eclipse.jetty.server.Handler">
   <Item>
     <!-- here begins the new stuff -->
     <New class="org.eclipse.jetty.server.handler.IPAccessHandler">
       <Call name="addWhite">
         <Arg>xxx.xxx.xxx.xxx</Arg>
       </Call>
       <Set name="handler">
         <!-- here's where you put what was there before: -->
         <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
       </Set>
     </New>
     <!-- here ends the new stuff -->
   </Item>
       <Item>
         <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
       </Item>
       <Item>
         <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"/>
       </Item>
     </Array>
    </Set>
  </New>
</Set>

资源:

  • http://comments.gmane.org/gmane.comp.java.jetty.support/6066
  • http://wiki.eclipse.org/Jetty#Configuration_Reference
  • http://wiki.eclipse.org/Jetty/Reference/jetty.xml_syntax
  • http://download.eclipse.org/jetty/stable-8/apidocs/org/eclipse/jetty/server/handler/IPAccessHandler.html

我找到了解决方案。

首先,提取example/webapps文件夹中solr.war的内容。然后创建一个名为.htaccess的文件,并将其放在example/webapps/solr文件夹(您刚刚提取的文件夹)中,其中包含以下内容:

<Limit>
    satisfy all
    order deny,allow
    deny from all
    allow from xxx.xxx.xxx.xxx
</Limit>

在示例/etc/edit jetty.xml文件并注释掉org.portbay.gety.deployer.WebAppDeployer部分。然后最后在示例/调用上下文中创建一个文件夹(如果还不存在),并向其中添加一个名为solr.xml的文件,其中包含:

<Configure id="solr" class="org.mortbay.jetty.webapp.WebAppContext">
    <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/webapps/solr</Set>
    <Set name="contextPath">/solr</Set>
    <Call name="setSecurityHandler">
        <Arg>
            <New class="org.mortbay.jetty.security.HTAccessHandler">
                <Set name="protegee">
                    <Ref id="solr"/>
                </Set>
            </New>
        </Arg>
    </Call>
</Configure>

然后启动您的新安全解决方案!

最新更新