限制 Jetty 和 Solr 的 IP 地址

发布于 2024-12-28 05:16:36 字数 83 浏览 5 评论 0原文

我正在使用 Jetty 设置 Solr。我想限制仅访问少数几个 IP 地址。使用 Jetty 来完成此操作似乎并不明显。可能吗?如果可能的话,如何实现?

I'm setting up Solr using Jetty. I would like to restrict access to only a few IP addresses. It doesn't seem immediately obvious that this can be done using Jetty. Is it possible and if so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○愚か者の日 2025-01-04 05:16:36

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 并滚动到处理程序部分。然后包装现有的 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>

资源:

Solr 4.2.1 uses Jetty 8.1.8. Jetty 8 (as noted by jonas789) doesn't support .htaccess. Instead, it uses IPAccessHandler, which doesn't have great documentation available. I had to play with it quite a bit to get it work, so I'm posting an updated solution here.

IPAccessHandler manages a blacklist and a whitelist, accepts arbitrary ranges of IPs, and supports attaching specific URI paths to each white/black -list entry. IPAccessHandler also subclasses HandlerWrapper, which turns out to be important.

The solr app still lives in a WebAppContext (as in Lyndsay's solution), but a WebAppContext is now governed by a ContextHandler, which resides in a ContextHandlerCollection occupying the first handler slot in the server. To stop requests from the wrong IP from getting to the app, we need to wrap it inside an IPAccessHandler somewhere along that path. IPAccessHandler behaves oddly if it's in the wrong spot: I tried inserting it before the context handlers and it gave 403 Forbidden to the wrong machines, threw NullPointerException tantrums with no additional error messages, all sorts of nonsense. I finally got it to work by wrapping the ContextHandlerCollection itself, at the server level.

Go to etc/jetty.xml and scroll to the handlers section. Then wrap the existing ContextHandlerCollection item as follows:

<!-- =========================================================== -->
<!-- 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>

Resources:

站稳脚跟 2025-01-04 05:16:36

我找到了解决方案。

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

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

在 example/etc/ 中,编辑 jetty.xml 文件并注释掉 org.mortbay.jetty。部署者.WebAppDeployer 部分。然后最后在 example/ 中创建一个名为 contexts 的文件夹(如果尚不存在),并向其中添加一个名为 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>

然后启动新的安全 solr!

I found the solution.

Firstly, extract the contents of solr.war in the example/webapps folder.
Then create a file called .htaccess and place it in the example/webapps/solr folder (the one you just extracted) containing the following:

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

In example/etc/ edit the jetty.xml file and comment out the org.mortbay.jetty.deployer.WebAppDeployer part. Then finally create a folder in example/ called contexts (if one does not yet exist) and add a file called solr.xml to it containing:

<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>

Then start up your new secure solr!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文