jerseyjax-rsosgikura

OSGI JAX-RS name binding


Currently I'm working with clean kura-osgi project. My aim is to implement JWT authentication, or something similar to it, just to provide secure endpoints. I have tried Authentication filter using name-binding. However seem like, in someway name-binding is not getting registered. In this case I have tested simple maven project, and found out everything works there. There is code:

TestAuth.class

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

    @GET
    @Secured
    @Produces(MediaType.TEXT_PLAIN)
    public String test() {
        return "hello";
    }
}

name binding interface:

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface Secured {}

Filter:

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

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

I have checked a lot of ways to fix it, for an example: this but this one seems not to work also.


Solution

  • After a lot of different solution approaches I have found simple solution. Simply by registering as component fixes problem. Filter class would look like this:

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