简体   繁体   中英

JAX-RS - Creating filter extending ContainerRequestFilter

I have an issue with my TomEE Plume 7.0.2.

I have created a filter that way :

package com.gfp.rest.providers;

import java.io.IOException;

import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;

@PreMatching
@Provider
@Priority(value = 1)
public class AuthenticationFilter implements ContainerRequestFilter
{
    public AuthenticationFilter()
    {
        System.out.println("AuthenticationFilter.AuthenticationFilter()");
    }

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException
    {
        System.out.println("AuthenticationFilter.filter()");
        String token = requestContext.getHeaderString("token");

        ResponseBuilder responseBuilder = null;
        Response response = null;

        // check if token is empty
        if (token.isEmpty()) {
            responseBuilder = Response.serverError();
            response = responseBuilder.status(Status.UNAUTHORIZED).build();
            requestContext.abortWith(response);
        }
    }
}

Here is my Rest Application :

    package com.gfp.rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("api/v1")
public class RestApplication extends Application
{
}

Problem is : Filter does not seem to get called. I have read lots of documentation and many other posts, I can not find any solution. Is there something special that I have missed ?

Thanks a lot !

Try this

package com.gfp.rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("api/v1")
public class RestApplication extends Application
{
public Set<Class<?>> getClasses() {
        return getRestClasses();
    }

    private Set<Class<?>> getRestClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();        
        resources.add(AuthenticationFilter.class);
        return resources;    
    }
}

Also you might need to use org.glassfish.jersey.servlet.ServletContainer as the servlet container in web.xml, for Jersey 2.x.

it depends your configuration (openejb-jar.xml and system properties I think) since it should work out of the box ( https://github.com/apache/tomee/blob/41cb392c6e3dc63d6792eae88e90c33743255212/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIProviderContainerRequestFilterTest.java does nothing more)

Alternative is to register the filter in the classes as in Rahul's answer (but please don't do anything jersey related since TomEE doesn't use jersey it will fail and just add a mess) or just configure it in openejb-jar.xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM