Learn Liferay7/DXP Filter Hook in 10 Minutes

dev_admin May 1, 2017

In my last post, I discussed about action hook. In this post, you will learn about filter hook in Liferay7/DXP.

Just a quick recap in case you have not read my previous post. Liferay provides mechanism to override behaviour for out-of-the-box features is called hook. Liferay supports following types of hook,

  • Struts Action Hook
  • Filter Hook
  • Jsp Hook
  • Language properties Hook
  • Model Listener hook
  • Service Wrapper Hook

After looking at above list of filters, you may have question that where filter hook is applicable in development?

Filter hook are used for intercepting HTTP request to execute piece of code before it reaches to execution logic. This is like HTTP Filter.

Following are the prerequisite to start with hook development in Liferay 7/DXP.

Pre-requisites:

There is a difference in way to implement hook in Liferay 7/DXP compare to prior versions as DXP does not provides option for XML configurations. Now Let’s create Filter hook in liferay7/DXP using following steps,


  1. Create Liferay Workspace Project:

    To create a Liferay Workspace in IDE follow the following steps:

Select File menu → click New → select Liferay Workspace Project. Upon creation of the workspace, the structure should look like below:


  1. Create Module:

    Next step is to create a Liferay Module Project.

Open File menu → click New → select Liferay Module Project. Provide project name as ‘FilterHook’ and click on ‘Next’.


  1. Configure hook properties:

    Provide component name and package path for the filter class and click on Finish.

Now class FilterHook with package com.enprowess.filterclass is created in workspace module as follow,


  1. Implement Component:

    In class, you need to add following property inside @Component.


  1. Implement code:

    Please note that your filter class will extend BaseFilter class. You will need to override processFilter() method which will take parameters as HttpServletRequest request, HttpServletResponse response, FilterChain filterChain.Please see following piece of code.

Now, you have filter hook ready to deploy in Liferay.

We at EnProwess believes in sharing knowledge to help open source community to grow. In my next post, I will talk about JSP hook. Till then happy learning!

Blog by,

Maitrik Panchal.

4 comments

  1. Good post. I want to overwrite existed filter. How should I do?
    For example, I found If I open AutoLogin Filter, it will afftect all portal, all urls. I want to make the affected scope smaller by changing url pattern. Is it possible?

    1. Hi Ying.c.Wang,

      There are 3 different possibilities and based on your requirement you can choose one of the following approach.

      1. If you want to apply your custom logic to auto login of Liferay, you can implement it by following way

      @Component(immediate = true)
      public class MyAutoLogin implements Autologin {

      }

      2. You can create new filter for specific url as suggested in this blog but If you want to modify OOTB AutoLoginFilter provided by Liferay, you need to create EXT plugin. Till Liferay 7 GA4, there is no way to create EXT plugins but it is expected from Liferay7 GA5.
      What we suggest here is to disable autologin filter by following entry in portal-ext.properties file
      com.liferay.portal.servlet.filters.autologin.AutoLoginFilter=false
      And then you can create your own filter with your own logic.

      3. If you want to add some action which can be performed after login then you can create post login hook. you can take reference of post login hook from here.
      http://www.enprowess.com/blogs/osgi-based-liferay-postloginaction/

    1. Hi Manat,

      Thank you for the comment.
      Is it custom service wrapper class you have? If you can elaborate objective/requirement in detail I would be happy to help you.

Leave a comment

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