Java 获取Jersey ResourceFilterFactory中的实际参数值

Java 获取Jersey ResourceFilterFactory中的实际参数值,java,rest,authorization,jersey,jax-rs,Java,Rest,Authorization,Jersey,Jax Rs,我想使用Jersey在我的REST服务中实施自定义授权。此自定义授权检查方法上的注释以及 方法接收 我的jax-rs注释方法如下所示: @GET @Path("customers") @Requires(Role.CustomerManager) public Customer getCustomer(@ParseFromQueryString @CheckPermission final Customer customer) { // ... } public class Query

我想使用Jersey在我的REST服务中实施自定义授权。此自定义授权检查方法上的注释以及 方法接收

我的jax-rs注释方法如下所示:

@GET
@Path("customers")
@Requires(Role.CustomerManager)
public Customer getCustomer(@ParseFromQueryString @CheckPermission final Customer customer) {
    // ...
}
public class QueryStringCustomerInjectable implements Injectable<Customer> {
  public Customer getValue() {
    final Customer customer = new Customer();
    // ... a UriInfo was injected using the @Context annotation
    // ... extract parameters from QueryString and use setters
    return customer;
  }
}
@ParseFromQueryString
是一个注释,它指示(通过可注入提供程序)从查询字符串中解组
客户。其代码如下所示:

@GET
@Path("customers")
@Requires(Role.CustomerManager)
public Customer getCustomer(@ParseFromQueryString @CheckPermission final Customer customer) {
    // ...
}
public class QueryStringCustomerInjectable implements Injectable<Customer> {
  public Customer getValue() {
    final Customer customer = new Customer();
    // ... a UriInfo was injected using the @Context annotation
    // ... extract parameters from QueryString and use setters
    return customer;
  }
}
公共类QueryStringCustomerInjectable实现可注入{
公共客户getValue(){
最终客户=新客户();
//…使用@Context注释注入了UriInfo
//…从QueryString中提取参数并使用setter
退货客户;
}
}
@CheckPermission
注释表示要检查客户权限的自定义授权人。某些用户可以访问某些客户的信息。类似地,
@Requires
注释承担调用程序应该具有的角色。这些不是java的安全角色(字符串),而是枚举值

使用Jersey的
ResourceDebuggingFilter
作为起点,我已经能够知道将调用哪个方法。但是,我仍然没有弄清楚如何确定实际将使用哪些参数来调用该方法

在我的脑海中,我能想到两个解决办法:

  • 使用Guice+Jersey的方法拦截器
  • QueryStringCustomerInjectable中编写此逻辑,但这似乎有点草率。这将是一门做得太多的课 然而,我真的很想只使用Jersey/JAX-RS来实现这一点。我觉得我已经很接近了

    想法?指针


    谢谢

    对于客户反序列化,您可以实现javax.ws.rs.ext.ParamConverterProvider并将其注册到Jersey中。然后,您可以使用@QueryParam(“客户”)将其注入到方法中。它更灵活一些,因为您还可以将它与@BeanParam或@PathParam注释一起使用

    然后可以使用ContainerRequestFilter。例如,请参见jersey如何处理Oauth1。 下一步你可以做的是创建一个功能来注册新创建的过滤器(请参阅参考资料-我现在找不到源代码)


    祝你好运

    为什么不使用自己的Servlet过滤器呢

    public class YourFilter implements Filter {
      ...
      @Override
     public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain filterChain) throws IOException, ServletException {
    
        // HttpServletRequest httpReq = (HttpServletRequest) request;
        // HttpServletResponse httpResp = (HttpServletResponse) response;
    
        // HttpServletRequest httpReq = (HttpServletRequest) request;
        // HttpServletResponse httpResp = (HttpServletResponse) response;
        // ..... httpReq.getUserPrincipal();
    
    
        // then set what you need using ThreadLocal and use it inside your resource class
    
        // do not forget to call 
        filterChain.doFilter(request, response); // at the end of this method
    
     }
    
    最后一步是注册servlet过滤器。这是使用web应用程序的web.xml完成的


    在调用jersey资源中的实际代码之前,它将拦截您的HTTP请求。

    您应该使用
    过滤器
    拦截器
    来处理有关该方法的所有信息。

    我还没有找到一种没有GUI的方法来做这件事。。。因此,它要么使用guice,要么使用cxf,后者提供了拦截器和一种提供您自己的调用程序的方法。您是否研究过AOP,或者只是在解决方案1中使用Servlet过滤器?您是否尝试过使用
    ContainerRequestFilter
    并将客户注入其中?我不确定
    @BeanParam
    是否仅限于资源,或者它们是否也可以注入过滤器,但我假设只要过滤器没有预匹配,它就应该工作。