Grails filters with static resources?
I'm converting an old school Java filter to a Grails filter. (The filter is recording access to particular static image.)
The problem is, I can't get it fire! Does Grails support filters in front of static resources?
class EmailImageFilters {
def filters = {
emailFilter(uri: '开发者_运维问答/images/**') {
log.info "email filter ${new Date()}"
before = {
return true;
}
}
}
}
I am using the UIPerformance plugin, but it's not turned on in development mode.
Grails 1.3.5
I can get it to fire when:
emailFilter(controller: '*', action: '*') {
but I need it to be through static resources; e.g. /images
Suggestions?
Static resources aren't served by a controller, so Grails filters (which are wrappers for Spring controller interceptors) won't fire for requests for them. You need to register a servlet filter in web.xml to process static resources.
Create a class in src/java or src/groovy that implements javax.servlet.Filter
, then run grails install-templates
and edit src/templates/war/web.xml
to register it, something like
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.mycompany.myapp.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Change the value of url-pattern to be more specific if you don't want it to fire for all requests.
精彩评论