Liferay DXP Custom Filter Tutorial

Liferay DXP Custom Filter Tutorial will drive you on creation of Custom Filter and you can override existing filter.

Installations:

Liferay DXP Custom Filter Creation:

    • Create Component Class in the Module Project: CustomFilter.java
    • For Filters, OSGI components requires below configuration:
      • Class need to extend BaseFilter
      • @Component Configuration is:
      • @Component(
         immediate = true, 
         property = {
         "servlet-context-name=", 
         "servlet-filter-name=Custom Filter",
         "url-pattern=/*" 
         }, 
         service = Filter.class
         )
    • CustomFilter.java class is:
      1. package org.demo.liferay.actions;
        
        import java.util.Enumeration;
        import javax.servlet.Filter;
        import javax.servlet.FilterChain;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import org.osgi.service.component.annotations.Component;
        import com.liferay.portal.kernel.log.Log;
        import com.liferay.portal.kernel.log.LogFactoryUtil;
        import com.liferay.portal.kernel.servlet.BaseFilter;
        
        
        @Component(
         immediate = true, 
         property = {
         "servlet-context-name=", 
         "servlet-filter-name=Custom Filter",
         "url-pattern=/*" 
         }, 
         service = Filter.class
         )
        
        public class CustomFilter extends BaseFilter {
         @Override
         protected void processFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
         throws Exception {
        
         _log.info("Filter is invoked");
         super.processFilter(request, response, filterChain);
         }
        
         @Override
         protected Log getLog() {
         return _log;
         }
        
         private static final Log _log = LogFactoryUtil.getLog(CustomFilter.class);
        }
        
        
    • Now deploy the bundle and you can see the bundle started message in the logs
3 thoughts on “Liferay DXP Custom Filter Tutorial”
  1. I Need ti pass some parameters to my implemented filter.
    Forse the old school ( web.xml) It is simple, but using osgi i’m not able to undestand the “standard” to pass init parameters to filter.

  2. Hi, how can I do if I want to apply different patterns? For example, /images/* and /img/*
    Somebody knows?

Comments are closed.