1

目前我正在使用干净的 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");
    }
}

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

4

2 回答 2

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");
    }
}
于 2018-07-25T07:37:54.693 回答
0

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

于 2019-09-04T08:42:39.423 回答