Je voulais utiliser un intercepteur (annotation personnalisée) dans un service jax-rs.L'intercepteur JAX-RS ne peut pas fonctionner
1.Premièrement, j'ai écrit une classe d'annotation: BasicAuthentication.java:
@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BasicAuthentication {
}
2.Puis J'ai ajouté le BasicAuthenticationInterceptor implémente javax.ws.rs.ext.ReaderInterceptor
BasicAuthenticationInterceptor.java :
@Provider
@Priority(Priorities.AUTHENTICATION)
@BasicAuthentication
public class BasicAuthenticationInterceptor extends Dumpable implements ReaderInterceptor {
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
//log.info("authentication here")
String authHeader = context.getHeaders().getFirst(AUTHORIZATION);
if (authHeader == null) {
error("\"authorization\" is not found from the request header.");
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
return context.proceed();
}
}
3.Enfin, j'ajoute un service de test avec l'annotation @BasicAuthentication.
TestRestfulService.java
@Stateless
@Path("/api")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@BasicAuthentication
public class TestRestfulService extends Dumpable{
@EJB
LocalService localService;
@Path("/test/{id}")
@GET
public Response test(@PathParam("id")String id) {
try {
localService.findUser(id);
} catch (Exception e) {
error(e);
return Response.serverError().build();
}
return Response.ok().build();
}
}
Mais chaque fois que je demande/api/test/1 avec en-tête vide, je peux obtenir la réponse correcte, l'intercepteur semble fonctionne pas du tout. J'utilise Wildfly 10 Merci d'avance.