Fórum

Proper way to add a servlet filter?

thumbnail
David H Nebinger, modificado 12 Anos atrás.

Proper way to add a servlet filter?

Liferay Legend Postagens: 14914 Data de Entrada: 02/09/06 Postagens Recentes
Okay, this is really kicking my butt, and I'm not sure why...

So our Liferay server sits behind a load balancer. All incoming requests have the remote IPs/Hosts set to the IP and hostname of the load balancer system, which really hoses the auditing stuff I've been adding.

So our load balancer guy adds a custom header to the request that has the remote IP of the actual client.

So now I need to implement a replacement AuditFilter that will look for the custom header and use it's value rather than the one that is part of the HttpServletRequest.

I cannot just override the class in an EXT plugin because it doesn't get invoked (the one from portal-impl.jar seems to get precedence).

I cannot add a new filter in an EXT plugin because the deployer code pretty much ignores the liferay-web.xml file when deploying the EXT plugin.

If I manually insert my new <filter /> stuff into ROOT/WEB-INF/liferay-web.xml (I know, I know, it is a bad thing to edit the files in ROOT directly), even though my filter is actually deployed with the EXT plugin, I get class not found exceptions so it would not seem to be available in the portal scope.

So the question is, what is the right way to add a new filter to Liferay? And by right way, I'm hoping there is a method to bundle this in an EXT plugin and deploy w/o having to touch the ROOT web application at all...
thumbnail
Mika Koivisto, modificado 12 Anos atrás.

RE: Proper way to add a servlet filter?

Liferay Legend Postagens: 1519 Data de Entrada: 07/08/06 Postagens Recentes
Since you have liferay-web.xml you must be using a Liferay 6.0 EE SP2+ so you can inject a new filter from a regular hook plugin. No EXT needed. Just create a filter class inside the hook and then in your liferay-hook.xml add the filter definition like this:
	<servlet-filter>
		<servlet-filter-name>My Filter</servlet-filter-name>
		<servlet-filter-impl>com.liferay.sample.hook.filter.SampleFilter</servlet-filter-impl>
		<init-param>
			<param-name>url-regex-ignore-pattern</param-name>
			<param-value>^/html/.+\.(css|gif|html|ico|jpg|js|png)(\?.*)?$</param-value>
		</init-param>
	</servlet-filter>
	<servlet-filter-mapping>
		<servlet-filter-name>My Filter</servlet-filter-name>
		<before-filter>SSO CAS Filter</before-filter>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</servlet-filter-mapping>


You can define where it's injected by using before-filter or after-filter element in the servlet-filter-mapping and this is the name of the filter in ROOT/WEB-INF/liferay-web.xml.

Hope this helps.
Mark Fitzgerald, modificado 10 Anos atrás.

RE: Proper way to add a servlet filter?

Regular Member Postagens: 108 Data de Entrada: 15/10/10 Postagens Recentes
I must be missing something in the deploy process. Can anyone show me where my mistake is?

  • I've created a class that extends NtlmFilter. This class is in a newly created hook.
  • I've updated the liferay-hook.xml to include the same tags that identified (with changes to class name and package).
  • When I deploy the hook, the hot deployer complains that it can't find the newly created class.
  • I believe that this is due to the fact that the new class isn't copied to the ROOT webapp.
  • I was able to make my modified NtlmFilter class work by putting it in a jar file and copying it to the ROOT/WEB-INF/lib directory, but that seems to defeat the purpose of making a hook.


According the this post, I should be able to create a hook that extends a filter, deploy it, and it should just work, without a restart. Is that a correct understanding? I understand how a hook delivers jsp file changes, but how does a hook deliver newly created class files?
thumbnail
Mika Koivisto, modificado 10 Anos atrás.

RE: Proper way to add a servlet filter?

Liferay Legend Postagens: 1519 Data de Entrada: 07/08/06 Postagens Recentes
Hooks can't extend anything that is in portal-impl.jar. The hook filter will be executed in the plugin classloader.
Mark Fitzgerald, modificado 10 Anos atrás.

RE: Proper way to add a servlet filter?

Regular Member Postagens: 108 Data de Entrada: 15/10/10 Postagens Recentes
Mika Koivisto:
Hooks can't extend anything that is in portal-impl.jar. The hook filter will be executed in the plugin classloader.



Thanks for the clarification. Is there a document that identifies which filters are extensible, like what David was doing, and which are not?
thumbnail
Mika Koivisto, modificado 10 Anos atrás.

RE: Proper way to add a servlet filter?

Liferay Legend Postagens: 1519 Data de Entrada: 07/08/06 Postagens Recentes
David was replacing not extending the filter. I think all Filters are in portal-impl.
thumbnail
jelmer kuperus, modificado 12 Anos atrás.

RE: Proper way to add a servlet filter?

Liferay Legend Postagens: 1191 Data de Entrada: 10/03/10 Postagens Recentes
Also AuditFilter extends from BasePortalFilter which means you can disable it via a property in portal-ext.properties

com.liferay.portal.servlet.filters.audit.AuditFilter=false


What we did in 6.0.6 was disable the existing filter, add the new filter to ext-impl and configure it in ext-web/docroot/WEB-INF/web.xml

Changes in there would be merged into the ROOT web.xml