Basic Auth can be a simple way to secure your web services, specially when it’s combined with SSL, to implement this kind of security with JaxRS, you need to create a ContainerRequestFilter and extract the header, check for the user and throw a exception if the user is invalid.
In this example we will use an simple stub to handle the authorization, for example, with this service:
The exception can extend WebApplicationException to handle the parsing to text, and have a nice message when the combination of user and password is wrong, for example:
When this two classes in order, we can create the security filter, we need the @Provider and @PreMatching annotations to intercept the request before is handled for our methods annotated with @Path, also we need to extends from ContainerRequestFilter: