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

2 Comments

  1. satyendra

    It’s working in Liferay-7.1

    Reply
  2. Ivan

    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.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *