2012-06-01 11 views
7

のweb.xml内の<のfilter-mapping>では動作しません:Tomcatを使用して、@WebFilterは、ここでの作業の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) 
     ... 

私は続きます回答はthis questionですが、それは私のためには機能しません。ここで

は、私の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> 

EDIT:Tomcatの(7.0.14)を使用しているとき、私は唯一の問題を持っています。グラスフィッシュは大丈夫です。

+0

Tomcat 7の最新バージョンを試すことができますか?現在のバージョンは7.0.27です。 –

+0

私は同じことをしました。 ;) – AndrewBourgeois

答えて

11

これはTomcat 7のバグです。issue 53354と報告しました。ところで

As it's not possible to specify the invocation order in a @WebFilter , users are forced to explicitly specify <filter-mapping> in web.xml. This works in combination with a @WebFilter(filterName) in Glassfish and JBoss AS as follows:

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

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

with

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

However it fails in Tomcat 7.0.27 with the following confusing exception (the <url-pattern>is been set)

Caused by: java.lang.IllegalArgumentException: Filter mapping must specify either a <url-pattern> or a <servlet-name> 
    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>でフィルタを登録することです。

+0

要素を使用しても問題ありません。自分の学習のためにこれをやっています。なぜ今のところこの問題が解消されただけですか?サーブレット3.0はしばらくの間、Tomcat 7も出てきましたが、これは非常に基本的な機能です(サーブレットの学習を始めたばかりです)。人々はフィルタを他の何かのために放棄したのですか? – AndrewBourgeois

+0

いいえ、考えられません。たぶん人々はこれを知らなかったからです。または注文が問題ではなかったからかもしれません。あるいは、彼らは、順序が重要な複数のフィルタを持つ、あまりにも小さいWebアプリケーションでTomcatを使用しなかったかもしれないからです。あるいは、努力報告の価値がないと思ったからかもしれません。等誰​​が知っている。 – BalusC

+2

FYI:[固定](https://issues.apache.org/bugzilla/show_bug.cgi?id=53354#c1)[Mark](http://stackoverflow.com/users/1299005/mark-トーマス)。 7.0.28以降になります。 – BalusC

2

サーブレットフィルタの対象を指定する必要があります。 'servletNames'または 'urlPatterns'の値を指定してください。

http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebFilter.html

例えば

@WebFilter(filterName="mustBeSignedInFilter", urlPatterns={ "/signed/in/path/*" }) 
public class MustBeSignedInFilter implements Filter 
+0

要素ではなく、web.xmlから要素のみを削除します。次の質問で答えられるように、それは動作するはずです:http://stackoverflow.com/questions/6560969/how-to-define-servlet-filter-order-of-execution-using-annotations。これはGlassfish環境に展開する場合にのみ機能します。 – AndrewBourgeois