gpt4 book ai didi

java - 如何使用 Tomcat 过滤器保护 pdf 文件?

转载 作者:行者123 更新时间:2023-11-28 22:38:52 25 4
gpt4 key购买 nike

我目前正在运行一个带有 struts 1 的 tomcat 实例,我希望 tomcat 检测何时在 URL 中请求 pdf 文件(例如链接:http://www.***.com/files/action=download&name=myreport.pdf).

此时我想要实例化一个 java 类,然后使用 pdf API 我想将密码注入(inject)文件。这里的要点是我不想将密码存储在我提供的原始 pdf 文件中,而是希望密码在运行时由 Tomcat 注入(inject)。

如果您有任何想法,请告诉我,我做了一些研究,遇到了 tomcat 过滤器,但我不确定这是否能解决这个问题。

请注意密码存储在数据库表中。

谢谢

最佳答案

我们从过滤器调用一个 Java 类来执行实际的“密码注入(inject)”。

web.xml 中的条目会将您的调用重定向到特定过滤器。

<!--web.xml call all calls to .pdf will invoke the particular filter.-->
<filter>
<filter-name>PDF Filter</filter-name>
<filter-class>PDFFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PDF Filter</filter-name>
<url-pattern>*.pdf</url-pattern>
</filter-mapping>

//This is the actual filter
public class PDFFilter implements Filter
{
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
PDFPasswordInjector pdfPassInject = new PDFPasswordInjector();
//use HttpServletRequestWrapper to get the pdf location/pdf name
pdfPassInject.injectPassword( "<pdf location>" );

chain.doFilter(request, response);
}
}

//Java class to inject the password
public class PDFPasswordInjector
{
public boolean injectPassword( String sPDFName )
{
// retrieve password from DB
// use API to inject password to PDF
}
}

关于java - 如何使用 Tomcat 过滤器保护 pdf 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6151927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com