球衣2请求过滤器在此代码中不起作用

时间:2018-11-05 07:11:19

标签: java jax-rs jersey-2.0

我已经使用Jersey 2实现了注释,但是不知何故代码未达到该点。

这些是我的课程:

安全

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;

import javax.ws.rs.NameBinding;

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

安全实施:

import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;

@Secured
@Provider
@Priority(Priorities.AUTHENTICATION)
public class SecureImplementation implements ContainerRequestFilter
{
  @Override
    public void filter(ContainerRequestContext requestContext) throws 
  IOException {
        System.out.println("Inside Something");
        throw new WebApplicationException(Status.UNAUTHORIZED);
}
}

TestingClass

@Path("/checkClass")
public class TestingClass{

    @Secured
    @Path("/checkFilter")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Map<String, String> testing(Input input)
            throws Exception {
        Map<String, Object> responseMap = new HashMap<String, Object>();
        responseMap.put("result","Success");
        return responseMap;
   }
  }

它给了我一个响应,而不是给我一个401错误。那么,我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

通常null应该与过滤器一起在应用程序类中注册,例如:

TestingClass