Jersey OSGiJAX-RS名称绑定

Jersey OSGiJAX-RS名称绑定,jersey,jax-rs,osgi,kura,Jersey,Jax Rs,Osgi,Kura,目前我正在从事clean kura osgi项目。我的目标是实现JWT身份验证,或者类似的东西,只是为了提供安全的端点。我尝试了使用名称绑定的身份验证过滤器。然而,似乎在某种程度上名称绑定并没有得到注册。在本例中,我测试了SimpleMaven项目,并发现在那里一切正常。代码如下: TestAuth.class @Path("/test") public class TestAuth extends Application { @GET @Secured @Produc

目前我正在从事clean kura osgi项目。我的目标是实现JWT身份验证,或者类似的东西,只是为了提供安全的端点。我尝试了使用名称绑定的身份验证过滤器。然而,似乎在某种程度上名称绑定并没有得到注册。在本例中,我测试了SimpleMaven项目,并发现在那里一切正常。代码如下:

TestAuth.class

@Path("/test")
public class TestAuth extends Application {

    @GET
    @Secured
    @Produces(MediaType.TEXT_PLAIN)
    public String test() {
        return "hello";
    }
}
名称绑定接口:

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface Secured {}
过滤器:

@Provider
@PreMatching
@Secured
public class  AuthenticationFilter implements ContainerRequestFilter {

    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        System.out.println("GG");
    }
}

例如,我已经检查了许多方法来修复它:但这一种似乎也不起作用。

在许多不同的解决方法之后,我找到了简单的解决方案。只需注册为组件即可解决问题。筛选器类将如下所示:

@Provider
@Component(service = ContainerRequestFilter.class)
@Secured
public class AuthenticationFilter implements ContainerRequestFilter, ContainerResponseFilter {

    private static final Logger LOG = LoggerFactory.getLogger(AuthenticationFilter.class);

    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        LOG.info("Request filter called");
    }

    public void filter(ContainerRequestContext containerRequestContext,
            ContainerResponseContext containerResponseContext) throws IOException {
        LOG.info("Response filter called");
    }
}

这有点晚了,但我又一次被这个问题绊倒了。。。 这里的问题实际上是
@PreMatching
注释,它实际上与
@NameBinding
注释冲突。
因此,如果您想使用
@RolesXXX
注释,您必须将过滤器定义为
@PreMatching
,取消
名称绑定
功能。

最有可能的是,您的包中包含注释类。然后,框架将不会检测到它们,因为它正在为类使用其他实例。如果您使用的是maven,请将包含注释的工件的作用域设置为“提供”。感谢您的回答,我已经检查了您提供的解决方案,但是我无法让它工作。我的主项目也没有maven,使用默认的kura项目。我通过添加:
@Provider@Component(service=ContainerRequestFilter.class)