regexgrailsgrails-2.4grails-filters

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?


Solution

  • 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 ors.

    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.