简体   繁体   English

拦截JAX-RS请求:使用tomcat注册ContainerRequestFilter

[英]Intercept JAX-RS Request: Register a ContainerRequestFilter with tomcat

I am trying to intercept a request to my JAX-RS webservice by a ContainerRequestFilter. 我试图通过ContainerRequestFilter拦截对我的JAX-RS Web服务的请求。 I want to use it with a custom annotation, so I can decorate certain methods of the webservice. 我想将它与自定义注释一起使用,因此我可以装饰webservice的某些方法。 This should enable me to handle requests to this methods based on the information whether they are made on a secure channel or not, before the actual method is executed. 这应该使我能够在执行实际方法之前根据信息是否在安全通道上处理来处理对此方法的请求。

I tried different approaches, searched several posts and then implemented mostly based on the answer by Alden in this post . 我尝试了不同的方法,搜索了几个帖子,然后主要根据Alden在这篇文章中的答案实施。 But I can't get it working. 但我无法让它发挥作用。

I have a method test in my webservice decorated with my custom annotation Ssl. 我的web服务中有一个方法测试,用我的自定义注释Ssl装饰。

@POST
@Path("/test")
@Ssl
public static Response test(){      
    System.out.println("TEST ...");
}

The annotation looks like this: 注释如下所示:

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface Ssl {}

Then I setup a filter implementation 然后我设置了一个过滤器实现

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;

@Ssl
@Provider
public class SslInterceptor implements ContainerRequestFilter
{
    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException
    {       
        System.out.println("Filter executed.");
    }
}

But the filter is never executed nor there occur any error messages or warnings. 但过滤器永远不会执行,也不会出现任何错误消息或警告。 The test method runs fine anyway. 无论如何,测试方法运行良好。

To resolve it, I tried to register the filter in the web.xml as described here . 要解决这个问题,我试图描述在web.xml中注册过滤器在这里

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>
      <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
      <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
    </init-param>

    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.my.packagewithfilter</param-value>
    </init-param>

    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.my.packagewithfilter.SslInterceptor</param-value>
    </init-param>

    <init-param>  
      <param-name>jersey.config.server.provider.packages</param-name>  
      <param-value>com.my.packagewithfilter</param-value>
    </init-param>    

  </servlet>

But that also doesn't work. 但这也行不通。 What am I missing? 我错过了什么? Any ideas how to make that filter work? 任何想法如何使过滤器工作? Any help is really appreciated! 任何帮助真的很感激!

You're using JAX-RS 2.0 APIs (request filters, name binding, ...) in your classes but Jersey 1 proprietary init params in your web.xml (package starting with com.sun.jersey , Jersey 2 uses org.glassfish.jersey ). 您正在使用类中的JAX-RS 2.0 API(请求过滤器,名称绑定等),但在您的web.xml Jersey 1专有的init params(以com.sun.jersey开头的包,Jersey 2使用org.glassfish.jersey )。 Take a look at this answer and at these articles: 看看这个答案和这些文章:

Just compiling the answer from Michael Gajdos to help someone who do not want open more tabs: 只需编译Michael Gajdos的答案,帮助那些不想打开更多标签的人:

When you are using Jersey-2 you must use the follow configuration to register your filter into the web.xml 使用Jersey-2时,必须使用以下配置将过滤器注册到web.xml中

jersey.config.server.provider.classnames jersey.config.server.provider.classnames

instead of 代替

com.sun.jersey.spi.container.ContainerRequestFilters (jersey-1x) com.sun.jersey.spi.container.ContainerRequestFilters (jersey-1x)

 <!-- This is the config needed -->
<servlet>    
      //...         
     <init-param>
         <param-name>jersey.config.server.provider.classnames</param-name>
         <param-value>com.your_package_path.yourClassFilter</param-value>
     </init-param>
      //...
</servlet>

看一下这篇博客文章 ,了解更多“经典”方法(不使用注释)

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

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