DontTurnAround
DontTurnAround

Reputation: 684

RESTeasy ContainerRequestFilter is never being called

I'm trying to implement an authentication filter with RESTeasy 3.0.17FINAL in Tomcat 8. Here I have my web.xml entries.

<!-- RESTEasy Config -->
<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
</listener>
<servlet>
    <servlet-name>RESTEasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>RESTEasy</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

Here I have my test API

@Path("/test")
public class TestAPI extends APIServlet {

    @GET
    @Path("/{id}")
    @AllowUsers
    @Produces(MediaType.APPLICATION_JSON)
    public Response printMessage(@PathParam("id") int id) {
        JsonObject json = new JsonObject();
        json.addProperty("status", "awesome");
        json.addProperty("yeah", "It worked");
        return Response.ok(json).build();
    }
}

And here is my temporary filter:

@Provider
public class AuthFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        if(Math.random() < 0.5) {
            containerRequestContext.abortWith(Response.status(403).build());
        }
        System.out.println("Filtered");
    }
}

My filter is never being called. The println is never reached, I can't successfully place breakpoints in the filter. There are no error messages in the cataline or localhost logs. I have tried using name-binding annotations as such:

@NameBinding
@Inherited
@Target({ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Secured {
}

With @Secured right above @Provider on AuthFilter and above @GET on printMessage. That doesn't work either.

I have tried explicitly listing the filter in web.xml

<context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>com.myPackage.mvc.servlet.API.AuthFilter</param-value>
</context-param>

Why is my filter never being called? I have two other classes marked @Provider and they are being recognized by the system and I can place breakpoints in them and they are working as expected.

Upvotes: 1

Views: 2145

Answers (3)

wuzhiwei in hujiang
wuzhiwei in hujiang

Reputation: 11

Add a Provider file under your META-INF/services, like: /META-INF/services/javax.ws.rs.ext.Providers.

javax.ws.rs.ext.Providers is a file, the content is your path of filter like:

com.hujiang.foe.instalment.liquidation.biz.impl.account.filter.ContextCleanFilter

Upvotes: 1

DontTurnAround
DontTurnAround

Reputation: 684

I switched to Jersey instead of RESTeasy and everything just worked.

Upvotes: 0

taoxiaopang
taoxiaopang

Reputation: 417

Add @PreMatching or @PostMatching to your ContainerRequestFilter

Upvotes: 0

Related Questions