简体   繁体   English

HK2 MethodInterceptor与Jersey资源

[英]HK2 MethodInterceptor with Jersey resource

How can I set up an aop MethodInterceptor to work with Jersey resources? 如何设置aop MethodInterceptor以使用Jersey资源?

Here is what I've tried, following this documentation: 按照文档,这是我尝试过的方法:

Step 1 - InterceptionService 步骤1-InterceptionService

public class MyInterceptionService implements InterceptionService
{
    private final Provider<AuthFilter> authFilterProvider;

    @Inject
    public HK2MethodInterceptionService(Provider<AuthFilter> authFilterProvider)
    {
        this.authFilterProvider = authFilterProvider;
    }

    /**
     * Match any class.
     */
    @Override
    public Filter getDescriptorFilter()
    {
        return BuilderHelper.allFilter();
    }

    /**
     * Intercept all Jersey resource methods for security.
     */
    @Override
    @Nullable
    public List<MethodInterceptor> getMethodInterceptors(final Method method)
    {
        // don't intercept methods with PermitAll
        if (method.isAnnotationPresent(PermitAll.class))
        {
            return null;
        }

        return Collections.singletonList(new MethodInterceptor()
        {
            @Override
            public Object invoke(MethodInvocation methodInvocation) throws Throwable
            {
                if (!authFilterProvider.get().isAllowed(method))
                {
                    throw new ForbiddenException();
                }

                return methodInvocation.proceed();
            }
        });
    }

    /**
     * No constructor interception.
     */
    @Override
    @Nullable
    public List<ConstructorInterceptor> getConstructorInterceptors(Constructor<?> constructor)
    {
        return null;
    }
}

Step 2 - Register the service 第2步-注册服务

public class MyResourceConfig extends ResourceConfig
{
    public MyResourceConfig()
    {
        packages("package.with.my.resources");

        // UPDATE: answer is remove this line
        register(MyInterceptionService.class);

        register(new AbstractBinder()
        {
            @Override
            protected void configure()
            {
                bind(AuthFilter.class).to(AuthFilter.class).in(Singleton.class);

                // UPDATE: answer is add the following line
                // bind(MyInterceptionService.class).to(InterceptionService.class).in(Singleton.class);
            }
        });
    }
}

However this doesn't appear to work because none of my resource methods are being intercepted. 但是,这似乎不起作用,因为我的资源方法都没有被拦截。 Could this be because I use @ManagedAsync with all of my resources? 可能是因为我对所有资源都使用了@ManagedAsync吗? Any ideas? 有任何想法吗?

Also, please do not suggest a ContainerRequestFilter . 另外,请不要建议ContainerRequestFilter See this question for why I can't use one to handle security. 有关为什么我不能使用一个人来处理安全性的问题 ,请参见此问题

I think that rather than calling register(MyInterceptionService.class) you might want to instead add into your configure() statement: 我认为您可能希望将其添加到您的configure()语句中,而不是调用register(MyInterceptionService.class):

bind(MyInterceptionService.class).to(InterceptionService.class).in(Singleton.class)

I am not sure it will work as I have not tried it myself so your results may vary lol 我不确定是否会奏效,因为我自己也没有尝试过,因此您的结果可能会有所不同

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

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