Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Sling filter not working when using SlingServletFilter annotation

Avatar

Level 9

might be best explained with an example:

Putting everything inside the component annotation works. When I tried using SlingServletFilter as suggested by Apache in their Sling Filter page (https://sling.apache.org/documentation/the-sling-engine/filters.html), the filter stop working.

 

Any ideas? Thanks

(service = Filter.class, property = {
        "sling.filter.scope=REQUEST",
        "sling.filter.pattern=^/content/myapp/sg/en/login.*",
        "service.ranking:Integer=101"
})

/*@Component
@SlingServletFilter(
        scope = {SlingServletFilterScope.REQUEST},
        pattern = "^/content/myapp/sg/en/login.*"
)
@ServiceRanking(101)*/

 

3 Replies

Avatar

Community Advisor

Hi @jayv25585659 ,

The @SlingServletFilter annotation is part of Apache Sling, not AEM’s OSGi annotations. In AEM 6.5 / AEMaaCS, it often won’t be processed unless you also enable Sling annotation processing in your Maven build (via the Sling Maven SCR plugin or bnd).

That’s why your filter works with plain @Component + property=… but not with @SlingServletFilter.

try approch

Stick to the OSGi R7 @Component with properties (recommended and supported in AEM).

Or, if you want to use @SlingServletFilter, ensure your build is configured to process Sling annotations (extra setup, not default in AEM projects).

Use the working @Component(service=Filter.class, property=…) form — that’s the supported way in AEM.

Hrishikesh Kagane

Avatar

Administrator

@jayv25585659 Just checking in! Were you able to get this resolved? If you found your own solution, sharing the details would be a big help to others who might face the same issue later on. And if one of the replies here helped, whether it fully solved the problem or simply pointed you in the right direction, marking it as accepted makes it much easier for future readers to find. Thanks again for helping close the loop and contributing to the community!



Kautuk Sahni

Avatar

Community Advisor

Hi @jayv25585659,

When you use the Sling annotation:

@Component(service = Filter.class)
@SlingServletFilter(
  scope = { SlingServletFilterScope.REQUEST },
  pattern = "^/content/myapp/sg/en/(?:login|members).*"
)
@ServiceRanking(101)
public class MyFilter implements javax.servlet.Filter { ... }

BND must turn those property-type annotations into real OSGi service properties. If your project isn’t set up for that, the filter registers without sling.filter.scope / sling.filter.pattern, so Sling never calls it - hence “works with @Component properties, stops with @SlingServletFilter”.

Try this:

  1. Add the annotation dependency (provided scope):

    <dependency>
      <groupId>org.apache.sling</groupId>
      <artifactId>org.apache.sling.servlets.annotations</artifactId>
      <version>1.x.x</version>
      <scope>provided</scope>
    </dependency>
  2. Make sure BND processes OSGi R7 annotations + property types.
    Using maven-bundle-plugin, ensure DS is enabled (modern archetypes already do):

    <plugin>
      <groupId>org.apache.felix</groupId>
      <artifactId>maven-bundle-plugin</artifactId>
      <extensions>true</extensions>
      <configuration>
        <instructions>
          <-dsannotations>*</-dsannotations>
          <-metatype>true</-metatype>
        </instructions>
      </configuration>
    </plugin>

(If you use bnd-maven-plugin, ensure it’s recent - Bnd 5+ - so @ComponentPropertyType is handled.)

  1. Verify at runtime:

  • /system/console/services - your class - you should see
    sling.filter.scope = [REQUEST],
    sling.filter.pattern = ^/content/myapp/sg/en/(?:login|members).*,
    service.ranking = 101.
    If these aren’t there, your build still isn’t generating properties.

If you don’t want to touch build config, the safe, supported route is exactly what you had working:

@Component(service = Filter.class, property = {
  "sling.filter.scope=REQUEST",
  "sling.filter.pattern=^/content/myapp/sg/en/(?:login|members).*",
  "service.ranking:Integer=101"
})
public class MyFilter implements Filter { ... }

Either way works; the first is “Sling style” (needs build support), the second is plain OSGi (works out of the box in most AEM projects).


Santosh Sai

AEM BlogsLinkedIn