简体   繁体   中英

Grails Filter regexs

I am new to grails and so far i have only been able to use simple filters. I want to use filter in an efficient manner.

(I am using grails 2.4.3, with jdk_1.6)

I want to create a filter to allow accessing AppName/ and AppName/user/login and i could not get it right! I wanted to use regex but i am not getting it right!

i tried this

       loggedInOnly(uri:'/**',uriExclude :"*.css|*.js|*image*|/|/user/login"){
            before = {
                println "### ###### #### #"
            }
        }

and i also tried to revers the regex parameter, but i am getting no luck! I searched all of google but i could not find a single thread to tell me how filter regex work!

i know i could create xxxx(controller:'*', action:'*') filter then use the controllerName and actionName parameters to check! But there gotta be a better way!

My question in a nutshell: How does regex work in filters?

First, take a closer look at the documentation . Notice that uri and uriExclude are ant paths and not regular expressions. Keeping that in mind if you look how ant paths function you will see they aren't capable of logical or s.

So, with all of that in mind it's back to using enabling regex and using the find attribute instead.

loggedInOnly(regex: true, find: '(.​*.css|.*.js|.*image.*|\\/|\\/user\\/login)​', invert: true){
  before = {
    ...
  }
}

Notice I hae used invert to have this filter apply to anything that doesn't match any of the patterns inside the find . Also, I wrote this off the top of my head so you may have to spot check the regular expression in your application (I did check it using groovy web console to make sure I didn't really mess up the syntax).

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM