【问题标题】:OSGI JAX-RS name bindingOSGI JAX-RS 名称绑定
【发布时间】:2018-07-20 10:54:26
【问题描述】:

目前我正在处理干净的 kura-osgi 项目。我的目标是实现 JWT 身份验证或类似的东西,只是为了提供安全的端点。我尝试过使用名称绑定的身份验证过滤器。但是看起来,在某种程度上,名称绑定没有被注册。在这种情况下,我测试了简单的 maven 项目,发现一切正常。有代码:

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

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

【问题讨论】:

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

标签: jersey jax-rs osgi kura


【解决方案1】:

经过很多不同的解决方案,我找到了简单的解决方案。只需注册为组件即可解决问题。过滤器类看起来像这样:

@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");
    }
}

【讨论】:

    【解决方案2】:

    这有点晚了,但我又一次偶然发现了这个问题...... 这里的问题实际上是@PreMatching 注释,它实际上与@NameBinding 注释冲突。 因此,如果您想使用 @RolesXXX 注释,您必须将过滤器定义为 @PreMatching 失去 NameBinding 功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      相关资源
      最近更新 更多