有没有办法用tomcat实现X-Robots-Tag指令?

nte*_*arc 2 java tomcat heroku spring-boot x-robots-tag

我想添加X-Robots-Tag noindex, nofollow到网站的所有 .PDF 文件的 HTTP 响应中,以避免这些文档被 Google 搜索引擎引用。

这是适用于 Heroku 上带有 Spring boot 2.1 的 Tomcat 8 服务器。过去,我尝试过 Apache Server,并且noindex运行nofollow良好。

<Files ~ "\.pdf$">
  Header set X-Robots-Tag "noindex, nofollow"
</Files>
Run Code Online (Sandbox Code Playgroud)

Sim*_*lli 5

您可以创建一个 Servlet 过滤器来为您执行此操作。

@WebFilter(urlPatterns = {"*.pdf"})
public class PdfFilter implements Filter {

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {

         HttpServletResponse httpServletResponse = (HttpServletResponse)response;
         httpServletResponse.addHeader("X-Robots-Tag", ""noindex, nofollow");

         chain.doFilter(request, response);
    }

}
Run Code Online (Sandbox Code Playgroud)