Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jersey ContainerResponseFilter未应用于所有响应_Filter_Jersey_Jetty_Servlet Filters_Dropwizard - Fatal编程技术网

Jersey ContainerResponseFilter未应用于所有响应

Jersey ContainerResponseFilter未应用于所有响应,filter,jersey,jetty,servlet-filters,dropwizard,Filter,Jersey,Jetty,Servlet Filters,Dropwizard,我有一个Dropwizard 1.0.0应用程序,使用ContainerResponseFilter,我希望看到应用于每个资源的一系列标题,但是我只看到它们应用于根级别的文档 过滤器类别: package com.uk.jacob.filters; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws

我有一个Dropwizard 1.0.0应用程序,使用ContainerResponseFilter,我希望看到应用于每个资源的一系列标题,但是我只看到它们应用于根级别的文档

过滤器类别:

package com.uk.jacob.filters;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;

public class SecurityFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
        MultivaluedMap<String, Object> headers = containerResponseContext.getHeaders();

        headers.add("strict-transport-security", "max-age=31536000");
        headers.add("x-content-type-options", "nosniff");
        headers.add("x-frame-options", "SAMEORIGIN");
        headers.add("x-xss-protection", "1;  mode=block");
    }
}
package com.uk.jacob.filters;
导入javax.ws.rs.container.ContainerRequestContext;
导入javax.ws.rs.container.ContainerResponseContext;
导入javax.ws.rs.container.ContainerResponseFilter;
导入javax.ws.rs.core.MultivaluedMap;
导入java.io.IOException;
公共类SecurityFilter实现ContainerResponseFilter{
@凌驾
公共无效筛选器(ContainerRequestContext ContainerRequestContext、ContainerResponseContext ContainerResponseContext)引发IOException{
多值Map headers=containerResponseContext.getHeaders();
标题。添加(“严格的运输安全”,“最大年龄=31536000”);
添加(“x-content-type-options”、“nosniff”);
标题。添加(“x-frame-options”、“SAMEORIGIN”);
添加(“x-xss-protection”,“1;模式=块”);
}
}
应用程序类别:

package com.uk.jacob;

import com.uk.jacob.filters.SecurityFilter;
import com.uk.jacob.resources.HomepageResource;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.client.HttpClientBuilder;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.views.ViewBundle;
import org.apache.http.client.HttpClient;

public class websiteApplication extends Application<websiteConfiguration> {

    public static void main(final String[] args) throws Exception {
        new websiteApplication().run(args);
    }

    @Override
    public String getName() {
        return "website";
    }

    @Override
    public void initialize(final Bootstrap<websiteConfiguration> bootstrap) {
        bootstrap.addBundle(new ViewBundle<websiteConfiguration>());
        bootstrap.addBundle(new AssetsBundle("/public/", "/public"));
    }

    @Override
    public void run(final websiteConfiguration configuration, final Environment environment) {
        final HttpClient httpClient = new HttpClientBuilder(environment).using(configuration.getHttpClientConfiguration()).build(getName());

        environment.jersey().register(new SecurityFilter());
        environment.jersey().register(new HomepageResource(httpClient));
    }

}
package com.uk.jacob;
导入com.uk.jacob.filters.SecurityFilter;
导入com.uk.jacob.resources.HomepageResource;
导入io.dropwizard.Application;
导入io.dropwizard.assets.AssetsBundle;
导入io.dropwizard.client.HttpClientBuilder;
导入io.dropwizard.setup.Bootstrap;
导入io.dropwizard.setup.Environment;
导入io.dropwizard.views.ViewBundle;
导入org.apache.http.client.HttpClient;
公共类websiteApplication扩展了应用程序{
公共静态void main(最终字符串[]args)引发异常{
新建websiteApplication().run(args);
}
@凌驾
公共字符串getName(){
返回“网站”;
}
@凌驾
公共无效初始化(最终引导引导引导){
addBundle(新的ViewBundle());
addBundle(新资产包(“/public/”,“/public”);
}
@凌驾
公共无效运行(最终网站配置、最终环境){
最终的HttpClient HttpClient=新的HttpClientBuilder(环境)。使用(configuration.getHttpClientConfiguration()).build(getName());
environment.jersey().register(new SecurityFilter());
environment.jersey().register(新HomepageResource(httpClient));
}
}

您的问题是资产负债。资产绑定是独立的servlet,它们不经过泽西岛生态系统。来源:

@Override
public void run(Environment environment) {
    LOGGER.info("Registering AssetBundle with name: {} for path {}", assetsName, uriPath + '*');
    environment.servlets().addServlet(assetsName, createServlet()).addMapping(uriPath + '*');
}
这就是为什么不对资产资源调用筛选器。解决方案是编写一个额外的ServletFilter(旧式),用于过滤资产请求并在其中添加头

为了将标题添加到资产包中,您必须使用普通servlet过滤器并向DW注册:

以下是我的ServletFilter的代码:

public class ServletRequestFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("Asset filter");

        HttpServletResponse httpServletResponse = ((HttpServletResponse) response);
        httpServletResponse.addHeader("ARTUR", "test");

        chain.doFilter(request, httpServletResponse);
    }

    @Override
    public void destroy() {
    }

}
然后按如下方式进行注册:

public class ViewApplication extends io.dropwizard.Application<Configuration>{

    @Override
    public void run(Configuration configuration, Environment environment) throws Exception {
        environment.jersey().register(ViewResource.class);
        environment.jersey().register(HeaderResponseFilter.class);
        environment.servlets().addFilter("Custom-Filter-Name", new ServletRequestFilter()).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/assets/*");
    }

    @Override
    public void initialize(Bootstrap<Configuration> bootstrap) {
        super.initialize(bootstrap);
        bootstrap.addBundle(new ViewBundle<>());
        bootstrap.addBundle(new AssetsBundle("/assets/", "/assets"));
    }

    public static void main(String[] args) throws Exception {
        new ViewApplication().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml");
    }

}
注意我的自定义标题
ARTUR:test

问候,


artur

什么是根级文档?请添加示例,说明何时添加标题,何时不添加(加上您希望看到的内容)。从您提供的信息来看,这应该可以正常工作。我使用1个视图和一个资源对我的框进行了快速测试,并且正确应用了筛选器。/index.html可以正常工作/image.png不能正常工作。这可能与AssetsBundle相关吗?
artur@pandaadb:~/dev/repo/sandbox$ curl -v "http://localhost:9085/assets/test.txt"
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9085 (#0)
> GET /assets/test.txt HTTP/1.1
> Host: localhost:9085
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 19 Sep 2016 15:07:13 GMT
< ARTUR: test
< Last-Modified: Mon, 19 Sep 2016 14:52:37 GMT
< ETag: "0d1ae97d61a8900c99abddb8741febaf"
< Content-Type: text/plain;charset=utf-8
< Vary: Accept-Encoding
< Content-Length: 11
< 
asd
asd
* Connection #0 to host localhost left intact