什么是Resteasy 3.X PreProcessInterceptor的正确替代品? [英] What is the proper replacement of the Resteasy 3.X PreProcessInterceptor?

查看:447
本文介绍了什么是Resteasy 3.X PreProcessInterceptor的正确替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用本教程中描述的身份验证/授权机制构建休息服务: http://howtodoinjava.com/2013/06/26/jax-rs-resteasy-basic-authentication-and-authorization-tutorial/

I'm building rest service using an authentication/authorization mechanism as described in this tutorial: http://howtodoinjava.com/2013/06/26/jax-rs-resteasy-basic-authentication-and-authorization-tutorial/

基本上它使用 PreProcessInterceptor 接口,用于扫描注释的目标方法(来自 javax.annotation.security 包),描述了访问该方法所需的角色。由于此处的验证器是拦截器,它可以取消目标方法调用,如果需要,返回401(未授权)。

Basically it uses the PreProcessInterceptor interface to scan the target method for annotations (from javax.annotation.security package) which describe the required roles to access that method. As the the authenticator here is an interceptor, it can cancel the target method invocation, returning a 401 (unauthorized) if needed.

这里的问题是接口组织。 jboss.resteasy.spi.interception.PreProcessInterceptor在当前的RestEasy版本(3.0.1)中已弃用,我在尝试使用标准JAX-RS接口实现相同的行为时遇到了问题。

The problem here is that the interface org.jboss.resteasy.spi.interception.PreProcessInterceptor is deprecated in the current RestEasy version (3.0.1), and I'm having problems trying to implement the same behaviour with the standard JAX-RS interfaces.

我正在使用javax.ws.rs.ext.ReaderInterceptor接口来拦截调用。但不知何故,服务器从不调用它:拦截器只是被忽略。

I'm using the javax.ws.rs.ext.ReaderInterceptor interface to intercept the call. But somehow the server never calls it: the interceptor is just ignored.

我正在注册拦截器/资源,就像我使用前PreProcessInterceptor一样,并使用相同的@Provider和@ServerInterceptor注释:

I'm registering the interceptors/resources the same way as I did with the former PreProcessInterceptor, and using the same @Provider and @ServerInterceptor annotations:

ServerApplication:

ServerApplication:

public class ServerApplication extends javax.ws.rs.core.Application {

     private final HashSet<Object> singletons = new LinkedHashSet<Object>();

     public ServerApplication() {
         singletons.add(new SecurityInterceptor());
         singletons.add( ... ); //add each of my rest resources
     }

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>();
        return set;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

SecurityInterceptor:

SecurityInterceptor:

@Provider
@ServerInterceptor
public class SecurityInterceptor implements javax.ws.rs.ext.ReaderInterceptor {
     @Override
     public Object aroundReadFrom(ReaderInterceptorContext context){
            //code that is never called... so lonely here...
     }
}

有关如何解决此问题的任何见解?

Any insights about how can I solve this problem?

谢谢。

推荐答案

RESTEasy 3.xx符合JAX-RS 2.0规范。

RESTEasy 3.x.x conforms to the JAX-RS 2.0 specification.

什么你想要做的就是完成(也许更好):

What you are trying to do could be accomplished (maybe better) with:

@Provider
public class SecurityInterceptor 
      implements javax.ws.rs.container.ContainerRequestFilter {
     @Override
     public void filter(ContainerRequestContext requestContext){
       if (not_authenticated){ requestContext.abortWith(response)};
     }
}

ReaderInterceptor MessageBodyReader.readFrom 时,才会调用code>。

since the ReaderInterceptor is invoked only if the underlying MessageBodyReader.readFrom is called by the standard JAX-RS pipeline, not fromthe application code.

但是,为什么不调用拦截器的原因可能是 @ServerInterceptor 注释,这是一个RESTEasy延期。

The reason why your interceptor is not called, though, could be the @ServerInterceptor annotation, which is a RESTEasy extension.

规则见§6.5.2拦截器是全局注册的,除非 @Provider 注释了一个 @NameBinding 注释,但我不知道 RESTEasy 是否可以处理 @ServerInterceptor 如果没有明确注册,如 RestEASY拦截器未被调用

The spec states at §6.5.2 that a interceptor is globally registered, unless the @Provider is annotated with a @NameBinding annotation, but I don't know if RESTEasy can handle a @ServerInterceptor if it's not explicitly registered as shown in RestEASY Interceptor Not Being Called

这篇关于什么是Resteasy 3.X PreProcessInterceptor的正确替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆