简体   繁体   English

JAX-RS - 创建扩展ContainerRequestFilter的过滤器

[英]JAX-RS - Creating filter extending ContainerRequestFilter

I have an issue with my TomEE Plume 7.0.2. 我的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 : 这是我的Rest应用程序:

    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. 此外,您可能需要使用org.glassfish.jersey.servlet.ServletContainer作为web.xml中的servlet容器,用于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) 它取决于你的配置(我认为是openejb-jar.xml和系统属性),因为它应该开箱即用( https://github.com/apache/tomee/blob/41cb392c6e3dc63d6792eae88e90c33743255212/server/openejb-cxf-rs/src /test/java/org/apache/openejb/server/cxf/rs/CDIProviderContainerRequestFilterTest.java不做任何事情)

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. 另一种方法是在Rahul的答案中注册类中的过滤器(但是请不要做任何与球衣相关的事情,因为TomEE不使用球衣它会失败而只是添加一团糟)或者只是在openejb-jar.xml中配置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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