【问题标题】:Setting dirAllowed to false in Jetty 8.1.12在 Jetty 8.1.12 中将 dirAllowed 设置为 false
【发布时间】:2017-07-11 17:10:19
【问题描述】:

我使用的是 Jetty 6.x,我们创建了一个基于 spring 的 Jetty 服务器,并将 dirAllowed 设置为 false。 配置如下。

<bean id="Server" class="org.mortbay.jetty.Server" init-method="start" destroy-method="stop">
    <property name="connectors">
        <list>
            <bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
                <property name="port" value="${tnplportal.jettyServer.httpPort}" />
                <property name="headerBufferSize" value="${tnplportal.jettyServer.headerBufferSize}" />
            </bean>
        </list>
    </property>

    <property name="handler">
        <bean id="handlers" class="org.mortbay.jetty.handler.HandlerCollection">
            <property name="handlers">
                <list>
                    <bean id="contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection">
                        <property name="handlers">
                            <list>
                                <bean class="org.mortbay.jetty.webapp.WebAppContext">
                                        <property name="contextPath" value="/fileServer" />
                                        <property name="resourceBase" value="ResourcePath" />
                                        <property name="initParams">
                                            <map> 
                                               <entry key="org.mortbay.jetty.servlet.Default.dirAllowed" value="false" />
                                            </map>
                                        </property>
                                </bean>

                            </list>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>
    </property>
</bean>

现在我正在升级到 Jetty 8.1.12,发现 initParams 不适用于 org.eclipse.jetty.webapp.WebAppContext.Now 配置如下(dirAllowed 已注释掉)

    <bean id="Server" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
    <property name="connectors">
        <list>
            <bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <property name="port" value="${tnplportal.jettyServer.httpPort}" />
            </bean>
        </list>
    </property>

    <property name="handler">
        <bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
            <property name="handlers">
                <list>
                    <bean id="contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
                        <property name="handlers">
                            <list>
                                <bean class="org.eclipse.jetty.webapp.WebAppContext">
                                    <property name="contextPath" value="/fileServer" />
                                    <property name="resourceBase" value="resourcePath" />
                                    <!-- <property name="initParams">
                                           <map>               
                                            <entry key="org.mortbay.jetty.servlet.Default.dirAllowed" value="false" />
                                           </map>
                                        </property> -->
                                </bean>

                            </list>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>
    </property>
</bean>

谁能告诉我如何为 Jetty 8.1.12 设置 dirAllowed 属性

我看到一些关于基于代码的服务器的帖子,例如 this

但我的服务器是基于弹簧的。如何使用基于弹簧的配置进行设置。

【问题讨论】:

    标签: java spring spring-mvc jetty


    【解决方案1】:

    从 Jetty 6 升级到 Jetty 8 需要您更新您的 Jetty 引用。

    首先,您需要更新所有您的命名类。该项目在 6 年前转移到 Eclipse 基金会,这导致包名从 org.mortbay.jetty 强制更改为 org.eclipse.jetty

    然后您需要更新各种设置器以与您尝试执行的操作相关。

    建议您获取Jetty Distribution tarball (or zip) 的副本并查看它附带的 Jetty XML 文件以获得一些灵感,同时也参考Jetty 8 Javadocs 了解一些详细信息。

    注意:Jetty 6 已于 2010 年停产。Jetty 8 is EOL at the end of 2014,今年之后 Jetty 8 将不再有更新。强烈建议您现在一直升级到 Jetty 9。

    【讨论】:

    • 如果您看到我附加的代码 sn-p,我已经完成了升级到 Jetty 8 的所有必要更改,并且可以毫无问题地启动应用程序。这里唯一的问题是设置 dirAllowed(以避免从浏览器中列出文件)。我不确定如何在 Jetty 8 中设置它。
    【解决方案2】:

    临时解决方法应该是创建一个自定义 WebAppContext,它不是优雅但有效。

    public class CustomWebAppContext  extends org.eclipse.jetty.webapp.WebAppContext{
      public void setInitParams(Map<String, String> values){
        Map<String, String> currectParams= getInitParams();
        if(currectParams==null){
            currectParams= new HashMap<String, String>();
        }
        for(Map.Entry<String,String> entry : values.entrySet()){
            currectParams.put(entry.getKey(), entry.getValue());
        }
    }}
    

    然后在 xml 中:

          <bean class="CustomWebAppContext">
                                        <property name="contextPath" value="/fileServer" />
                                        <property name="resourceBase" value="ResourcePath" />
                                        <property name="initParams">
                                            <map> 
                                               <entry key="org.mortbay.jetty.servlet.Default.dirAllowed" value="false" />
                                            </map>
                                        </property>
                                </bean>
    

    【讨论】:

      猜你喜欢
      • 2011-08-12
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多