泽西岛没有触发ContainerRequestFilter

时间:2016-02-05 13:59:29

标签: jersey grizzly

我正在尝试使用ContainerRequestFilter在基于Grizzly的Jersey应用程序上强制执行某些身份验证。

我通过扩展PackagesResourceConfig创建自己的资源配置:

public class MainResourceConfig extends PackagesResourceConfig {

    public MainResourceConfig() {
        super("za.co.quinn.ws");
        Map<String, Object> properties = getProperties();
        properties.put(
                "com.sun.jersey.spi.container.ContainerRequestFilter",
                "com.sun.jersey.api.container.filter.LoggingFilter;" + MainRequestFilter.class.getName()
        );
        properties.put(
                "com.sun.jersey.spi.container.ContainerResponseFilters",
                "com.sun.jersey.api.container.filter.LoggingFilter;" + MainResponseFilter.class.getName()
        );
    }
}

请求过滤器用于身份验证:

@Inject
Authorization authorization;

@Override
public ContainerRequest filter(ContainerRequest request) {

    if (!request.getRequestUri().getPath().endsWith(".wadl"))
        authorization.authorize(request);

    return request;
}

响应过滤器适用于标题:

@Override
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {

    response.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
    response.getHttpHeaders().add("Access-Control-Allow-Methods", "GET, HEAD, POST, DELETE, PUT");
    response.getHttpHeaders().add("Access-Control-Allow-Headers", "Authorization, Content-Type");

    return response;
}

MainResponseFilter确实被触发但不是ContainerRequestFilter。

1 个答案:

答案 0 :(得分:1)

  

“MainResponseFilter确实被触发但不是ContainerRequestFilter。”

查看您的属性,主要是发送它,并比较它们

...container.ContainerRequestFilter"

...container.ContainerResponseFilters"

您遗漏了s请求过滤器。为了避免像这样的拼写错误,你可以使用常量

您也可以

getContainerRequestFilters().add(new YourRequestFilter());
getContainerResponseFilters().add(new YourResponseFilter());