3

I have an .ear with a war webapplication which is composed of n web-fragment jars. I somehow can't get my servlet filters to call doFilter or even the init methods when mapping the filter with an url-pattern. I am using a shiro-filter which is mapped to the root of the application and filters every request. This filter actually works and the code of the shiro-filter gets executed:

  <filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>

When I try to register following filters in another web-fragment.xml and call localhost:8080/application-name/myservice/getMyData it just ignores the url-pattern supplied:

<?xml version="1.0" encoding="UTF-8"?>
<web-fragment metadata-complete="true" version="3.0"
 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-fragment_3_0.xsd">
 <display-name>My Web Application</display-name>
 <servlet>
  <servlet-name>MyService</servlet-name>
  <servlet-class>at.mycompany.product.service.impl.MyServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>MyService</servlet-name>
  <url-pattern>myservice/*</url-pattern>
 </servlet-mapping>
 <servlet>
  <servlet-name>GetToPostServlet</servlet-name>
  <servlet-class>at.mycompany.core.common.service.GetToPostServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>GetToPostServlet</servlet-name>
  <url-pattern>myservice/getToPostServlet</url-pattern>
 </servlet-mapping>

 <filter>
  <filter-name>HttpGetToPostFilter</filter-name>
  <filter-class>at.mycompany.core.common.service.HttpGetToPostFilter</filter-class>
  <init-param>
   <param-name>at.mycompany.core.common.service.utils.HTTP_GET_TO_EMPTY_SOAP</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>HttpGetToPostFilter</filter-name>
  <url-pattern>myservice/getMyData/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
 </filter-mapping>
 
 <!-- ServiceInfoFilter: ServiceInfo speichern -->
 <filter>
  <filter-name>ServiceInfoFilter</filter-name>
  <filter-class>at.mycompany.shared.util.info.ServiceInfoFilter</filter-class>
  <init-param>
   <param-name>ServiceInfoFilter</param-name>
   <param-value>ServiceInfoFilter</param-value>
  </init-param>
 </filter>

 <filter-mapping>
  <filter-name>ServiceInfoFilter</filter-name>
  <url-pattern>myservice/*</url-pattern>
 </filter-mapping>

 <filter>
  <filter-name>NoCacheFilter</filter-name>
  <filter-class>at.mycompany.core.common.service.NoCacheFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>NoCacheFilter</filter-name>
  <url-pattern>myservice/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
 </filter-mapping>


 <security-constraint>
  <web-resource-collection>
   <web-resource-name>The unprotected myservice servlet</web-resource-name>
   <url-pattern>/myservice</url-pattern>
  </web-resource-collection>
 </security-constraint>
</web-fragment>

None of the above filter trigger... I also tried different url-patterns like:

<url-pattern>/myservice/getMyData</url-pattern>
<url-pattern>myservice/getMyData/</url-pattern>
<url-pattern>myservice/getMyData/*</url-pattern>

What am I doing wrong? There is no evidence that url-patterns do not work with web-fragments in the specification: http://download.oracle.com/otn-pub/jcp/servlet-3_1-fr-eval-spec/servlet-3_1-final.pdf?AuthParam=1539621798_87c76bcc77456ce463d523d10f4ac5e1

I know that I can also bind the filter by servlet-mapping, but I really do not want to filter all requests from MyService with GetToPostFilter only those requests on .../getMyData

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Manuel Waltschek
  • 515
  • 1
  • 6
  • 23
  • Does it work when you remove the ShiroFilter? Just wondering if it's conflicting in some way. – JGlass Oct 15 '18 at 18:32
  • No it does not affect the other filters. I also realized that my filters do not work even with the binding... – Manuel Waltschek Oct 16 '18 at 09:17
  • 1
    Are you adhering to the rules in the answer of this SO Q&A? [missing elements from web fragment xml in the effective web xml](https://stackoverflow.com/questions/10993236/missing-elements-from-web-fragment-xml-in-the-effective-web-xml) disregard the "missing" part of the question and focus on the rules in the answer as the answer author didnt have a problem it appears. – JGlass Oct 16 '18 at 13:28
  • Yes all of these rules apply and as the author's interceptorfilter my filter also fires with an url mapping of "/*" – Manuel Waltschek Oct 16 '18 at 14:01
  • So this is being done: 1. It must be named web-fragment.xml 2. It must be placed in a JAR file inside WEB-INF/lib 3. The web-fragment.xml must be present inside the META-INF directory of the JAR file. and in web.xml `metadata-complete="true"` if it exists is set to false? Also, what app container is this running in, e.g jboss, Glassfish, web sphere or tomcat? – JGlass Oct 16 '18 at 14:06
  • Yes, all of these rules apply. The application is running on wildfly10. – Manuel Waltschek Oct 16 '18 at 14:25
  • 1
    See if this helps as you have a lot of `/service/*` and `myservice/*` and `myservice/getToPostServlet` as well as `myservice/*`, e.g. some start with "/" and a lot end with "/*" if it helps get you straightened out and working, I'll reference it and add it as an answer since it was a little difficult to find! [Web.xml filter-mapping breaks servlet mapping](https://developer.jboss.org/thread/267316) - ignore the web.xml part - I'd pay more attention to the specifications. – JGlass Oct 16 '18 at 15:11

0 Answers0