在Jetty中的web.xml文件中移除过滤器

3
我已经使用Jetty Maven配置了Jetty来运行我的Web应用程序。 Jetty被认为是一种轻量级的开发替代方案,因此它不需要Web.xml中的所有内容。更具体地说,我想删除Web.xml中的过滤器。
我尝试使用overrideDescriptor配置属性,但这只允许我覆盖Web.xml,而不是替换它。因此,过滤器仍然存在。
有什么办法可以在不修改原始Web.xml文件的情况下删除过滤器吗?
3个回答

1

既然没有答案,我就发表一下我的解决方案,虽然不完美。

<!-- Jetty configuration -->
<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.5.v20120716</version>
    <configuration>
        <webApp>
            <descriptor>src/main/webapp/mock-web.xml</descriptor>
            [...]
        </webApp>
        [...]
    </configuration>
</plugin>

这种方法的缺点是您必须维护两个几乎相同的web.xml文件。我还没有找到一种解决方案,可以让我覆盖原始的web.xml文件并删除一个监听器。

1
你可以在override-web.xml中将filter-class替换为PassThroughFilter:
public class PassThroughFilter implements Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {}
}

<filter>
    <filter-name>OriginalFilter</filter-name>
    <filter-class>mypackage.PassThroughFilter</filter-class>        
</filter>

0
一个强大的解决方案是在你的web.xml中使用2个XML实体:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document [
<!ENTITY webEntity1 SYSTEM 'webEntity1.xml'>
<!ENTITY webEntity2 SYSTEM 'webEntity2.xml'>
]>
<web-app>
    &webEntity1;
    &webEntity2;
</web-app>

而且只有一个在custom-web.xml文件中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document [
<!ENTITY webEntity1 SYSTEM 'webEntity1.xml'>
]>
<web-app>
    &webEntity1;
</web-app>

这样,在webEntity1.xml中,您可以声明共享的servlet、过滤器、映射,而在webEntity2.xml中只声明Jetty不想使用的过滤器。

然后,您可以像这样配置Jetty插件:

    <configuration>
        ...
        <webApp>
            ...
            <descriptor>${project.basedir}/src/main/webapp/WEB-INF/custom-web.xml</descriptor>
        </webApp>
        ...
    </configuration>

我刚刚在我的jetty插件维基页面上添加了一个章节。


1
不要这样做!你基本上是利用了一个特定于服务器的安全漏洞,这个漏洞可能在新版本的服务器中得到修复,并且不一定存在于其他服务器中。换句话说,具有这种web.xml的Web应用程序是不可移植的。相关问题报告:https://bugzilla.redhat.com/show_bug.cgi?id=1069911 - BalusC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接