小编典典

使用Tomcat,@ WebFilter不适用于 在web.xml中

tomcat

这是一个正常工作的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

     <filter>
        <filter-name>rememberMeCookieFilter</filter-name>
        <filter-class>be.example.fun.jsp.filters.RememberMeCookieFilter</filter-class>
    </filter>

    <filter>
        <filter-name>mustBeSignedInFilter</filter-name>
        <filter-class>be.example.fun.jsp.filters.MustBeSignedInFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>rememberMeCookieFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>mustBeSignedInFilter</filter-name>
        <url-pattern>/private/*</url-pattern>
    </filter-mapping>
</web-app>

当我删除<filter>元素并使用以下注释时:

@WebFilter(filterName="rememberMeCookieFilter")
public class RememberMeCookieFilter implements Filter

@WebFilter(filterName="mustBeSignedInFilter")
public class MustBeSignedInFilter implements Filter

然后,Tomcat 7.0.14给我以下错误:

java.lang.IllegalArgumentException: Filter mapping must specify either a <url-pattern> or a <servlet-name>
    at org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:2956)
    at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:2915)
    at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1180)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1270)
        ...

我遵循了[这个问题](http://codingdict.com/questions/115618的答案,但这对我不起作用。

这是我的Web应用程序的依赖项:

<dependencies>
        <!-- SLF4J (+ LOGBack) for logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>1.8.3</version>
        </dependency>

        <!-- The servlet API that I installed in my local repo -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0</version>
            <type>jar</type>
            <scope>provided</scope>
            <!--optional>false</optional-->
        </dependency>

        <!-- JUnit for testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

编辑:我只有在使用Tomcat(7.0.14)时才有问题。玻璃鱼很好。


阅读 391

收藏
2020-06-16

共1个答案

小编典典

这是Tomcat7中的错误。我将其报告为问题53354

由于无法在中指定调用顺序@WebFilter,因此用户被迫<filter- mapping>在web.xml中明确指定。可以与@WebFilter(filterName)Glassfish和JBoss AS
中的结合使用,如下所示:

@WebFilter(filterName="filter1")
public class Filter1 implements Filter {}

@WebFilter(filterName="filter2")
public class Filter2 implements Filter {}

<filter-mapping>
    <filter-name>filter1</filter-name>
    <url-pattern>/url1/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>filter2</filter-name>
    <url-pattern>/url2/*</url-pattern>
</filter-mapping>

然而,它在Tomcat的7.0.27失败,出现以下异常混乱(的<url-pattern> 被设定)

Caused by: java.lang.IllegalArgumentException: Filter mapping must

specify either a or a
at
org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:3009)
at
org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:2968)
at
org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1207)
at
org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1294)
at
org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:855)
at
org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:345)
at
org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at
org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
at
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
… 7 more

同时,最好的选择是使用Glassfish或JBoss AS,或者<filter>无论如何都要注册过滤器。

2020-06-16