2012-06-01 13 views
7

यहाँ एक काम web.xml है अंदर <फिल्टर-मानचित्रण> साथ काम नहीं करता:बिलाव का उपयोग करना, @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> तत्वों को हटाने और बजाय निम्नलिखित एनोटेशन का उपयोग:

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

मैं पीछा किया:

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

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

फिर बिलाव 7.0.14 मुझे निम्न त्रुटि देता है this question का उत्तर, लेकिन यह मेरे लिए काम नहीं करता है।

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

संपादित करें:

यहाँ मेरी वेब अनुप्रयोग की निर्भरता हैं मैं केवल मुद्दा है जब बिलाव (7.0.14) का उपयोग कर। ग्लासफ़िश ठीक है।

+0

क्या आप टॉमकैट 7 के अद्यतित संस्करण के साथ प्रयास कर सकते हैं? वर्तमान संस्करण 7.0.27 है। –

+0

मैंने ऐसा किया, वही मुद्दा। ;) – AndrewBourgeois

उत्तर

11

यह बिलाव 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 का उपयोग करने के रूप में के बजाय, या वैसे भी <filter> द्वारा फिल्टर रजिस्टर करने के लिए है में।

+0

मैं तत्वों का उपयोग कर ठीक हूं क्योंकि मैं इसे अपने स्वयं के सीखने के लिए कर रहा हूं। ऐसा क्यों है कि यह मुद्दा अभी केवल तब्दील हो गया था? सर्वलेट 3.0 थोड़ी देर के लिए बाहर हो गया है, टॉमकैट 7 भी, और यह एक बहुत ही बुनियादी विशेषता है ... (मैंने केवल सर्लेट सीखना शुरू कर दिया है)। क्या लोगों ने कुछ और के लिए फ़िल्टर छोड़ दिया ...? – AndrewBourgeois

+0

कोई विचार नहीं। शायद क्योंकि लोगों को इसके बारे में पता नहीं था। या शायद क्योंकि आदेश उनके लिए कोई फर्क नहीं पड़ता। या हो सकता है क्योंकि उन्होंने टॉमकैट का उपयोग अपने गैर-छोटे वेबपैस के लिए कई फ़िल्टरों के साथ नहीं किया था, जिसके लिए ऑर्डरिंग मायने रखती है। या शायद क्योंकि उन्हें प्रयास रिपोर्टिंग के लायक नहीं मिला। आदि कौन जानता है। – BalusC

+2

एफवाईआई: [निश्चित] (https://issues.apache.org/bugzilla/show_bug.cgi?id=53354#c1) [मार्क] द्वारा किया गया है (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

मैं केवल अपने वेब.एक्सएमएल से तत्वों को हटा रहा हूं, न कि तत्व। जैसा कि निम्नलिखित प्रश्न में उत्तर दिया गया है, इसे काम करना चाहिए: http://stackoverflow.com/questions/6560969/how-to-define-servlet-filter-order-of-execution-using-annotations। यह केवल तब काम करता है जब मैं इसे ग्लासफ़िश वातावरण में तैनात करता हूं। – AndrewBourgeois