我想从我的HTML过滤所有输入标签的内容。我正在使用AntiSamy过滤器,到目前为止,我的过滤器正在过滤完整的html内容(而不仅仅是输入值)。
我正在使用这里提供的实现:
Github
在doFilter方法中,我使用这段代码扫描内容
HttpServletResponseInvocationHandler invocationHandler = httpResponseInvocationHandlerFactory.build((HttpServletResponse) response);
CleanResults cleanResults = antiSamy.scan(invocationHandler.getContents(), policy);而我想要的是:
CleanResults cleanResults = antiSamy.scan(request.getParameter("input"), policy);这样,只有输入字段的内容被过滤。
下面是整个doFilter方法:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (response instanceof HttpServletResponse) {
HttpServletResponseInvocationHandler invocationHandler = httpResponseInvocationHandlerFactory.build((HttpServletResponse) response);
HttpServletResponse proxiedResponse = httpResponseProxyFactory.build(invocationHandler);
chain.doFilter(request, proxiedResponse);
if ("text/html;charset=UTF-8".equals(proxiedResponse.getContentType())) {
try {
Policy policy = policyFileLoader.load(policyFile);
antiSamy.setInputEncoding(inputEncoding);
antiSamy.setOutputEncoding(outputEncoding);
CleanResults cleanResults = antiSamy.scan(invocationHandler.getContents(), policy);
log.info("Number of Errors: " + cleanResults.getNumberOfErrors());
if (log.isDebugEnabled()) {
log.debug("Errors found: ");
List errors = cleanResults.getErrorMessages();
for (int i = 0; i < errors.size(); i++) {
log.debug("\t" + (i + 1) + ". " + errors.get(i));
}
}
log.info("Scan time (in seconds): " + cleanResults.getScanTime());
response.getOutputStream().write(cleanResults.getCleanHTML().getBytes());
} catch (ScanException e) {
log.error(GENERIC_ERROR, e);
} catch (PolicyException e) {
log.error(GENERIC_ERROR, e);
}
} else {
response.getOutputStream().write(invocationHandler.getBytes());
}
} else {
chain.doFilter(request, response);
}
}我已经在默认情况下对AntiSamy提供的所有策略文件进行了尝试。
如果能在这方面得到一些帮助,那就太好了。
发布于 2014-03-05 00:33:46
找到解决方案时,错误是使用不正确的正则表达式。现在工作得很好。
https://stackoverflow.com/questions/20070801
复制相似问题