1

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.

Dudeson
  • 41
  • 1
  • 8
  • Most likely, your bundle is containing the annotation classes. Then, the framework won't detect them, as it's using other instances for the classes. If you're using maven, set scope to 'provided' for the artifacts containing the annotations. – gjoranv Jul 20 '18 at 13:13
  • Thanks for answer, I have checked your provided solution, however i could not get it to work. Also my main project is without maven, using default kura project. I found solution, by adding : `@Provider @Component(service = ContainerRequestFilter.class) ` – Dudeson Jul 25 '18 at 07:32

2 Answers2

1

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");
    }
}
Dudeson
  • 41
  • 1
  • 8
0

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.

mate00
  • 2,727
  • 5
  • 26
  • 34