泽西岛不触发 ContainerRequestFilter
Jersey not triggering ContainerRequestFilter
我正在尝试使用 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;
}
响应过滤器适用于 headers:
@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 不会被触发。
"The MainResponseFilter does get triggered but not the ContainerRequestFilter."
看看你的属性,主要是它的发送,然后比较它们
...container.ContainerRequestFilter"
...container.ContainerResponseFilters"
您缺少请求过滤器的 s
。为避免这样的拼写错误,您可以使用常量
ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS
ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS
你也可以
getContainerRequestFilters().add(new YourRequestFilter());
getContainerResponseFilters().add(new YourResponseFilter());
我正在尝试使用 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;
}
响应过滤器适用于 headers:
@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 不会被触发。
"The MainResponseFilter does get triggered but not the ContainerRequestFilter."
看看你的属性,主要是它的发送,然后比较它们
...container.ContainerRequestFilter"
...container.ContainerResponseFilters"
您缺少请求过滤器的 s
。为避免这样的拼写错误,您可以使用常量
ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS
ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS
你也可以
getContainerRequestFilters().add(new YourRequestFilter());
getContainerResponseFilters().add(new YourResponseFilter());