Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey ContainerRequestFilter not triggered

I'm trying to use a ContainerRequestFilter to enforce some authentication on a Tomcat based Jersey application. I followed this document. Problem : the filter is never triggered

The filter class :

@Provider
public class AuthFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext)
        throws IOException {

        // MY AUTHENTICATION CODE GOES HERE

    }

The web.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="debate-rest"
    version="3.0">
  <display-name>rest</display-name>
   <servlet>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.hck.debate.rest.security.AuthFilter</param-value>
    </init-param>
    <init-param>  
      <param-name>jersey.config.server.provider.packages</param-name>  
      <param-value>com.hck.debate.rest.controller</param-value>  
    </init-param>  
    <init-param>  
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>  
        <param-value>true</param-value>  
    </init-param>      
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <url-pattern>/*</url-pattern>  
  </servlet-mapping>
like image 414
koyaga Avatar asked Jun 25 '13 14:06

koyaga


5 Answers

Okay, I didn't get that the jersey.config.server.provider.packages init param needs to reference not only service classes (API endpoints) but ALL the classes including filters.

Now it works :

<init-param>  
  <param-name>jersey.config.server.provider.packages</param-name>  
  <param-value>com.hck.debate.rest.controller;com.hck.debate.rest.security</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>com.hck.debate.rest.security.AuthFilter</param-value>
</init-param>
like image 162
koyaga Avatar answered Oct 13 '22 10:10

koyaga


I also had to add the @Provider JAX-RS annotation to my filters.
This makes the filter discoverable during JAX-RS scanning phase.

@Provider
public class MyAppFilter implements ContainerRequestFilter {
    // filter logic
}
like image 42
uris Avatar answered Oct 13 '22 12:10

uris


Some hints:

  1. Make sure you're using JAX-RS version 2.17.
  2. Make sure you're using the right imports in your filter:

    • import javax.ws.rs.container.ContainerRequestContext;
    • import javax.ws.rs.container.ContainerRequestFilter;
  3. Add the @Provider annotation

like image 35
ACV Avatar answered Oct 13 '22 10:10

ACV


We were missing the below call in our ResourceConfig implementation class:

register(CorrelationIdFilter.class);
like image 20
AshishKumar Avatar answered Oct 13 '22 12:10

AshishKumar


The minimum requirements to work filters with jersey:

  • add @Provider annotation to filter class
  • the namespace of filter class has to be included in 'jersey.config.server.provider.packages' init-param

Any other settings aren't required (e.g. 'com.sun.jersey.spi.container.ContainerRequestFilters' init-param or ResourceConfig)

like image 2
crazyman Avatar answered Oct 13 '22 11:10

crazyman