Cq5.5将Servlet过滤器应用于特定路径 [英] Cq5.5 applying a servlet filter to a specific path

查看:98
本文介绍了Cq5.5将Servlet过滤器应用于特定路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cq5.5中的自定义表单处理程序,一切都进行得很好.我现在正在努力锁定某些安全性,我的任务之一是为表单处理程序路径实现请求限制过滤器.

I'm working on a custom form handler in cq5.5 and everything is going great. I'm now working on locking down some of the security and one of my tasks is to implement a request throttling filter to the form handlers path.

目前我有类似的东西

@Component(immediate = true, metatype = true)

@Service(javax.servlet.Filter.class)

@Properties({
  @Property(name="service.pid", value="com.xxxxxx.cq.core.filter.FormFilter",propertyPrivate=false),
  @Property(name="service.description",value="FormFilter", propertyPrivate=false),
  @Property(name="service.vendor",value="xxxxxx - Microsites", propertyPrivate=false),
  @Property(name = "filter.scope", value = "request"),
  @Property(name = "sling.filter.scope", value = "request"),
  @Property(name = "service.ranking", intValue = 100001)
})

public class FormFilter implements javax.servlet.Filter {
  private Logger LOGGER = LoggerFactory.getLogger(TrackingFilter.class.getName());
  private static final Object lock = new Object();

  @Override
  public void doFilter(ServletRequest pRequest, ServletResponse pResponse, FilterChain pChain) throws IOException, ServletException {
      //my filter stuff
  }
}

这工作正常,但我想将其锁定为仅在特定路径上运行.

This works fine but I'd like to lock it down to only run at a specific path.

感谢您的见解.

----编辑----- 在进行了更多研究之后,我发现了几篇文章,指出无法将过滤器注册到默认ServletFilter处理程序的指定路径.基本上,我找到的两个解决方案是为过滤器创建一个新的OSGI捆绑包,然后使用ExtHTTPService或Whiteboard注册它:

----EDIT----- After doing more research I found a few posts stating that there is no way to register a filter to a specified path for the default ServletFilter handler. Basically the two solutions to this issue I've found were either create a new OSGI bundle for the filter and register it using the ExtHTTPService or Whiteboard:

http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html

OR

过滤掉过滤器本身中的网址.因此,基本上在我的过滤器中添加对指定路径的检查.

Filter out the url within the filter itself. So basically add a check for the specified path in my filter.

即:

  @Override
  public void doFilter(ServletRequest pRequest, ServletResponse pResponse, FilterChain pChain) throws IOException, ServletException {

      String path = pRequest.getContextPath();

      if (path.contains("my/matching/path")
      {
        //my filter stuff
      }
  }

我很想看看是否有其他解决方案可以解决此问题,但我想分享到目前为止我所能找到的解决方案,希望这将有助于激发更多的想法,甚至只是帮助具有相同想法的人问题可以节省一些在Google搜索上的时间.

I would love to see if there are additional solutions to this issue, but wanted to share what I've been able to find so far, in hopes that this will either help spur more ideas or even just help someone with the same issue save some time on google searching.

谢谢你, 布罗迪

推荐答案

  1. 您的见解是正确的:无法将过滤器绑定到路径.您应该手动检查它(不要忘记调用chain.doFilter()).

替代选项是 OptingServlet .它是提供一种方法的接口:accepts(SlingHttpServletRequest request).在Sling[Safe|All]MethodsServlet中实现此接口可让您定义您感兴趣的请求类型.

Alternative option is the OptingServlet. It is an interface providing one method: accepts(SlingHttpServletRequest request). Implementing this interface in your Sling[Safe|All]MethodsServlet allows you to define what kind of requests you are interested in.

另一种选择是使用选择器而不是路径片段.例如.具有以下注释的servlet将通过选择器(如/content/geometrixx/en.my-selector.html)针对所有请求被调用:

Another option is to use selector instead of a path fragment. Eg. servlet with following annotation will be invoked for all requests with a selector (like /content/geometrixx/en.my-selector.html):

@SlingServlet(selectors = "my-selector", resourceTypes="sling/servlet/default")

边注:您可能想使用这个漂亮的注释来声明一个过滤器:

Sidenote: you may want to use this nice annotation to declare a filter:

@SlingFilter(scope = SlingFilterScope.REQUEST, order = 100001)

它将自动添加@Component@Service声明.

这篇关于Cq5.5将Servlet过滤器应用于特定路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆