目前,我正在研究清洁的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,但是这似乎也不起作用。
答案 0 :(得分: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");
}
}
答案 1 :(得分:0)
这有点晚了,但是我再次迷失了这个问题...
这里的问题实际上是@PreMatching
注释,它实际上与@NameBinding
注释冲突。
因此,如果您想使用@RolesXXX
注释,则必须将@PreMatching
定义为过滤器,而失去NameBinding
的功能。