Java 如何使用Tomcat过滤器保护pdf文件?
嗨 我目前正在使用struts 1运行tomcat实例,我希望tomcat能够检测URL中何时请求pdf文件(例如链接:Java 如何使用Tomcat过滤器保护pdf文件?,java,tomcat,passwords,servlet-filters,protection,Java,Tomcat,Passwords,Servlet Filters,Protection,嗨 我目前正在使用struts 1运行tomcat实例,我希望tomcat能够检测URL中何时请求pdf文件(例如链接:http://www.***.com/files/action=download&name=myreport.pdf) 此时,我希望实例化一个java类,然后使用pdf API向文件中注入密码。这里的要点是,我不希望在我提供的原始pdf文件中存储密码,而是希望Tomcat在运行时注入密码 请让我知道如果你有任何想法,我做了一些研究,我遇到了tomcat过滤器,但我不确定这是否会
http://www.***.com/files/action=download&name=myreport.pdf
)
此时,我希望实例化一个java类,然后使用pdf API向文件中注入密码。这里的要点是,我不希望在我提供的原始pdf文件中存储密码,而是希望Tomcat在运行时注入密码
请让我知道如果你有任何想法,我做了一些研究,我遇到了tomcat过滤器,但我不确定这是否会解决这个问题
请注意,密码存储在数据库表中
谢谢
编写一个
过滤器
截取所有返回PDF的请求应该是相当简单的。Filter的doFilter()方法可以访问请求和响应,因此您可以随意修改它。编写一个过滤器来拦截所有返回PDF的请求应该是相当简单的。Filter的doFilter()方法可以访问请求和响应,因此您可以随意修改它。筛选器不是解决此特定问题的方法。过滤器允许您修改请求,并使它们重定向或重新分配到不同的servlet。但是他们不允许你重写响应体。据我所知,这就是你想要做的
您必须在Servlet中修改PDF文件,如@Aardvocate的回答中所述。过滤器不是解决此特定问题的方法。过滤器允许您修改请求,并使它们重定向或重新分配到不同的servlet。但是他们不允许你重写响应体。据我所知,这就是你想要做的
您必须在Servlet中修改PDF文件,如@Aardvocate的回答中所述。我们从过滤器中调用Java类来执行实际的“密码注入”
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
}
}
PDF过滤器
PDFFilter
PDF过滤器
*.pdf
//这是实际的过滤器
公共类PDFFilter实现过滤器
{
public void doFilter(ServletRequest请求、ServletResponse响应、,
FilterChain链)抛出IOException、ServletException
{
PDFPasswordInjector pdfPassInject=新的PDFPasswordInjector();
//使用HttpServletRequestWrapper获取pdf位置/pdf名称
pdfPassInject.injectPassword(“”);
链式过滤器(请求、响应);
}
}
//Java类来注入密码
公共类PDFPasswordInjector
{
公共密码(字符串sPDFName)
{
//从数据库检索密码
//使用API将密码注入PDF
}
}
从过滤器中,我们调用一个Java类来执行实际的“密码注入”
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
}
}
PDF过滤器
PDFFilter
PDF过滤器
*.pdf
//这是实际的过滤器
公共类PDFFilter实现过滤器
{
public void doFilter(ServletRequest请求、ServletResponse响应、,
FilterChain链)抛出IOException、ServletException
{
PDFPasswordInjector pdfPassInject=新的PDFPasswordInjector();
//使用HttpServletRequestWrapper获取pdf位置/pdf名称
pdfPassInject.injectPassword(“”);
链式过滤器(请求、响应);
}
}
//Java类来注入密码
公共类PDFPasswordInjector
{
公共密码(字符串sPDFName)
{
//从数据库检索密码
//使用API将密码注入PDF
}
}
您是说您存储PDF的密码受保护吗?或者您希望用户在访问pdf之前输入密码?您是说您存储pdf的密码受保护吗?或者您希望用户在访问pdf之前输入密码?