Grails过滤器与静态资源? [英] Grails filters with static resources?

查看:234
本文介绍了Grails过滤器与静态资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将老派的Java过滤器转换为Grails过滤器。 (过滤器正在记录对特定静态图像的访问。)

问题是,我无法启动它! Grails支持静态资源之前的过滤器吗?

  class EmailImageFilters {
def filters = {
emailFilter(uri:'/ images / **'){
log.infoemail filter $ {new Date()}
before = {
return true ;






I我使用的是UIPerformance插件,但在开发模式下没有打开。



Grails 1.3.5



I可以在以下情况下触发:

  emailFilter(controller:'*',action:'*'){

但是我需要通过静态资源;例如/ images



建议?

解决方案

静态资源不是由控制器提供的,所以Grails过滤器(它们是Spring控制器拦截器的包装器)为他们的请求火。您需要在web.xml中注册一个servlet过滤器来处理静态资源。
$ b 在src / java或者src / groovy中创建一个实现 javax.servlet.Filter ,然后运行 grails install-templates 并编辑 src / templates / war / web.xml 来注册它,就像

 < 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>

更改url-pattern的值,如果您不希望为所有请求。


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.

这篇关于Grails过滤器与静态资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆