JAX-RS剩余过滤器不调用 [英] JAX-RS Rest Filter does not invoke

查看:113
本文介绍了JAX-RS剩余过滤器不调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要实现安全性的api.

I have an api that needs to implement security.

但是没有调用过滤器.我的呼叫直接传递到端点...

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

我的安全界面

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

我的过滤器

@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) {
        ...
    }

}

}

我需要认证的方法.

@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?

我的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>

我正在使用Jersey 1.17和glassfish 4

I am using Jersey 1.17 and glassfish 4

推荐答案

我发现了这个问题,是在Paul建议尝试在Glassfish上发布Jersey 2的应用程序之后,我发现了Glassfish版本中的不兼容问题. Glassfish 4.0不支持jersey 2,但4.1.2版本是.我迁移了服务器并解决了问题.

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.

这篇关于JAX-RS剩余过滤器不调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆