Issue
I am working on an Spring Application in which I want to do some security checks like MD5 checking of files, DB checking, Application version check etc.
I have read about Interceptors and Filters but still a bit confused about which one is good to use.
Whatever documents I have read, it is specified that filters and interceptors both can be used for Logging and auditing so which one is good for this scenario. Also all this security checks (MD5 checking of files, DB checking, Application version check) can be configured means DB check is allow, Application version check is allow likewise.
Solution
In my opinion one of the biggest difference between Filters and Interceptors is:
Filter works only in J2EE web applications, you can not use outside of the application servers, Interceptors can work in different components and not depends on the web layer, in summary interceptor have a wide field than filters. If you are planning to move some component outside the container, you should consider use interceptors.
Filters work more in the request/response domain, in the other hand interceptor act more in the method execution domain.
If you need to do something that could affect the request or response to your application such as logging, security, audit, or you will affect the data coming on them, your option is filter, don't forget the plug ability that those provides.
Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events
Answered By - Koitoer