Search code examples
javarestjerseyjax-rsrestful-authentication

JAX-RS Rest Filter does not invoke


I have an api that needs to implement security.

But the filter is not invoked. my call pass directly to the endpoint...

My Secure interface

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface Seguro {}

My Filter

@Seguro
@Provider
@Priority(Priorities.AUTHENTICATION)
public class FiltroAutenticacao implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {

    String authorizationHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);

    if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
        throw new NotAuthorizedException("Authorization header precisa ser provido");
    }

    String token = authorizationHeader.substring("Bearer".length()).trim();

    try {
        ...

    } catch (Exception e) {
        ...
    }

}

}

My method that needs to be authenticated.

@Seguro
@GET
@Path("/metodo-teste")
@Produces("application/json")
public Response medotoTeste(@QueryParam("codigo") String codigo){       

    ModeloTesteTO to = new ModeloTesteTO("codigo enviado foi " + codigo);       
    return Response.ok(to, MediaType.APPLICATION_JSON).build();
}

Do I need to implement anything else?

My web.xml

  <servlet>
  <servlet-name>Jersey REST Service</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

  <init-param>
  <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
  <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
  </init-param>

  <init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>br.gov.es.dataci.aprender</param-value>
  </init-param>

  <init-param>
  <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
  <param-value>br.gov.es.dataci.aprender.seguranca.FiltroAutenticacao</param-value>
</init-param>

  <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/*</url-pattern>
  </servlet-mapping>

I am using Jersey 1.17 and glassfish 4


Solution

  • I discovered the problem, following Paul's suggestion of trying to publish the application with Jersey 2 on the glassfish, I discovered incompatibility in glassfish version. Glassfish 4.0 does not support jersey 2, the 4.1.2 version yes. I migrated the server and solved the problem.