Cq5.5, applying a servlet filter to a specific path

I am working on a special form handler in cq5.5 and everything is going fine. Now I am working on blocking some security, and one of my tasks is to implement a request throttling filter on the path of the form handlers.

I currently have something like

@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 would like to block it so that it only works on a specific path.

Thanks for any ideas.

---- EDIT ----- After further research, I found several messages stating that it was not possible to register a filter for the specified path for the default ServletFilter handler. Basically, two solutions to this problem that I found either created a new OSGI kit for the filter and registered it using ExtHTTPService or the board:

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

OR

Filter the URL inside the filter itself. So basically add a check for the specified path in my filter.

i.e:

  @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 } } 

I would like to see if there are additional solutions to this problem, but I wanted to share what I managed to find so far, in the hope that this will help either increase the number of ideas, or even help someone with the same problem save some time on google search.

Thank you brody

+6
source share
2 answers
  • Your understanding is correct: there is no way to bind a filter to a path. You must check it manually (remember to call chain.doFilter() ).

  • An alternative is OptingServlet . This is an interface that provides one method: accepts(SlingHttpServletRequest request) . Implementing this interface in your Sling[Safe|All]MethodsServlet allows Sling[Safe|All]MethodsServlet to determine which queries you are interested in.

  • Another option is to use a selector instead of a path fragment. For instance. a servlet with the following annotation will be called for all requests with a selector (for example, /content/geometrixx/en.my-selector.html ):

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

Sidenote: you can use this nice annotation to declare a filter:

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

It will automatically add @Component and @Service .

+4
source

@Tomek Rękawek: That’s not true .... You can match the filter to the outline. I did this using the following method: see code below.

 @SlingFilter(order=1) @Properties({ @Property(name="service.pid", value="com.videojet.hiresite.filters.AddNewUserFilter",propertyPrivate=false), @Property(name="service.description",value="Authentication Filter", propertyPrivate=false), @Property(name="service.vendor",value="Zensar Tech", propertyPrivate=false), @Property(name="pattern",value="/services/videojet/v1/AddNewUserController/view", propertyPrivate=false) }) public class AddNewUserFilter implements javax.servlet.Filter{ private final Logger log = LoggerFactory.getLogger(this.getClass()); public void destroy() { // TODO Auto-generated method stub } ...... 

The template property matches the filter to a URL. And don't forget to use @SlingFilter

In the template property, you can also use regx "/.*", which may be. This is tried and tested code.

There is also no need to register it with the bundle activator or ExtHttpSevice.

+8
source

All Articles