在Java中实现容器请求过滤器

时间:2019-01-10 11:18:03

标签: java tomcat jax-rs

我正在尝试构建一个实现容器请求过滤器的Maven Web应用程序。我正在将NetBeans IDE与jax-rs框架和Tomcat服务器一起使用。并使用Postman测试程序。但是找不到类错误。
这是我的过滤程序。

  import com.sun.jersey.core.util.Base64;
  import java.io.IOException;
  import java.util.List;
  import java.util.StringTokenizer;

  import javax.ws.rs.container.ContainerRequestContext;
  import javax.ws.rs.container.ContainerRequestFilter;
  import javax.ws.rs.core.Response;
  import javax.ws.rs.ext.Provider;

  @Provider public class SecurityFilter implements ContainerRequestFilter {

     private static final String AUTHORIZATION_HEADER_KEY = "Authorization";
     private static final String AUTHORIZATION_HEADER_PREFIX = "Basic ";
     private static final String SECURED_URL_PREFIX = "test";

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

        if(requestContext.getUriInfo().getPath().contains(SECURED_URL_PREFIX)) {

            List < String > authHeader = requestContext.getHeaders().get(AUTHORIZATION_HEADER_KEY);
            if (authHeader != null && authHeader.size() > 0) {
                String authToken = authHeader.get(0);
                authToken = authToken.replaceFirst(AUTHORIZATION_HEADER_PREFIX, "");
                String decodedString = Base64.base64Decode(authToken);
                 StringTokenizer tokenizer = new StringTokenizer(decodedString, ":");
                 String username = tokenizer.nextToken();
                 String password = tokenizer.nextToken();

                 if ("user".equals(username) && "password".equals(password)) {
                    System.out.println("Headers :" + requestContext.getHeaders());
                   }

            }
            Response unauthorizedStatus = Response
            .status(Response.Status.UNAUTHORIZED)
            .entity("User cannot access the resource.")
            .build();

             requestContext.abortWith(unauthorizedStatus);

    }

  }
}

这是我的错误

  

javax.servlet.ServletException:Servlet [Jersey的Servlet.init()   REST服务]引发异常java.lang.NoClassDefFoundError:   javax / ws / rs / container / ContainerRequestFilter

有人可以帮忙吗?

0 个答案:

没有答案