Setting dirAllowed to false in Jetty 8.1.12

吃可爱长大的小学妹 提交于 2019-12-23 01:19:17

问题


I was using Jetty 6.x where we created a spring based Jetty server with dirAllowed set to false. The config is as follows.

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

Now I am upgrading to Jetty 8.1.12 and found that initParams is not available for org.eclipse.jetty.webapp.WebAppContext.Now present config is below (with dirAllowed commented out)

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

Can someone tell me how to set dirAllowed property for Jetty 8.1.12

I saw few posts for code based servers like this

But my server is spring based. How do I set with spring based configuration.


回答1:


The upgrade from Jetty 6 to Jetty 8 requires you to update your Jetty references.

To start with, you'll need to update all of your named classes. The project moved to the Eclipse Foundation 6 years ago, this resulted in a mandatory package name change from org.mortbay.jetty to org.eclipse.jetty

Then you'll want to update the various setters to be relevant to what you are attempting to do.

Would recommend that you grab a copy of the Jetty Distribution tarball (or zip) and check out the Jetty XML files that it comes with for some inspiration, while also referencing the Jetty 8 Javadocs for some details.

Note: Jetty 6 was EOL'd in 2010. Jetty 8 is EOL at the end of 2014, there will be no more updates to Jetty 8 after this year. Would highly encourage that you upgrade all the way to Jetty 9 now.




回答2:


An temp workaround should be create a custom WebAppContext, it's not grace but works.

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());
    }
}}

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


来源:https://stackoverflow.com/questions/26845808/setting-dirallowed-to-false-in-jetty-8-1-12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!