java - HK2 MethodInterceptor 与 Jersey 资源

标签 java jersey aop jersey-2.0 hk2

如何设置 aop MethodInterceptor 来处理 Jersey 资源?

这是我在 this 之后尝试过的文档:

第 1 步 - 拦截服务

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;
    }
}

第 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);
            }
        });
    }
}

但是这似乎不起作用,因为我的资源方法都没有被拦截。这可能是因为我对所有资源都使用了 @ManagedAsync 吗?有什么想法吗?

此外,请不要建议 ContainerRequestFilter。参见 this question为什么我不能用一个来处理安全问题。

最佳答案

我认为与其调用 register(MyInterceptionService.class),不如在 configure() 语句中添加:

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

我不确定它是否有效,因为我自己还没有尝试过,所以您的结果可能会有所不同,哈哈

关于java - HK2 MethodInterceptor 与 Jersey 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275562/

相关文章:

java - 处理对 {}->http ://IP:80: Too many open files 的请求时捕获 I/O 异常

java - 从域对象生成 DTO 和映射器

java - java和mysql中的restful webservice

java - LOG4j Spring AOP

java - 使用 OSGi 时是否可以进行字节码操作?

java - 在哪里可以找到官方 Java 编码/样式标准?

java - 如何使 Logback 记录一个空行,而不包括模式字符串?

jquery - ajax 调用不处理 gzipped JSON

java - 为什么 Jersey 1.x 会与 Jersey 2.x 冲突?

java - 在 Java 中使用面向方面的编程和注释标记的方法