securitygrailsfilter

Does a Grails filter need to be registered to make it work


I'm trying to write a Filter for my web-app. I read the documentation, and wrote this dummy filter in my grails-app/conf directory

class SecurityFilters {
   def filters = {
       someFilter(controller:'*',action:'*') {

              write('Filtering')
           
       }
   }
}

Next thing I do is set a breakpoint on the write statement, but it just doesn't stop there.

Do I need to "register" this filter or anything? Spring may be bodering?

From this question, it doesn't look like it.

Maybe I'm doing something wrong, or overlooked anything?

Update

class SecurityFilters {
   def filters = {

       all(controller:'*',action:'*') {
        before={
              println 'Filtering'
              return false
        }
       }
   }
}

Solution

  • Two problems. One is there's no 'write' method - change it to 'println' and it should work. But a filter is comprised of some combination of before, after, and afterView sub-closures, so what you really want is

    class SecurityFilters {
       def filters = {
          someFilter(controller:'*',action:'*') {
             before = {
                println 'Filtering'
             }
          }
       }
    }
    

    But if you're really creating a security filter, please don't. It's too easy to do this incorrectly. The Spring Security Core and Shiro plugins have plenty of features and are easy to configure and use.