【发布时间】:2018-09-12 09:56:36
【问题描述】:
我有一个 Spring MVC 应用程序,部署到 Apache Tomcat。其中一页必须显示使用 itext pdf 库生成的 PDF 文件。
所以我在 JSP 文件中添加了object 标签:
<object data="<c:url value="/view-pdf" />"></object>
我在控制器内部有处理这个 URL 的方法:
@RequestMapping(value = "/view-pdf", method = RequestMethod.GET)
protected void viewPdf(HttpServletResponse response) {
ServletOutputStream out = response.getOutputStream();
//generate pdf here
Document document = new Document();
PdfWriter.getInstance(document, out);
document.setPageSize(PageSize.A4);
document.open();
document.add(new Paragraph("Hello, World"));
document.close();
out.close();
}
现在,当我打开应该显示 PDF 的页面时,它不显示 PDF 文件。 Chrome 控制台显示此错误:
Refused to display 'http://localhost:8080/MyApp/view-file' in a frame because it set 'X-Frame-Options' to 'deny'.
直接在地址栏中输入http://localhost:8080/MyApp/view-pdf URL 即可访问PDF。所以生成PDF没有问题。
这里的一些答案建议将这些行添加到web.xml 文件中:
<filter>
<filter-name>httpHeaderSecurity</filter-name>
<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>antiClickJackingEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>antiClickJackingOption</param-name>
<param-value>ALLOW-FROM</param-value>
</init-param>
<init-param>
<param-name>antiClickJackingUri</param-name>
<param-value>http://localhost:8080/MyApp/*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>httpHeaderSecurity</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我这样做了,但完全没有效果。我在这里做错了什么?如何避免这个错误?
我的 Spring 版本是 5.0.4.RELEASE,Tomcat 版本是 8.0.48。
【问题讨论】:
标签: spring-mvc pdf tomcat itext