Dudeson
Dudeson

Reputation: 41

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.

Upvotes: 1

Views: 319

Answers (2)

callibso
callibso

Reputation: 1

This is a little bit late, but I just stumble over this problem again... The problem here is really the @PreMatching annotation, which effectively clashes with the @NameBinding annotation. So if you want to use the @RolesXXX annotations you are forced to define your filter as @PreMatching loosing the NameBinding feature.

Upvotes: 0

Dudeson
Dudeson

Reputation: 41

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

Upvotes: 1

Related Questions